Having recently had to create a new user for my computer due to several things going wrong, I took the opportunity of putting in several things that I'd been considering for some time, so here it is, finally! As far as I can see, the only times that you go below the hash line is to alter the paths for 'xclude txt' and 'backup.txt'. But they are fairly easy to see and trivial to do.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #!/usr/bin/env bash
#: Title : Backup.sh
#: Date : May 2013
#: Author : Keith Edwards (GLLUG), and Moggers87
#: Version : 5.1
#: Description : Backs up and retains a certain number of days worth before
: deleting them in a cycle. Works with rsync from the users crontab.
#: Options : Seven options, all shown under '# Variables'. You shouldn't need
: to insert anything after.
####################################################
# Variables
HOME_DIR="/home/boudiccas"
USB_DIR="/media/backup/back"
BACKUP_DIR="$USB_DIR/$(/bin/date +%Y%m%d)"
EMAIL="[email protected]"
SUBJECT="Backup Error"
EMAIL_MESSAGE="Couldn't find $USB_DIR, are you sure its plugged in or mounted?"
NUM_LEFT="14" #number of days backups to hold
####################################################
# Don't change anything after here.
# Check if rsync still running
#if pgrep -f 'rsync.*/home/'; then echo "E: old rsync still running" 1>&2; exit 1; fi
# Backup files
if [ -d $HOME_DIR ] & [ -d $USB_DIR ]; then
[ -d $BACKUP_DIR ] || mkdir $BACKUP_DIR \
>> /home/boudiccas/cron/backup.txt 2>&1
/usr/bin/rsync -avzP \
--exclude-from '/home/boudiccas/cron/xclude.txt' \
/home/boudiccas/ $BACKUP_DIR \
>> /home/boudiccas/cron/backup.txt 2>&1
# Delete those old backups! Yeah!
(find $USB_DIR -maxdepth 1 -type d -name "20*" | sort | tail -n $NUM_LEFT; \
find $USB_DIR -maxdepth 1 -type d -name "20*") | \
sort | uniq -u | xargs -n1 rm -rf \
>> /home/boudiccas/cron/backup.txt 2>&1
else
# Send an email
/bin/mail -s "$SUBJECT" "$EMAIL" < "$EMAIL_MESSAGE"
fi
|
I hope its useful for you, and would love some feedback on it. If you've read this blog for any length of time, you'll have seen that the backup script gets updated fairly regularly, but its interesting to see how its evolved.
Just think, one months time and its the summer solstice, the longest day of daylight here in the UK! I wish that the pace of the year would slow down and let me catch my breath! :)
Comments
comments powered by Disqus