ABHIONLINUX
Site useful for linux administration and web hosting

2009/08/16

How to make remote backup between your server and your remote server

To run a regular interactive FTP session.

lftp -u 'username,password' backupspace.remoteserver.com

To backup one or more files:

lftp -u 'username,password' backupspace.remoteserver.com -e "set ftp:ssl-protect-data true; mput local/dir/files* /remotedir; exit"

You need to set ftp:ssl-protect-data else you will not be able to store the file. If you want to make this a default option, add it to the lftp.conf file. e.g:

grep -qai "set ftp:ssl-protect-data true" /etc/lftp.conf || echo "set ftp:ssl-protect-data true" >> etc/lftp.conf

To restore a file from the FTP server to your server:

lftp -u 'username,password' backupspace.remoteserver.com -e "set ftp:ssl-protect-data true;mget /remotedir/files* -O /localdir; exit"

The -O option is not required it you wish to store to the current local directory.

To mirror a whole directory to the FTP server:

lftp -u 'username,password' backupspace.remoteserver.com -e "set ftp:ssl-protect-data true;mirror --reverse /local/dir/name remotedirname; exit"

--reverse means that the 'mirroring' is going in the reverse direction than 'normal'. i.e. from your server to the backup server. If you run man lftp there are a few other options to choose from. e.g. --delete to delete files on the backup server that do not exist locally. Or --continue to continue a mirror job. Or --exclude files to exclude certain files from the transfer.

To restore a whole directory from the FTP server to your server:

lftp -u 'username,password' backupspace.remoteserver.com -e "set ftp:ssl-protect-data true;mirror remotedirname /local/dir/name;exit"

To create a nightly cronjob that uploads a directory to the backup FTP server, create a /etc/crond.daily/ftpbackup file like this:

#!/bin/bash
lftp -u 'username,password' backupspace.remoteserver.com -e "set ftp:ssl-protect-data true;mirror --reverse /local/dir/name remotedirname;exit" > /dev/null

And run:

chmod +x /etc/cron.daily/ftpbackup

Then check the files have been mirrored as you expect the next day.

No comments:

Post a Comment