-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
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 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:
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:
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:
#!/bin/bash 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 EOF
Then, all you have to do is add this script as a cron job and you’re good to go!