Backup from one host to another using rsync / ssh

rsync allows you to copy / send data from one host to another, ideally over an ssh connection to make it secure. A great feature of rsync is its ability to allow incremental backups. This means that once you’ve done the backup once the only data it sends the next time are files that have changed since the last check. This can make subsequent backups extremely quick.

First install rsync on Ubuntu / Debian

sudo apt-get install rsync

or if using CentOS

yum install rsync

These examples assume that you are using private / public keys for authentication (check out my guide for setting up the keys)

login to the server which has the data that you want to backup, make sure you are logged in as a user that has permission to access that data.
The following command will copy everything from this servers mywebsite directory to the mywebsite directory on the remote machine under bobs user account.

rsync -v -a -r -e ssh /var/www/mywebsite/

the -r option means rsync will copy all files and folders recursively, the -e option sets the option for ssh. is the username of the account that you are logging into at the server you want to receive the backup. You can of course just use IP addresses instead of host names.

I want to exclude a folder from the copy! No problem, lets assume that we don’t want to copy anything from the /mywebsite/video directory as the files are just too big and we don;t need them backed up. Just run the following command.

rsync -v -a -r -e ssh --exclude /video /var/www/mywebsite/

–exclude tells rsync not to copy the videos directory. Unlike the host and destination paths which have to be absolute paths on the server, the excluded directory is relative to the main source directory.

The next time you run the same rsync command only the files that have changed will be sent to your destination machine!

If you’d like to see a progress of how much data is left to copy etc, use the –progress command (not really useful if you are putting the command into cron or a script. Nor is it particularly useful if you’ve used the -v command), you can also add the –delete command to remove any files on the destination that have also been deleted on the source since the last backup was run.

Print Friendly, PDF & Email

More Like This


CentOS Linux Ubuntu / Mint Web Hosting
  • Post a comment