New user's registration have been closed due to high spamming and low trafic on this forum. Please contact forum admins directly if you need an account. Thanks !

Automatic backup, triggered by connecting device

A collection of tips on howto tweak your Bubba.
RandomUsername
Posts: 904
Joined: 09 Oct 2009, 18:49

Re: Automatic backup, triggered by connecting device

Post by RandomUsername »

6feet5 wrote:@RandomUsername: I'm interested in knowing if my solution didn't work so I can rewrite it
Hi Johan,

I never tried your script so I don't know if it works or not. I've no reason to think it wouldn't though.

Darren.
scott-2
Posts: 31
Joined: 05 Oct 2008, 21:25

Re: Automatic backup, triggered by connecting device

Post by scott-2 »

6feet5 wrote:Judging by the log, it's not the device being detected several times but the backup script being called several times.

Could it be that your udev rule (the one in /etc/udev/rules.d) is too general? In this case the script might be called before the device node exist. Show us the rule.

Code: Select all

SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{manufacturer}=="SanDisk Corporation", ATTRS{product}=="U3 Cruzer Micro", ATTRS{serial}=="0000187FC5707725", RUN+="/usr/local/bin/fork /usr/local/bin/usb_backup"
6feet5 wrote:Also, start 'udevmonitor' as root, then connect the device and show us the resulting output.

Code: Select all

UEVENT[1265601179.212819] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1
UDEV  [1265601179.212819] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1
UEVENT[1265601179.214185] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0
UEVENT[1265601179.214842] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7
UEVENT[1265601179.215404] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/scsi_host/host7
UEVENT[1265601179.215931] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/usb_endpoint/usbdev1.6_ep81
UEVENT[1265601179.221732] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/usb_endpoint/usbdev1.6_ep01
UEVENT[1265601179.222296] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/usb_device/usbdev1.6
UEVENT[1265601179.222821] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/usb_endpoint/usbdev1.6_ep00
UDEV  [1265601179.900576] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0
UDEV  [1265601179.967318] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/usb_device/usbdev1.6
UDEV  [1265601180.004634] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/usb_endpoint/usbdev1.6_ep00
UDEV  [1265601180.215628] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7
UDEV  [1265601180.302209] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/usb_endpoint/usbdev1.6_ep81
UDEV  [1265601180.314137] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/usb_endpoint/usbdev1.6_ep01
UDEV  [1265601180.335662] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/scsi_host/host7
UEVENT[1265601184.197502] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0
UEVENT[1265601184.198181] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0
UEVENT[1265601184.198842] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/scsi_disk/7:0:0:0
UDEV  [1265601184.245000] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0
UEVENT[1265601184.277308] change@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0
UEVENT[1265601184.330177] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/block/sdb
UEVENT[1265601184.330752] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/block/sdb/sdb1
UEVENT[1265601184.331279] add@/devices/virtual/bdi/8:16
UEVENT[1265601184.331785] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/scsi_device/7:0:0:0
UEVENT[1265601184.332320] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/scsi_generic/sg1
UDEV  [1265601184.340863] add@/devices/virtual/bdi/8:16
UDEV  [1265601184.370169] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0
UDEV  [1265601184.547640] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/scsi_disk/7:0:0:0
UDEV  [1265601184.662189] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/scsi_device/7:0:0:0
UDEV  [1265601184.719584] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/scsi_generic/sg1
UDEV  [1265601185.002848] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/block/sdb
UDEV  [1265601185.050216] change@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0
UDEV  [1265601185.182106] add@/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/block/sdb/sdb1
6feet5 wrote:While the device is connected, run

Code: Select all

udevtest /class/block/sdb
where sdb is replaced with the device name your stick get. Show us the output.

Code: Select all

parse_file: reading '/etc/udev/rules.d/010_backup.rules' as rules file
parse_file: reading '/etc/udev/rules.d/020_permissions.rules' as rules file
parse_file: reading '/etc/udev/rules.d/030_ifplugd.rules' as rules file
parse_file: reading '/etc/udev/rules.d/udev.rules' as rules file
parse_file: reading '/etc/udev/rules.d/z20_persistent-input.rules' as rules file
parse_file: reading '/etc/udev/rules.d/z20_persistent.rules' as rules file
parse_file: reading '/etc/udev/rules.d/z25_persistent-net.rules' as rules file
parse_file: reading '/etc/udev/rules.d/z45_persistent-net-generator.rules' as rules file
parse_file: reading '/etc/udev/rules.d/z50_run.rules' as rules file
parse_file: reading '/etc/udev/rules.d/z55_hotplug.rules' as rules file
parse_file: reading '/etc/udev/rules.d/z75_cd-aliases-generator.rules' as rules file
This program is for debugging only, it does not create any node,
or run any program specified by a RUN key. It may show incorrect results,
if rules match against subsystem specfic kernel event variables.

