Automatic backups with rsync on ubuntu

I like my photos - so I'd like to keep them. I have bigger backup plans than this, but for now, this works nicely.

And no, I dont want to back up into the cloud, thankyou very much.

This is a couple of small scripts I've created (and one I borrowed) that automatically back up my laptop when I place it in its dock (into which is plugged a USB external hard-drive).

The basic idea is that udev will notice the filesystem appear, and will kick off a script that runs rsync.

I also want to be able to undock my laptop without panicing *too* much if I forget the backup is running, and some pretty notifications would be nice also. Backups start 5 mins (300 seconds) after docking, so I can plug / unplug the laptop quickly without upsetting things.

so…

you will need to install libnotify-bin installed

aptitude install libnotify-bin

And you will need to create a file in /etc/udev/rules.d/ containing these lines (edit to match your hardware)

ACTION=="add", ENV{ID_SERIAL_SHORT}=="12345678", RUN+="/usr/local/bin/kick-back-me-up.sh"
ACTION=="remove", ENV{ID_SERIAL_SHORT}=="12345678", RUN+="/usr/local/bin/kick-back-me-up.sh"

These will run the script “kick-back-me-up.sh” which I keep in /usr/local/bin and contains the following:

#! /bin/bash

/usr/local/bin/back-me-up.sh &
exit

This script then runs (in the background, so as not to hold up all other udev processing) the following script (called back-me-up.sh):

#! /bin/bash

if [ "$ID_FS_UUID" != "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ]; then
        exit 0
fi

if [ "$ACTION" == "add" ]; then
        sleep 300

        if [ -d /media/Backup/rsync_backup ]
        then
                if [ -f /tmp/backup.lock ] ; then
                                su ian alt-notify-send "Photos Sync" "Syncronization fail (In progress or prior failure)" 1000
                                exit 1
                fi
                touch /tmp/backup.lock
                su ian alt-notify-send "Photos Sync" "Syncronization start" 1000
                rsync -ruo /home/ian/Pictures /home/ian/.shotwell /media/Backup/rsync_backup
                if [ "$?" -eq "0" ] ; then
                        su ian alt-notify-send "Photos Sync" "Syncronization complete" 1000
                        rm /tmp/backup.lock
                else
                        if [ "$?" -ne "1" ]; then
                                su ian alt-notify-send "Photos Sync" "Syncronization fail (rsync error $?)" 1000
                        fi
                fi
        else
                su ian alt-notify-send "Photos Sync" "Syncronization fail (No target)" 1000
        fi
fi


if [ "$ACTION" == "remove" ]; then
        if [ -f /tmp/backup.lock ] ; then
                su ian alt-notify-send "Photos Sync" "Syncronization fail (Interrupted)" 1000
                rm /tmp/backup.lock
        else
                sleep 1
                pkill back-me-up.sh
        fi
fi

Edit this to taste :)

You'll notice that the FS UUID and the paths to backup are hardcoded.

The script will produce notifications of what its up to via a little script I found on the internet, I dont recall where:

#!/bin/sh
user=`whoami`
pids=`pgrep -u $user gnome-panel`
title=$1
text=$2
timeout=$3
icon=$4

if [ -z "$title" ]; then
 echo You need to give me a title >&2
 exit 1
fi
if [ -z "$text" ]; then
 text=$title
fi
if [ -z "$timeout" ]; then
 timeout=60000
fi

for pid in $pids; do
 # find DBUS session bus for this session
 DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS \
 /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`
 # use it

 #icon hack:
 if [ -z $icon ]; then
 DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
 notify-send -u low -t $timeout "$title" "$text"
 else
 DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
 notify-send -u low -t $timeout -i "$icon" "$title" "$text"
 fi
done

save it in /usr/local/bin and name it alt-notify-send.

Job done!

 
hacks/linux/rsync.txt · Last modified: 2011/07/03 13:41 by ian
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki