Ninetynine is a creative design & coding agency

Ubuntu: backup to USB drive on mount

with 10 comments

Hi everyone,

I always was sloppy with my backups and I really needed to do something about it. I bought a new Freecom HDD 500GB and what I wanted to do is quickly backup my files every time I plug the device in, and it should give me a proper notice when the backup is done.

Doesn’t sound that hard, or does it?

It became clear that I needed a little thingy called “udev”. And with big help from unutbu from ubuntuforums.org I managed to get a very nice script up and running.

First thing you have to do is to know how your system recognizes your USB drive.

I followed this great tutorial here. (Don’t bother the fstab!)

My rules look like these.

1
2
 
SUBSYSTEMS=="usb", KERNEL=="sd?1", ATTRS{product}=="Freecom Network Drive", NAME="freecomHD", RUN+="/usr/bin/usb_backup.sh"

Note that I have saved them under “/etc/udev/rules.d/81-local.rules”, because /etc/udev/rules.d/README says. It would also be safer I you select your device using UUID.

Files should be named xx-descriptive-name.rules, the xx should be
chosen first according to the following sequence points:

80 rules that run programs (but do not load modules)

After adding your rule, you need to restart udev.

$ sudo /etc/init.d/udev restart

Next install libnotify-bin through synaptic. This will make it possible for us to show notifies as if it were GNOME itself ;)

Next we’ll need to hack the script a little bit:


Place this in “/usr/local/bin/alt-notify-send”

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
#!/bin/sh
user=`whoami`
pids=`pgrep -u $user gnome-panel`
title=$1
text=$2
timeout=$3
 
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
        DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
        notify-send -u low -t $timeout "$title" "$text"
done

This is because

The reason why notify-send was not working was because notify-send uses the DBUS_SESSION_BUS_ADDRESS environment variable and that variable was not set in the shell that runs your script. This environment variable is set when you start a gnome session, but is not set when root runs a udev rule.

Moreover, one user can not send messages to another user using notify-send. So we use “su” to change root to your normal user, and we use the alt-notify-send script to find and set the DBUS_SESSION_BUS_ADDRESS before launching notify-send.

(unutbu)

Then I created a file /usr/bin/usb_backup.sh.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
{
mount_point=$(grep freecomHD /etc/mtab)
attempts=1
while [ -z "$mount_point" ] && [ "$attempts" -le 50 ]; do
    # $mount_point has not been found
    # quit if this fails more than 50 times. 
    # This should not be necessary, but it better to be safe
    # than have the script trapped in this loop forever for
    # some unforeseen reason.
    sleep 1
    mount_point=$(grep freecomHD /etc/mtab)
    attempts=$(($attempts+1))
done
 
if [ -n "$mount_point" ]; then
    su ruben alt-notify-send "Backup Message" "USB Backup device detected" 0
 
    rsync -a /home/ruben/ /media/FREECOM\ HDD/
 
    su ruben alt-notify-send "Backup Message" "Your USB Backup has completed" 0
fi
} &

Note that my user is named “ruben” and that you should replace it by your own. Also replace the the name in mount_point to the name you have given in your udev-rules.

Also note that the whole process is run in a new process (due to the “&” character). Otherwise ubuntu will try to do the backup before the mount. But it needs to be mounted before backup. This requires the little hack on top of the script ;)

Don’t forget to make the scripts executable!

1
2
$ sudo chmod 755 /usr/bin/usb_backup.sh
$ sudo chmod 755 /usr/local/bin/alt-notify-send

It may be a good thing to tweak your rsync a little bit, so you don’t lose too much time every time you plug in.

And that’s it! This was a little harder than I thought. But then again, it shows that you’re absolutely free to do whatever you want on ubuntu. This is why we all love our tux.

Hope this helped somebody. And stick around to read more about all sorts of stuff!

Happy coding!

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks

Written by ruben

March 25th, 2009 at 12:37 pm

Posted in Ubuntu

Tagged with , ,