How to backup your server over FTP
Recently I needed to find a way to backup the contents of one of my servers, onto another server in a completely different location. Normally I would use rsync over SSH but I don’t have SSH access on my remote server. So the next best thing I’ve found is lftp.
If you’ve ever wondered how to do this, read on.
Firstly you need to make sure that lftp is installed on one of the servers and an FTP server is installed on the other. Let’s call the server with lftp Server A, and the server with just FTP can be Server B.
Server A Configuration
- Linux operating system
- lftp application installed
- Firewall open to connected to Server B on FTP port
Server B Configuration
- Any operating system
- FTP application installed
- An FTP user created for Server A to use
- Firewall open to accept FTP connections
Now that you’ve got them both setup correctly, we can go ahead and create a script file for lftp to follow:
lftp -c "set ftp:list-options -a;
open ftp://ftp_user:ftp_pass@server_b;
lcd /path/to/local/dir1;
cd /path/on/ftp/server1;
mirror --reverse --delete --verbose
lcd /path/to/local/dir2;
cd /path/on/ftp/server2;
mirror --reverse --delete --verbose
lcd /path/to/local/dir3;
cd /path/on/ftp/server3;
mirror --reverse --delete --verbose
This will tell lftp how to connect to Server B and what to mirror. Make sure you change this script to suit your needs and your FTP user credentials. Save it anywhere you want, but for this example we’ll use: lftp.script
Now all you have to do is run lftp: lftp -f lftp.script
And watch it sync away. Note that this program will only transfer files that have changed or don’t exist since the last sync.
If you don’t have shell access to run this, put it in a shell script and use your hosting control panel to run it via cron or similar.
