Share!

Automatic backup in Linux

From it’s man page, rsync is a program that behaves in much the same way that rcp does, but has many more options and uses the rsync remote-update protocol to greatly speed up file transfers when the destination file is being updated.

The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files across the network connection, using an efficient checksum-search algorithm described in the technical report that accompanies this package.

Say 192.168.1.10 (anduril) is the backup destination , herugrim is the host’s hostname, the following step will automatically rsync the home directory to the backup server.

Configure Password-less Login

Generate RSA key for use with SSH. Make sure the passphrase is empty.

user@herugrim ~ $ ssh-keygen -t rsa -C user@192.168.1.10
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): /home/user/.ssh/id_rsa.192.168.1.10
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.192.168.1.10.
Your public key has been saved in /home/user/.ssh/id_rsa.192.168.1.10.pub.
The key fingerprint is: 11:d9:23:2e:68:05:59:d9:ac:5a:00:69:17:3a:b4:24  

Copy the public key to the server

user@herugrim ~ $ ssh-copy-id -i .ssh/id_rsa.192.168.1.10.pub user@192.168.1.10

Test if passwordless login successful

user@herugrim ~ $ ssh user@192.168.1.10 -i .ssh/id_rsa.192.168.1.10
user@anduril:~$

Create Target Directory

user@anduril:~$ mkdir backup/herugrim -p

Manual Rsync (test if the thing works)

user@herugrim ~ $ rsync --verbose --progress --compress --rsh=ssh --recursive --times --perms --links --delete /home/user anduril:/home/user/backup/herugrim -e "ssh -i /home/user/.ssh/id_rsa.192.168.1.10"

Configure Cron for Automatic Backup

Edit your crontab entry by issuing this command

crontab -e

Add this entry to your crontab to rsync at 1:30 AM everyday

30      1       *       *       *     rsync --compress --rsh=ssh --recursive --times --perms --links --delete /home/user anduril:/home/user/backup/herugrim -e "ssh -i /home/user/.ssh/id_rsa.192.168.1.10"

Run cron if it’s not already started

sudo /etc/init.d/cron start     #depending on your linux distro