Rsync for automated & incremental Backup

A significant number of people have been repeatedly asking me how to set up an “incremental backup on an Unix server”, so I decided to write this post to help you guys out.

I’m running the following operation on a Debian server, but the procedure should almost be the same on every other Linux/Unix operating system.

 

First, you have to install rsync on your server. Type :

aptitude install rsync

And here is the command to start synching your files with a backup  folder :

rsync -Hurovl --delete /folder/to/backup /backup/folder

 

According to the rsync manual, here are the parameters I’m using :

  • -H, –hard-links : preserve hard links
  • -u, –update : skip files that are newer on the receiver
  • -r, –recursive : recurse into directories
  • -o, –owner : preserve owner (super-user only)
  • -v, –verbose : increase verbosity
  • -l, –links : copy symlinks as symlinks
  • –delete : delete extraneous files from dest dirs

 

Now, if you want to backup your files in a remote location, here’s how you do it.

 

First, you have to install openssh-client & openssh-server in both the local and the remote location. Type :

aptitude install openssh-client openssh-server

To start the backup, the command line on local server will be almost as simple as the first one (replace “user” and “remoteip” by your values) :

rsync -Hurovl --delete /folder/to/backup user@remoteip:/backup/folder

 

There’s still one issue with this method though : this command will prompt you every time to enter the remote password. Which is quite awkward if your goal is to put this into your crontab.

In order to solve this issue, let’s generate SSH keyfiles.

 

On the remote backup server, type :

ssh-keygen -b 4096 -t rsa

Follow the indicated steps, and by default, you should now have two new files in your /root/.ssh folder (id_rsa & id_rsa.pub)

Then, copy the public key in the list of authorized keys :

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

Eventually copy the private key in the /root/.ssh/ folder of your local server (with a CHMOD 600 to be safe), and here you go ; the job’s done !

 

I hope you found this small tutorial helpful. Don’t hesitate to post advice and questions in the comments section.

Share