Remote backups using rsync

rsync is a software application for Unix systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction. rsync can copy or display directory contents and copy files, optionally using compression and recursion.

We’re going to configure a machine to sync files from a specific folder to a remote machine every day using rsync in four easy steps. This is ideal solution to do automated backups for servers and workstations.


1. Configure remote machine (if not using service by a third party)

Install rsync, it must be installed on both remote and local machines to be able to sync data

apt-get install rsync

Create a new user used by the local machine to connect to the remote machine

adduser user1 --home /home/user1

Replace user1 with your preferred username. You can set any password you like and make it complicated because it’s only needed once.
2. Configure the local machine (the one being backed up)

Install rsync

apt-get install rsync

Create a private key to avoid passphrase popup to be able to do automatic backups

ssh-keygen -t dsa

Copy the key to the remote machine

ssh-copy-id -i .ssh/id_dsa.pub user1@remote.machine.com

Replace user1 with your username and remote.machine.com with the remote machine’s hostname or ip address

The link between the two machine is set up and you no longer need password to connect to the remote machine using ssh. You can try: ssh user1@remote.machine.com
3. The actual backup process

Backup folders using the following command

rsync -avz --progress -e ssh /var/www user1@remote.machine.com:backup

Using this command, all files in the /var/www folder will be synced to the remote machine into a folder named backup
4. Automate the backup using cron

Open the cron config file for current user

crontab -e

Add a line similar to the following into the config file

0 3 * * * rsync -az -e ssh /var/www user1@remote.machine.com:backup >> /dev/null 2>&1

Your /var/www will be synced to the remote machine at 3am every day

4 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>