You are not logged in.

#1 2007-05-04 21:05:46

mehldutt
Member
From: Germany
Registered: 2006-01-08
Posts: 128

[SOLVED] automatic usb-backup with udev-rules + script

I would like to have my usb-harddrive automatically start a backup as soon as it is plugged in.  And finally a bell is supposed to ring.
I've created an udev-rule and a backup-script as shown below.
However, instead of creating /dev/backup-drive first something strange is happening:
The bell rings 3 times, followed by the backup, followed by a 4th ring.
What's going on?

Here's may udev-rule:
## /etc/udev/rules.d/95-backup.rules
SUBSYSTEMS=="usb", ATTRS {serial}=="100", SYMLINK=="backup-drive", RUN+="/usr/local/bin/backup-thumb.sh"

fstab:
/dev/backup-drive    /media/backup   ext3     rw,user    0 0

and my script:
#!/bin/bash
sleep 10
rsync -vrtolgh --exclude '/.VirtualBox/' --delete /home/myhome /media/backup-drive
aplay /usr/share/sounds/phone.wav

Last edited by mehldutt (2007-07-03 20:28:27)

Offline

#2 2007-05-05 17:14:25

lanrat
Member
From: Poland
Registered: 2003-10-28
Posts: 1,274

Re: [SOLVED] automatic usb-backup with udev-rules + script

The syntax of your udev rule is all wrong - for example after SYMLINK you should use "+=" or ":=". "==" is for comparing to some value.

I think in rsync command line you want -H not -h (help) option. Also -v (verbose) is useless since you'll never see the output.
Another thing which will prevent running rsync properly is /media/backup-drive while you use /media/backup in fstab.

I suggest that you first try running rsync command from the command line "manually" and try if it works at all. After you'll make it work try the below suggestions.

Another problem: what makes the backup drive mounted under /media/backup directory when you plug it into usb slot ? Are you doing it somehow "manually" in the 10 second period after plugging ? Do you use automounter of some kind ?

If you intend to use ext3 as the backup drive filesystem you should also add sync command after rsync to make sure no data stays in RAM cache.

I think you need to read udev manpage first.

I can also advice you to read my udev rules for automounting usb devices (it's for any type of filesystem and any number of partitions):

KERNEL=="sd[b-z]", NAME:="%k", SYMLINK+="usbhd-%k", GROUP:="users", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SYMLINK+="usbhd-%k", GROUP:="users", NAME:="%k"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/mkdir -p /media/usbhd-%k"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/ln -s /media/usbhd-%k /mnt/usbhd-%k"
ACTION=="add", KERNEL=="sd[b-z][0-9]", PROGRAM=="/lib/udev/vol_id -t %N", RESULT=="vfat", RUN+="/bin/mount -t vfat -o rw,noauto,flush,dirsync,noexec,nodev,noatime,dmask=000,fmask=111 /dev/%k /media/
usbhd-%k", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,async,dirsync,noexec,nodev,noatime /dev/%k /media/usbhd-%k", OPTIONS="last_rule"
ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/rm -f /mnt/usbhd-%k"
ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/umount -l /media/usbhd-%k"
ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/rmdir /media/usbhd-%k", OPTIONS="last_rule"

I think it could be simplified and modified for your backup device like this:

SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", KERNEL=="sd[a-z]", NAME:="%k", SYMLINK:="backupdevice", OPTIONS="last_rule"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", ACTION=="add", KERNEL=="sd[a-z]1", SYMLINK:="backuppartition", GROUP:="users", NAME:="%k"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", ACTION=="add", KERNEL=="sd[a-z]1", RUN+="/bin/mount -t auto -o rw,noauto,async,dirsync,noexec,nodev,noatime /dev/%k /media/backup-drive"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", ACTION=="add", KERNEL=="sd[a-z]1", RUN+="/usr/local/bin/backup-thumb.sh", OPTIONS="last_rule"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", ACTION=="remove", KERNEL=="sd[a-z]1", RUN+="/bin/umount -l /media/backup-drive", OPTIONS="last_rule"

assuming that you have a single partition backup drive formatted as ext3 (or at least it's the first partition).
Also another assumption is that idVendor and idProduct are unique to your backup drive. If not you should add some extra ATTRS parameters that will ensure this combination is unique.
The directory /media/backup-drive must already exist (remove your fstab line - it's not needed in this case because mount command is run directly from the udev rule).

Replace all XXXX and YYYY with the values from lsusb for your backup device - plug it in, run lsusb and copy values which look like XXXX:YYYY near to the name of your backup device.

Anyway, you can play with different options and try to modify it yourself. The automounting rules work for me very well for some time (including flush option). The backup rules you need to test yourself :-)

Last edited by lanrat (2007-05-05 17:20:51)

Offline

#3 2007-05-05 18:45:09

mehldutt
Member
From: Germany
Registered: 2006-01-08
Posts: 128

Re: [SOLVED] automatic usb-backup with udev-rules + script

You are fantastic. smile:):)
That was as easy as copy and past.
It worked right away after adding the IDs.
Thanks for your extensive response.  I could learn a lot from you.

