Automated Backup

A while ago (google cache of bash/bat scripts) I showed you my Windows -> Linux backup script.

Now let's start a Linux -> Linux backup script, that requires no user intervention.

This tutorial assumes you have SSH up and running, and that you do all the following as 'user'. Bother computers should have a user called 'user'.

192.168.0.101 - Workstation 1
192.168.0.102 - Workstation 2

First type 'ssh-keygen -t dsa' to generate public/private keys. Do this on workstation 1.

The default path is fine, and the passphrase should be empty. Just press enter.

Now copy the public key to Workstation 2. You can do this easily in one command:


cat ~/.ssh/id_dsa.pub | ssh user@192.168.0.102 "cat - >> ~/.ssh/authorized_keys"

Time to test that out.

ssh user@192.168.0.102

If it doesn't ask for a password, so far so good!

From this point on, you should 'man rsync'. This should give you an idea as to what you can do. Let's try something simple, for a simple scenario.

Workstation 1: Important Documents are stored here in /home/user/documents, used for a personal website.
Workstation 2: Spare machine, used for browsing the web.

On 'Workstation 1', create the following script.


#!/bin/bash
#this file in /home/user/scripts/backup1
tar cvzf /temp/docs-`date +%F`.tar.gz /home/user/documents
tar cvzf /temp/www-`date +%F`.tar.gz /var/www
rsync -e ssh -avz --delete-after user@192.168.0.102:/home/user/backup /tmp/*.gz

Test out this script, see if it works. If so, time for crontab.

crontab -e

30 3 * * 1 /home/user/scripts/backup1 2>&1 > /var/log/workstation1_backup.log

That crontab line will backup your files at 3:30 every Monday.

Just be sure the backed-up files don't eat up too much space; you may wish to update less often, or more often, as the case may be.