User Tools

Site Tools


howtos:backup_script_with_rsync

Table of Contents

Setup

You have a client with data on it you would like to make a backup of. You could make a local backup, however the client is mobile so moving the data of it is the best solution.

To get this working you need a server with rsync and a ssh server installed. And of course diskspace to keep the data on.

The client just need rsync capabilities. In my world that is not a problem but if you're running Windows you will need some tweaking through cygwin (not covered).

The script

rsync -avzgr --stats --protocol=29 /home/tdd /boot /etc \
 --exclude=".macromedia" \
 --exclude="vmware/" \
 --exclude=".Trash" \
 --exclude=".Trash-1000" \
 --exclude="No-rsync-stuff" \
 --exclude=".VirtualBox/" \
 --exclude="enc-data*" \
 --exclude="dataspace" \
 --exclude="pagefile" \
 --exclude="samba" \
 --exclude="smb" \
 --exclude=".ICAClient/cache"  \
 --exclude=".adobe/Flash_Player/AssetCache"  \
 --exclude=".cache"  \
 --exclude=".mozilla/firefox/*/Cache"  \
 --exclude=".nx"  \
 --exclude=".thumbnails"  \
 --exclude=".gvfs" \
 --exclude=".local/share/Trash" \
 --exclude=".evolution" \
tdd@192.168.1.1:/media/disk/backup > /home/tdd/backup.log

if [ $? != 0 ]
then
rsync -avzgr --stats /home/tdd /boot /etc \
 --exclude=".macromedia" \
 --exclude="vmware/" \
 --exclude=".Trash" \
 --exclude=".Trash-1000" \
 --exclude="No-rsync-stuff" \
 --exclude=".VirtualBox/" \
 --exclude="enc-data*" \
 --exclude="dataspace" \
 --exclude="pagefile" \
 --exclude="samba" \
 --exclude="smb" \
 --exclude=".ICAClient/cache"  \
 --exclude=".adobe/Flash_Player/AssetCache"  \
 --exclude=".cache"  \
 --exclude=".mozilla/firefox/*/Cache"  \
 --exclude=".nx"  \
 --exclude=".thumbnails"  \
 --exclude=".gvfs" \
 --exclude=".local/share/Trash" \
 --exclude=".evolution" \
/root/backup
fi

Set the script to run every hour (or what timeframe you like) by editing /etc/crontab and inserting this line:

@hourly     root    /home/tdd/backup.sh

The script will test if the online backup has succeeded and if it fails make a local backup.

The script has a special parameter appended (--protocol=29). This is to make rsync (version < 3.0.4) work with rssh.

rssh

rssh is a cool feature that makes it possible to use rsync but not opening up a full shell to the user. As we only want to copy files the user shouldn't be allowed to logon to the system with ssh.

Install rssh:

sudo apt-get install rssh

Edit the file /etc/rssh.conf and make sure the following is uncommented:

allowscp
allowsftp
allowrsync

This allows file transfer with scp, sftp and rsync. You could restrict it even further by only allowing rsync.

Now edit the users shell in /etc/passwd:

tdd:x:1021:1021::/home/tdd:/usr/bin/rssh

libnotify

With a Ubuntu client you can use its notification framework to tell how the backup went.

Look at the following script:

#!/bin/bash
BACKUPLOG="/home/tdd/backup.log"
su tdd -c "/usr/bin/notify-send 'Backup Start...'" 2>&1 > $BACKUPLOG

rsync -avzgr --stats /home/tdd /boot /etc \
 --exclude="vmware/" \
 --exclude=".Trash" \
 --exclude=".Trash-1000" \
 --exclude="No-rsync-stuff" \
 --exclude=".VirtualBox/" \
 --exclude="enc-data*" \
 --exclude="dataspace" \
 --exclude="pagefile" \
 --exclude="samba" \
 --exclude="smb" \
 --exclude=".ICAClient/cache"  \
 --exclude=".adobe/Flash_Player/AssetCache"  \
 --exclude=".cache"  \
 --exclude=".mozilla/firefox/*/Cache"  \
 --exclude=".nx"  \
 --exclude=".thumbnails"  \
 --exclude=".gvfs" \
 --exclude="Downloads" \
 --exclude=".local/share/Trash" \
 --exclude=".evolution" \
 --exclude="tdd/Music" \
tdd@172.21.1.1:/media/disk/backup 2>&1 > $BACKUPLOG
echo $?
if [ $? -eq 0 ]
then
	NUMBEROFFILES=`grep "Number of files transferred" $BACKUPLOG |awk -F ':' '{print $2}'`

	su tdd -c "DISPLAY=:0.0 /usr/bin/notify-send 'Backup Succesful Completed - $NUMBEROFFILES tranferred'"
else
	su tdd -c "DISPLAY=:0.0 /usr/bin/notify-send 'Backup Failed...'"
fi

After running this script a notification is display on the screen telling either how many files were backup'ed or that it failed.

howtos/backup_script_with_rsync.txt · Last modified: d/m/Y H:i by 127.0.0.1