28 Feb 2011

Bash script lftp with lock file


Bash script to upload a directory to a remote ftp site.

Uses the built in mirror function of the lftp ftp client.

A lock file is created so script so two instances of the script cannot conflict with each other.
#!/usr/bin/bash
LOCKFILE=/tmp/ftp_mirror.lock

if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
   exit # already running
fi

HOST="ftphost"
USER="username"
PASS="password"

LCD="localpath"
RCD="remotepath"

# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}

# upload files
lftp -c "set ftp:list-options -a;
open ftp://$USER:$PASS@$HOST;
lcd $LCD;
cd $RCD;
mirror --reverse"

rm -f ${LOCKFILE}

No comments: