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.

Be Sociable, Share!
This entry was posted by jc on Monday, October 10th, 2011 at 1:50 pm and is filed under Linux, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments : Leave a Reply

  1. Prefabrik says:

    Great topic. Thanx for information.I lived in the ftp problem

  2. gamerboy says:

    this inspired me to make a little script for myself. maybe it helps someone

    lftp -e ”
    set ftp:charset ISO-8859-15;
    set ssl:check-hostname no;
    set net:timeout 60;
    set net:max-retries 2;
    set net:reconnect-interval-base 5;
    open ftp://$user:$pass@$host;
    lcd $local_dir;
    cd $remote_dir;
    mirror –use-cache \
    –parallel=8 \
    –continue \
    –delete \
    –verbose \
    –log=lftp-log.txt \
    –exclude-glob screensnapr/ \
    –exclude-glob usage/ \
    –exclude-glob logs/ \
    –exclude-glob cgi-bin/ \
    –exclude-glob INCOMING/ \
    –exclude-glob 1_EBAY/ \
    –exclude-glob amazon/
    quit” | tee log.txt

    echo “lftp return $?”
    echo “Backup took $minutes minutes”

Leave a Reply