But one more Question:
Instead of a bell ringing I would rather have a message window pop up (Gnome).
Any suggestions on that?

Gary

Last edited by mehldutt (2007-05-05 19:21:58)

Offline

#4 2007-05-06 15:43:16

lanrat
Member
From: Poland
Registered: 2003-10-28
Posts: 1,274

Re: [SOLVED] automatic usb-backup with udev-rules + script

I don't know how to raise gnome window with some message using script (I'm using kde myself).

I think you could use something like Xdialog to do that (pacman -Ss dialog).

Assuming that you want to show a message on display 0

Xdialog --display :0 --msgbox "Backup finished" 0 0

However, this will probably not work until you allow to display any messages by users other than current X session user (udev script will run as root). You can allow this with

xhost +localhost

which must be run when you start your user X session (you can add it to autorun or something like this).

Please, browse the forums first and search for xhost keyword for some thoughts on security of this solution.

Last edited by lanrat (2007-05-06 15:45:16)

Offline

#5 2007-05-10 21:14:02

mehldutt
Member
From: Germany
Registered: 2006-01-08
Posts: 128

Re: [SOLVED] automatic usb-backup with udev-rules + script

So - now I'm finally happy with the following solution:

## /etc/udev/rules.d/95-backup.rules
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", KERNEL=="sd[a-z]", NAME:="%k", SYMLINK:="backupdevice", OPTIONS="last_rule"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", ACTION=="add", KERNEL=="sd[a-z]1", SYMLINK:="backuppartition", GROUP:="users", NAME:="%k"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", ACTION=="add", KERNEL=="sd[a-z]1", RUN+="/bin/mount -t auto -o rw,noauto,async,dirsync,noexec,nodev,noatime /dev/%k /media/backup-drive"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", ACTION=="add", KERNEL=="sd[a-z]1", RUN+="/usr/local/bin/backup-thumb.sh", OPTIONS="last_rule"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="XXXX", ATTRS(idProduct)=="YYYY", ACTION=="remove", KERNEL=="sd[a-z]1", RUN+="/bin/umount -l /media/backup-drive", OPTIONS="last_rule"

and here my backup-script in /usr/local/bin/backup-thumb.sh:

#!/bin/bash
sleep 5
rsync -rtolg --exclude '/whateverfile' --delete /home/myhome/ /media/backup-drive/backup/
su - username -c "DISPLAY=:0.0 gdialog --msgbox 'Backup finished'"
## su - username -c "DISPLAY=:0.0 xmessage -center Backup finished"    #alternative for non-GNOME-users#

Works great without any 'xhost +localhost' or 'SSH'.

Thx for your help smile

Last edited by mehldutt (2007-05-11 05:18:26)

Offline

#6 2007-05-11 10:24:25

lanrat
Member
From: Poland
Registered: 2003-10-28
Posts: 1,274

Re: [SOLVED] automatic usb-backup with udev-rules + script

Great. I'm not sure if the last rule (remove event) will ever really work because when you unplug the device idVendor and idProduct might (it's hard to say without the device) be empty variables at this moment. Alternatively you could remove this rule and simply place umount -l /media/backup-drive command (maybe with additional sync command before it) in your backup script or just umount it "manually" (or use a yes/no dialog to choose when the backup is finished).

Anyway, it's just an observation. If it works don't fix it :-)

EDIT: Since, you say it's working for you maybe you should place this in the wiki...

Last edited by lanrat (2007-05-11 10:25:52)

Offline

#7 2007-05-11 22:02:49

mehldutt
Member
From: Germany
Registered: 2006-01-08
Posts: 128

Re: [SOLVED] automatic usb-backup with udev-rules + script

You're right - the last rule is ineffective.
I changed my script accordingly.

Last edited by mehldutt (2007-05-12 08:16:32)

