Using lftp for automated backups

Posted: January 2, 2012    Author:     Category:  Linux

Due to a hard drive failure recently, I discovered an excellent ftp client called lftp. I needed a way to backup the important directories such as /home /etc /var/www etc. quickly before the drive failed completely. I had never used any ftp client in the past for automated backups because I couldn’t find one that did what I wanted.

I needed a way of mirroring local directories to a remote backup and it had to delete files that no longer existed locally, a trivial task for something like rsync but finding a FTP client that provided this proved unsuccessful until I came across lftp.

According to Wikipedia:

lftp is a command-line file transfer program (FTP client) for UNIX and Unix-like systems. It was written by Alexander Lukyanov, and is made available under the GNU General Public License.

lftp is available on most major Linux distributions and can be installed through your systems package manager.

Once you have it installed, you can launch it by running:

lftp username@hostname

You’ll be prompted for a password if required. You can do all the usual commands such as ls, cd, mkdir etc. and you can find out more commands by typing help.

The command that is of most interest to me is the mirror command. It mirrors a remote directory recursively ie. it copies the directories contents including sub directories. The mirror command itself takes a number of parameters:

-c, --continue          continue a mirror job if possible
-e, --delete            delete files not present at remote site
-s, --allow-suid        set suid/sgid bits according to remote site
    --allow-chown       try to set owner and group on files
-n, --only-newer        download only newer files (-c won't work)
-r, --no-recursion      don't go to subdirectories
-p, --no-perms          don't set file permissions
    --no-umask          don't apply umask to file modes
-R, --reverse           reverse mirror (put files)
-L, --dereference       download symbolic links as files
-N, --newer-than FILE   download only files newer than the file
-P, --parallel[=N]      download N files in parallel
-i RX, --include RX    include matching files
-x RX, --exclude RX    exclude matching files
-I GP, --include-glob GP        include matching files
-X GP, --exclude-glob GP        exclude matching files
-v, --verbose[=level]   verbose operation
    --use-cache         use cached directory listings
--Remove-source-files   remove files after transfer (use with caution)
-a                      same as --allow-chown --allow-suid --no-umask

Care should be taken when using the mirror command because by default the command mirrors a remote directory to the local file system so when you think you’re copying local files to a remote location, you’re actually overwriting your local files with the files from the remote server!
To mirror files from the local file system to the remote, the -R parameter should be passed to the mirror command:

mirror -R /path/to/local/ /path/to/remote/

You can delete files on the remote server that no longer exist on the local file system by passing the -e parameter to the command above:

mirror -Re /path/to/local/ /path/to/remote/

As from what can be seen above, there is a substantial number of options that can be passed to the mirror command. The only extra argument I use is –use-cache to speed up the transfer process.

You can run lftp from a bash script like so:

lftp -u username,password ftphost << EOF
mirror -Re --use-cache /home/ /backup/
mirror -Re --use-cache /etc/ /backup/
mirror -Re --use-cache /var/www/ /backup/
quit 0


Then, all you have to do is add this script as a cron job and you’re good to go!


2 Responses

  1. Are you sure ‘use-cache’ works between two separate lftp runs?
    From my experiences lftp cache is only ‘in memory’ cache, which means, that second run of lftp will have empty cache too (I checked it with ‘cache stat’ option – after lftp relaunch cache was empty).

    I was looking for some kind of ‘sending commands from crontab to existing lftp process’ (thats how I entered your blog) – but is it anyhow possible?

  2. I recommend to use the –no-symlinks switch, because if you backup a folder containing symlinks this backup would fail.

Leave a Reply

Home Linux Using lftp for automated backups