main: looking at device '/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/block/sdb' from subsystem 'block'
run_program: 'usb_id -x'
run_program: '/lib/udev/usb_id' (stdout) 'ID_VENDOR=SanDisk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_MODEL=U3_Cruzer_Micro'
run_program: '/lib/udev/usb_id' (stdout) 'ID_REVISION=4.05'
run_program: '/lib/udev/usb_id' (stdout) 'ID_SERIAL=SanDisk_U3_Cruzer_Micro_0000187FC5707725'
run_program: '/lib/udev/usb_id' (stdout) 'ID_TYPE=disk'
run_program: '/lib/udev/usb_id' (stdout) 'ID_BUS=usb'
run_program: '/lib/udev/usb_id' returned with status 0
run_program: 'edd_id --export /dev/.tmp-8-16'
run_program: '/lib/udev/edd_id' (stderr) 'no kernel EDD support'
run_program: '/lib/udev/edd_id' returned with status 2
run_program: 'path_id /devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/block/sdb'
run_program: '/lib/udev/path_id' (stderr) '/lib/udev/path_id: line 429: cd: /sys/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/block/subsystem: No such file or directory'
run_program: '/lib/udev/path_id' (stderr) '/lib/udev/path_id: line 73: [: [0-9]*: integer expression expected'
run_program: '/lib/udev/path_id' (stderr) '/lib/udev/path_id: line 211: 7/target7:0:0 - 7/target7:0:0: division by 0 (error token is ":0:0 - 7/target7:0:0")'
run_program: '/lib/udev/path_id' returned with status 1
udev_rules_get_name: add symlink 'disk/by-id/usb-SanDisk_U3_Cruzer_Micro_0000187FC5707725'
udev_rules_get_name: no node name set, will use kernel name 'sdb'
udev_device_event: device '/devices/platform/fsl-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0/host7/target7:0:0/7:0:0:0/block/sdb' already in database, validate currently present symlinks
udev_node_add: creating device node '/dev/sdb', major = '8', minor = '16', mode = '0660', uid = '0', gid = '25'
udev_node_add: creating symlink '/dev/disk/by-id/usb-SanDisk_U3_Cruzer_Micro_0000187FC5707725' to '../../sdb'
main: run: '/usr/local/bin/fork /usr/local/bin/usb_backup'
main: run: 'socket:/org/kernel/udev/monitor'
Thank you very much for your attention on this issue.

scott-2
6feet5
Posts: 269
Joined: 13 Apr 2007, 17:32
Location: Gnesta, Sweden
Contact:

Re: Automatic backup, triggered by connecting device

Post by 6feet5 »

I'm no expert on these matters. As far as I can tell, your output looks ok.

One thing that is a bit weird is that I can't see a line in the udevtest output where it creates the uuid node, only the id node. I made a quick test with a U3 Cruzer I have, and I notice the same thing here, no entry about creating a uuid symlink, still the node is created.

Another thing I noticed is that the flash disk takes some time to settle. Not sure what's going on, but I'm beginning to suspect the node hasn't been created yet when the script is run.

Try replacing the 'sleep 5' in the backup script with the following code:

Code: Select all

((a=0))
while [ ! -e /dev/disk/by-uuid/$UUID ]; do
    ((a++))
    if ((a>20)); then
        ### Give up after 20 seconds/retries
        logger -t usb_backup "Node hasn't been created after 20 seconds"
        exit -1
    fi
    sleep 1
done
This will wait at most 20 seconds and then give up. The script proceeds immediately if node is created sooner.

None of this explains why your script is called several times, but that is something I haven't been able to reproduce here and therefor can't test. Your log files have no explanation as far as I can tell. Anyway, the lock mechanism in the backup script should prevent the script from running several times. If not, let me know and I'll come up with a better solution.

/Johan
scott-2
Posts: 31
Joined: 05 Oct 2008, 21:25

Re: Automatic backup, triggered by connecting device

Post by scott-2 »

Thank you for your efforts. It works! Replacing "sleep 5" with the loop enables it to run correctly with my Cruzer memory stick.
6feet5
Posts: 269
Joined: 13 Apr 2007, 17:32
Location: Gnesta, Sweden
Contact:

Re: Automatic backup, triggered by connecting device

Post by 6feet5 »

I'm glad you got it working.

I have updated the original instruction since the loop is a much better solution.

/Johan
6feet5
Posts: 269
Joined: 13 Apr 2007, 17:32
Location: Gnesta, Sweden
Contact:

