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
#!/usr/bin/env bash
#    Keith Edwards, GLLUG, December 2011.
#    Additions from moggers87
#shell script for use with rsync in user crontab

# Variables
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?"

# 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 $USB_DIR ]; then
        [ -d $BACKUP_DIR ] || mkdir $BACKUP_DIR \
                >> /home/boztu/cron/backup.txt 2>&1
        /usr/bin/rsync -avz \
                --exclude-from '/home/boztu/cron/xclude.txt' \
                /home/boztu/ $BACKUP_DIR \
                >> /home/boztu/cron/backup.txt 2>&1

        # Delete those old backups! Yeah!
        (find $USB_DIR -maxdepth 1 -type d -name "20*" | sort | tail -n 15; \
                find $USB_DIR -maxdepth 1 -type d -name "20*") | \
                sort | uniq -u | xargs -n1 rm -rf \
                >> /home/boztu/cron/backup.txt 2>&1
else
        # Send an email
    /bin/mail -s "$SUBJECT" "$EMAIL" < $EMAIL_MESSAGE
fi


Comments

comments powered by Disqus