Offline

#8 2007-06-02 00:24:31

mehldutt
Member
From: Germany
Registered: 2006-01-08
Posts: 128

Re: [SOLVED] automatic usb-backup with udev-rules + script

in the meantime I added  terminal, verbose and incremental backup:

#!/bin/sh
su - username -c "DISPLAY=:0.0 terminal -x rsync -avb --backup-dir=/media/backup-drive/backup-archive/`date +%Y-%m-%d` --progress --delete --exclude /whateverfile/ /media/backup-drive/backup/"
su - username -c "DISPLAY=:0.0 gdialog --msgbox 'Backup finished'"

Last edited by mehldutt (2007-06-02 09:23:38)

Offline

#9 2007-06-30 09:06:54

mehldutt
Member
From: Germany
Registered: 2006-01-08
Posts: 128

Re: [SOLVED] automatic usb-backup with udev-rules + script

Unfortunately, this backup-rule is not as specific to a particular usb-drive as I thought, because "/usr/local/bin/backup-thumb.sh" is initiated no mater what usb-drive is connected.

Here's my usb-rule:

/etc/udev/rules.d/95-backup.rules

SUBSYSTEMS=="usb", ATTRS(idVendor)=="04cf", ATTRS(idProduct)=="8818", KERNEL=="sd[a-z]", NAME:="%k", SYMLINK:="backupdevice", OPTIONS="last_rule"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="04cf", ATTRS(idProduct)=="8818", ACTION=="add", KERNEL=="sd[a-z]1", SYMLINK:="backuppartition", GROUP:="users", NAME:="%k"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="04cf", ATTRS(idProduct)=="8818", ACTION=="add", KERNEL=="sd[a-z]1", RUN+="/bin/mount -t auto -o rw,noauto,async,dirsync,noexec,nodev,noatime /dev/%k /media/backup-drive"
SUBSYSTEMS=="usb", ATTRS(idVendor)=="04cf", ATTRS(idProduct)=="8818", ACTION=="add", KERNEL=="sd[a-z]1", RUN+="/usr/local/bin/backup-thumb.sh", OPTIONS="last_rule"
##SUBSYSTEMS=="usb", ATTRS(idVendor)=="04cf", ATTRS(idProduct)=="8818", ACTION=="remove", KERNEL=="sd[a-z]1", RUN+="/bin/umount -l /media/backup-drive", OPTIONS="last_rule"

Last edited by mehldutt (2007-06-30 09:22:30)

Offline

#10 2007-07-03 20:27:12

mehldutt
Member
From: Germany
Registered: 2006-01-08
Posts: 128

Re: [SOLVED] automatic usb-backup with udev-rules + script

I figured it out myself:

I should have used { } instead of ( ) in my backup.rules

So, here's my corrected usb-rule:

/etc/udev/rules.d/95-backup.rules

SUBSYSTEMS=="usb", ATTRS{idVendor}=="04cf", ATTRS{idProduct}=="8818", KERNEL=="sd[a-z]", NAME:="%k", SYMLINK:="backupdevice", OPTIONS="last_rule"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="04cf", ATTRS{idProduct}=="8818", ACTION=="add", KERNEL=="sd[a-z]1", SYMLINK:="backuppartition", GROUP:="users", NAME:="%k"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="04cf", ATTRS{idProduct}=="8818", ACTION=="add", KERNEL=="sd[a-z]1", RUN+="/bin/mount -t auto -o rw,noauto,async,dirsync,noexec,nodev,noatime /dev/%k /media/backup-drive"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="04cf", ATTRS{idProduct}=="8818", ACTION=="add", KERNEL=="sd[a-z]1", RUN+="/usr/local/bin/backup-thumb.sh", OPTIONS="last_rule"
##SUBSYSTEMS=="usb", ATTRS{idVendor}=="04cf", ATTRS{idProduct}=="8818", ACTION=="remove", KERNEL=="sd[a-z]1", RUN+="/bin/umount -l /media/backup-drive", OPTIONS="last_rule"

Offline

#11 2007-07-03 23:20:52

lanrat
Member
From: Poland
Registered: 2003-10-28
Posts: 1,274

Re: [SOLVED] automatic usb-backup with udev-rules + script

Sorry for not answering ealier but I was very busy. You're right of course - that's because I wasn't testing these rules before with ATTRS :-) It's always good to check udev man page first ;-)

Offline

Board footer

Powered by FluxBB