Re: Automatic backup, triggered by connecting device

Post by 6feet5 »

NOTE! This entry is for the B1 (BUBBA|server). It will work on a B2 as well, but the original instruction is probably better suited for the B2.

I thought I'd share the solution I'm using on my old but trusty B1 as well. It is a modified version of the one I use on my B2.

The reason the original solution doesn't work on a B1 is that this version of udev has no RUN option and therefor can't start the backup script. Upgrading udev is unfortunately not an option since this require a kernel upgrade as well and the kernel is kind of hard to upgrade (it could turn your bubba into a brick). The workaround is to let cron watch every minute for the device node to be created and then start the backup script. This instruction will guide you through the different steps. Reading the original post may help.

Install rsync or what ever backup program you want to use (the script uses rsync, change accordingly). Installing rsync or other external software require you enable the debian repository. Search this forum for instructions.

Create a configuration file, /etc/default/usb_backup and put the following in it:

Code: Select all

#
# Array of folders to backup. Separate each entry with a space and make sure to use ""
# if name contain spaces.
#
BACKUP_FOLDERS=("/etc" "/home" "/root" "/var/lib" "/var/mail" "/var/spool/cron/crontab")

#
# Should script be allowed to start when device is connected?
# 0 = start when connected; 1 = don't start when connected
#
NO_START=0

#
# At what rate should LED flash during backup. Lower value means higher frequency
#
LED_RATE=4096
Create the script, /etc/local/bin/usb_backup, and paste the following text in it:

Code: Select all

#!/bin/bash
#
# A simple backup solution for a BUBBA|server.
# Written by Johan Stenarson
#

CMD=usb_backup
CONFIG=/etc/default/$CMD

### Include user settings if there are any
test -f $CONFIG && . $CONFIG

### Test if script is already running
if [ -f /var/run/$CMD.pid ]; then
    PID="`cat /var/run/$CMD.pid`"
    for i in `ps x|grep $CMD|sed 's/^ *\([0-9]*\).*/\1/g'`; do
        if [ "$i" == "$PID" ]; then
            exit 2
        fi
    done
    # Script not running but .pid file exists, continue
fi

echo $$ >/var/run/$CMD.pid

## Does settings allow script to start
if [ "$NO_START" != "0" ]; then
    logger -t usb_backup "Not starting backup - edit $CONFIG and change NO_START to 0";
    exit 0;
fi

### Mount point for USB device during backup
MOUNT_POINT=/tmp/backup_mount_point

### A fast flashing LED will indicate progress
/usr/sbin/gpioapp blink $LED_RATE $LED_RATE

### Mount device
mkdir -p $MOUNT_POINT && mount /dev/auto_backup_device $MOUNT_POINT

for FOLDER in ${BACKUP_FOLDERS[*]}
do
    DEST=`dirname "$FOLDER"`
    mkdir -p "$MOUNT_POINT/B1_autobackup$DEST"
    rsync -a "$FOLDER" "$MOUNT_POINT/B1_autobackup$DEST"
done

### We're done, clean up
umount $MOUNT_POINT && rmdir $MOUNT_POINT

### Remove device node to prevent cron from starting us again
rm /dev/auto_backup_device

rm /var/run/$CMD.pid
/etc/init.d/led_on
The above script require the USB device has a node named '/dev/auto_backup_device', so we need to make sure udev will create one for us. To do this:
  • 1) Connect your USB device to your bubba
  • 2) Locate device in kernal log:

    Code: Select all

    tail -n 20 /var/log/kern
  • 3) With the device node from previous step, run

    Code: Select all

    udevinfo -a -p /sys/block/sdb
    (replacing 'sdb' with the device node you got). This will show a list of attributes that identifies the device. Pick a couple from same section that appears to be unique (there aren't many to choose from).
  • 4) Create a file /etc/udev/rules.d/010_backup.rules and put the attributes in it

    Code: Select all

    BUS=="scsi", SYSFS{vendor}=="Maxtor 6", SYSFS{model}=="L250R0", SYMLINK+="auto_backup_device"
    This will create a device node named '/dev/auto_backup_device' when the USB disk is connected (you will ofcourse replace the attributes with the ones you picked).
Now that we know udev will create the device node, lets make cron monitor its existance and start the backup script when it exist. Create a file named /etc/cron.d/usb_backup containing:

Code: Select all

* * * * * root test -b /dev/auto_backup_device && /usr/local/bin/usb_backup
That should do it. The script will start when you connect the disk. Note that cron verifies the existence of the node only once every minute so it may take up to one minute after you connect the device before the script actually starts. The LED will blink during the whole process and return to a solid state when backup is done.

/Johan
Post Reply