Thursday, January 18, 2007

Backing up your home directory to a remote server using cron

install cron:

sudo apt-get install cron

Allow all users to use cron:

sudo touch /etc/cron.deny

Add a job to rsync your home directory:

crontab -e

nano should pop up, add the following line, replacing username with your username and server with the server you are backing up to:

0 1 * * * rsync -r ~ username@server:~

Exit (Ctrl-X) and say yes to save when prompted.

Because the backup script will need to login to the remote server you need to setup ssh to allow you to login using your ssh key. If you don't have an ssh key create one with the following command, accepting defaults when prompted (do not set a passphrase):


Now copy your public key to the server to allow you to login automatically:

scp ~/.ssh/ username@server:~/.ssh/authorized_keys

Now test the backup by running, replacing username with your username on the server:

rsync -r ~ username@server:~

and see if your home directory is copied to the server correctly. Note with default settings rsync will not delete files that you have removed locally from the backup.

In order to solve that you can write your own script to handle the backup of the home dir. Here is an example:

#! /bin/bash

echo "Backing up files from $HOME"
echo "Backing up files to $TARGET"
rsync -Cavz --delete --delete-excluded --exclude-from=$HOME/.exclude_backup.txt $HOME $TARGET

echo "Completed"

exit 1;

This is the exclude file:

No comments: