Backing up your website using FTP on Linux
Linux provides a load of great command line tools for working with FTP, but by far my favourite is the lftp client – it is immensely powerful and allows you to work in a consistent way across all FTP operations. It is also very easy to use. Chances are you’ve got lftp installed already, but if not you should be able to install it using your default package manager:
apt-get install lftp
The easiest way to get started with lftp is to use it in interactive mode, typing commands into the command prompt. To connect to the remote FTP server type:
lftp -u ftpusername ftp.ftpserver.com
You can then use the help command to give you a breakdown of all of the FTP commands available to you. You’ll find many of them are very familiar: ls, cd, rm etc. To get help on any command type help commandname.
However lftp becomes most powerful when its scripted. You can create a script like the following to pretty much do whatever you want:
open -u ftpusername,ftpassword ftp.ftpserver.com ... some FTP operations here... close
Save this file as a text file, and then run it manually or from a bash script using:
lftp -f filename
One of the most useful (and simple) scripts that you can create is to back up the entire contents of your remote FTP account to your local PC:
open -u ftpusername,ftppassword ftp.ftpserver.com lcd /local/path/to/copy/to cd /remote/path/to/start/from mirror -v close
lcd points lftp to the relevant folder on your PC. cd points to the relevant folder on the remote PC. The mirror command will then download the contents of all changed files to your local PC. Once you’re happy with the way that it works, you can add the --delete parameter to the mirror command to delete any local files that are not found on the remote server.
You can also very easily add exclusions to the task, using the exclude parameter:
mirror -v --exclude somefolder/somesubfolder
In this case, nothing in somefolder/somesubfolder will be synchronised. This is especially useful for ignoring large image directories, or logfile directories.
You can also use the -R parameter to reverse the operation, so if you wanted to work locally and then sync the contents of the local folders back to the remote server, you can use this script:
open -u ftpusername,ftppassword ftp.ftpserver.com lcd /local/path/to/start/from cd /remote/path/to/copy/to mirror -R -v close
In this case all contents from the lcd folder will be copied to the remote directory specified by cd. Again you can specify exclusions using --exclude or use the --delete parameter to get rid of deleted files.
If you’re concerned about version control (overwriting changed files when synchronising) you can add the --only-newer parameter to mirror to stop it overwriting any files on the destination that are newer than the source.
You could pretty easily get a nice development workflow going by syncing down to your local PC before you start working, and then syncing up to the remote FTP server when you’re happy with the result.

