Rsync and Bash

Have had an interesting morning delving into Bash, on MSN with Jasper, which is not something I get to do often enough.

The upshot was a nice and fairly sophisticated rsync script to backup some files, which I won’t paste here as it’s Jaspers baby.

However, I did think up a [much] simpler script to enable a quick and dirty backup of any particular folder to any particular media.

#!/bin/bash
sudo rsync -av –progress –delete –log-file=/dir/$(date +%Y%m%d)_rsync.log /dir /media/dirBackup

You can of course exclude certain files from the backup with:

–exclude “/dir/.jpg”

Naturally, no one wants to type it out everytime and so lets make it into an executable script:

sudo chmod +x /path/rsync-backup.sh

So you now have an executable script you can call whenever you want, or you can of course create a cron job and have it run automatically.


Here’s a slightly more sophisticated backup script that revolves on a weekly basis, and then clears out any backups that are older then this.

#!/bin/sh
# directory to backup
BDIR=/home/$USER

# excludes file
EXCLUDES=$HOME/cron/excludes

# name of the backup machine
BSERVER=server

# password on the backup server
export RSYNC_PASSWORD=

# lets get down to it
BACKUPDIR=`date +%A`
OPTS=”–force –ignore-errors –delete-excluded –exclude-from=$EXCLUDES
–delete –backup –backup-dir=/$BACKUPDIR -a”

export PATH=$PATH:/bin:/usr/bin:/usr/local/bin

# the following line clears the last weeks incremental directory
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync –delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/
rmdir $HOME/emptydir

# now the actual transfer
rsync $OPTS $BDIR $BSERVER::$USER/current

One thought on “Rsync and Bash

  1. Feel free, sir. It’s only based on an idea I had – if you can improve it, post and I’ll steal it again :D

Leave a Reply