( http://simon.zilliken.name/index.php?archives/1-Creating-a-backup-disk.html ) Creating a backup disk June 29, 2008 ================= Up to now my way doing backups was the following: Switch on an external hard drive, open a console and run a backup-script. That may sound easy, but repeating easy tasks is stupid, so this had to be automated. My idea was to run the backup as soon as the drive is powered on, which can be done by udev. These are the steps: 1. identify the backup drive when it's powered on 2. create a unique device path for the backup partition (makes mounting easier) 3. mount the backup partition somewhere 4. run backup 5. unmount backup partition 6. notify me about successful backup or failure The first two steps are done by udev. Also, udev will run a short script that runs a backup script in background. If it's not run in background, udev will be blocked until the backup returns. The backup script does everything else. For a deeper look into writing udev rules, see this really good HowTo. So here are the steps: Plug in and switch on the drive. This time you need to know it's device file, let's assume it is /dev/sdb. Use udevinfo to get some information about the device: udevinfo -a -p $(udevinfo -q path -n /dev/sdb) For the udev rule, you can pick any information from the device and a single parent device as match keys. You can not combine attributes from multiple parent devices. The shortened output in my case is: looking at device '/block/sdb': KERNEL=="sdb" SUBSYSTEM=="block" DRIVER=="" ATTR{dev}=="8:16" ATTR{range}=="16" ATTR{removable}=="0" ATTR{size}=="156301488" ATTR{capability}=="12" ATTR{stat}==" 32 115 348 149 0 0 0 0 0 65 149" [...] looking at parent device '/devices/pci0000:00/0000:00:1a.7/usb1/1-3/1-3.4': KERNELS=="1-3.4" SUBSYSTEMS=="usb" DRIVERS=="usb" ATTRS{dev}=="189:5" ATTRS{configuration}=="" ATTRS{bNumInterfaces}==" 1" ATTRS{bConfigurationValue}=="1" ATTRS{bmAttributes}=="c0" ATTRS{bMaxPower}=="100mA" ATTRS{urbnum}=="155" ATTRS{idVendor}=="1058" ATTRS{idProduct}=="0403" ATTRS{bcdDevice}=="0602" ATTRS{bDeviceClass}=="00" ATTRS{bDeviceSubClass}=="00" ATTRS{bDeviceProtocol}=="00" ATTRS{bNumConfigurations}=="1" ATTRS{bMaxPacketSize0}=="64" ATTRS{speed}=="480" ATTRS{busnum}=="1" ATTRS{devnum}=="6" ATTRS{version}==" 2.00" ATTRS{maxchild}=="0" ATTRS{quirks}=="0x0" ATTRS{authorized}=="1" ATTRS{manufacturer}=="Western Digital" ATTRS{product}=="External HDD" ATTRS{serial}=="57442D5743414D3943323431373634" [...] I picked the highlighted entries for matching the device, but the KERNEL=="sdb" line has to be modified. First, you can't assume your drive will always be /dev/sdb if you use multiple external drives. Second, as I want a symlink for the backup partition (not the drive), I match the first partition of the drive (which is the only one anyway). Now, create the udev rule in /etc/udev/rules.d/10-local.rules that identifies the drive, creates a symlink and runs the kickoff script: KERNEL=="sd*1", SUBSYSTEMS=="usb", ATTRS{serial}=="57442D5743414D3943323431373634", SYMLINK+="backup", RUN+="/usr/local/sbin/run-backup" The rule must be in one line. /usr/local/sbin/run-backup is very short: #! /bin/sh /usr/local/sbin/backup & Finally, a skeleton for /usr/local/sbin/backup: #! /bin/sh mount /dev/backup /mnt/backup # place your favourite backup command here EXIT_STATUS=$? umount /mnt/backup # notification if [ $EXIT_STATUS = 1 ] then aplay signal_failure.wav else aplay signal_success.wav fi That's it. Playing a sound as notification is not the best solution, as you might not be present when the script finishes. If you have a better idea, let me know please.