Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 41

Thread: Running backup script with udev

  1. #11
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Running backup script with udev

    Okay, I just did a test and managed to successfully autobackup a directory on to an NTFS partition on a USB flash drive.

    This is what I did:

    I made a file called
    Code:
    /etc/udev/rules.d/81-local.rules
    I used 81 because /etc/udev/rules.d/README says
    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)
    This is the contents of /etc/udev/rules.d/81-local.rules
    Code:
    SUBSYSTEM=="block", SUBSYSTEMS=="usb", KERNEL=="sd?1", RUN+="/usr/local/bin/test.sh"
    and this is the contents of /usr/local/bin/test.sh:
    Code:
    #!/bin/sh
    mount /dev/sdb1 /mnt
    rsync -a /home/user/test/ /mnt/test/
    One reason why your script was not working is because /dev/sdb1 had not been mounted anywhere at the moment sbackupd was being run. We can use udev, but we need to mount the partition manually first.

    I'm not familiar with how sbackupd works. Are you willing to try rsync? I like rsync.

    Also, let's start by making the script as simple as possible -- comment out the notify-send commands.

    Good luck with the chkdisk. Once you are done with that, it looks like it should be quite easy to setup autobackup using udev.

    Edit: Oh -- I'm very glad you got the drive to work again!
    Last edited by unutbu; March 12th, 2009 at 09:38 PM.

  2. #12
    Join Date
    Feb 2009
    Location
    Belgium
    Beans
    299
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Running backup script with udev

    Hey,

    I need to go now, I'll check the thread again tomorow.

    Thanks for all your help.

    If anybody else knows what the problem could be, don't hesitate

  3. #13
    Join Date
    Feb 2009
    Location
    Belgium
    Beans
    299
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Running backup script with udev

    Hey,

    Thanks for doing this for me!

    I moved my rules to /etc/udev/rules.d/81-local.rules
    Code:
    SUBSYSTEMS=="usb", KERNEL=="sd?1", ATTRS{product}=="Freecom Network Drive", NAME="freecomHD", RUN+="/usr/bin/usb_backup.sh"
    My script:
    Code:
    #!/bin/sh  
    mount /dev/sdb1 /mnt           
    # /usr/bin/notify-send -t 0 "Backup Message" "Backup device detected, starting backup"
    # sleep 5
    # sbackupd
    # /usr/bin/notify-send -t 0 "Backup Message" "Your USB Backup has completed."
    env > /mnt/env.out
    Notice that I changed from bash to sh. Does this change something? (I remember bash from college)

    What happens: The drive is still being mounted on /media/FREECOM HDD (where this name comes from, I don't know). And a file is created in /mnt/env.out, but the drive isn't mounted there.

    EDIT: The mount failed because I gave the name "freecomHD" to the device in udev. Doing mount /dev/freecomHD fixed it.

    I'm not familiar with how sbackupd works. Are you willing to try rsync? I like rsync.
    I would follow you through hell! I just picked out sbackup, because I didn't like timevault. I like the way you can go back in time, but I don't know if that is so usefull.

    Nothing ever seems to go according to plan in my world.

    Thanks!
    Last edited by Rackstar; March 13th, 2009 at 12:28 PM. Reason: Bug fix

  4. #14
    Join Date
    Feb 2009
    Location
    Belgium
    Beans
    299
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Running backup script with udev

    Hey!

    The good news is, that it now backups when I plug it in. Which was the main purpose.

    Now some annoying little things that show up:

    When I plug it in, it shows a media icon in the panel (using gnome). It then backups. And then another usb-device icon appears. The sad part is, I can't unmount the media. I'm only able to unmount it doing

    Code:
    sudo umount /media/freecom
    Which isn't that bad, but I really want this to be perfect

    Also the notifications don't show. Probably because the script is run by root.

    Doing
    Code:
    /usr/bin/notify-send -t 0 "Backup Message" "Backup"
    works, but doing

    Code:
    sudo /usr/bin/notify-send -t 0 "Backup Message" "Backup"
    doesn't work.

    Maybe there is some parameter, I'll have a look around.

    Thanks!

  5. #15
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Running backup script with udev

    Hey, I think we are making progress!

    To get notify-send working:

    Save this gnome-hack script
    to /usr/local/bin/alt-notify-send

    Make it executable:
    Code:
    chmod 755 /usr/local/bin/alt-notify-send
    Then alter your script to call alt-notify-send like this:
    Code:
    su USER alt-notify-send "Backup Message" "Backup" 0
    Change USER to your username

    I've tried this script and got it to work when I plugged in a USB flash drive.

    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.

    To fix the umount problem, please post your script.
    Last edited by unutbu; March 13th, 2009 at 02:02 PM.

  6. #16
    Join Date
    Feb 2009
    Location
    Belgium
    Beans
    299
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Running backup script with udev

    I also found a variant of this script, but didn't got it working. I tried yours, but it didn't also do a thing.

    This is the hack you linked to. I also tried changing whoami to my username, but then it kept saying, ruben: not found.

    Code:
    ruben@ruben-laptop:~$ alt-notify-send "test"
    /usr/local/bin/alt-notify-send: 2: ruben: not found
    pgrep: invalid user name: gnome-session
    EDIT: Hahaha, Now I get it who-am-i, not that whoami was the username. Sorry And the quotes were command substitutions, that's why `ruben` didn't work.

    Code:
    #!/bin/sh
    user=`whoami`
    pids=`pgrep -u $user gnome-session`
    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
    If I run this separately, it gives no output, and no popup.

    And this is my script.
    Code:
    #!/bin/bash
    
    mount /dev/freecomHD /media/freecom/
    su ruben alt-notify-send "Backup Message" "backup" 0                          
    sleep 5
    # sbackupd
    su ruben alt-notify-send "Backup Message" "Your USB Backup has completed." 0
    We're so close! I really think this will be usefull to other people to.

    Thank you very much for helping me get this far!

    EDIT: a little more information about the unmount bug:
    http://www.ninetynine.be/screens/e73...df4236e3f3.png
    It's in dutch, it says: "Could not unmount, the device was probably mounted manually in terminal". Rest is in english.
    Last edited by Rackstar; March 13th, 2009 at 02:25 PM.

  7. #17
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Running backup script with udev

    After getting the "Could not unmount" error window, go to the terminal and type
    Code:
    mount
    please post the output.

    Now, regarding alt-notify-send: what is the output of these two commands when run from the terminal:
    Code:
    ps axuw | grep gnome-session
    pgrep gnome-session

  8. #18
    Join Date
    Feb 2009
    Location
    Belgium
    Beans
    299
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Running backup script with udev

    Not always getting error message, sometimes it just does nothing, like now.

    Got two icons on my gnome panel.
    -------------------
    | usb device icon |
    -------------------
    options:
    mount FREECOM HDD

    --------------
    | drive icon |
    --------------
    options:
    Open freecom
    unmount freecom

    Except Open, all of the options don't do anything.

    mount output:
    Code:
    ruben@ruben-laptop:~$ mount
    /dev/sda6 on / type ext3 (rw,relatime,errors=remount-ro)
    tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
    /proc on /proc type proc (rw,noexec,nosuid,nodev)
    sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
    varrun on /var/run type tmpfs (rw,nosuid,mode=0755)
    varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
    udev on /dev type tmpfs (rw,mode=0755)
    tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
    devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
    fusectl on /sys/fs/fuse/connections type fusectl (rw)
    lrm on /lib/modules/2.6.27-11-generic/volatile type tmpfs (rw,mode=755)
    /dev/sda4 on /home type ext3 (rw,relatime)
    securityfs on /sys/kernel/security type securityfs (rw)
    binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
    gvfs-fuse-daemon on /home/ruben/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=ruben)
    /dev/freecomHD on /media/freecom type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096)
    First command:
    Code:
    ruben@ruben-laptop:~$ ps axuw | grep gnome-session
    ruben     5818  0.0  0.1  14172  3748 ?        S    14:33   0:00 /usr/lib/gnome-session/helpers/gnome-keyring-daemon-wrapper
    ruben     7249  0.0  0.0   3256   816 pts/0    S+   14:52   0:00 grep gnome-session
    Second command:
    Code:
    ruben@ruben-laptop:~$ pgrep gnome-session
    ruben@ruben-laptop:~$
    The second command didn't output anything, so probably there's something wrong here.

    EDIT: sorry, got to go, will be back in about 2 hours.
    Last edited by Rackstar; March 13th, 2009 at 02:55 PM.

  9. #19
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Running backup script with udev

    The second command didn't output anything, so probably there's something wrong here.
    Yes. This is why alt-notify-send is not working.

    Since you are running a gnome-panel, let's try this:

    Edit /usr/bin/local/alt-notify-send by changing
    Code:
    pids=`pgrep -u $user gnome-session`
    to
    Code:
    pids=`pgrep -u $user gnome-panel`
    If that does not work, please post the output of
    Code:
    pstree
    Regarding the umount problem: let's try adding this line to the end of the backup script:
    Code:
    umount /dev/freecomHD

  10. #20
    Join Date
    Feb 2009
    Location
    Belgium
    Beans
    299
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Running backup script with udev

    Haha!

    I could kiss you!

    Everything works!

    It has a very handy side-effect. When plugging in the drive icon appears on my gnome-panel, and I still can't unmount it using the button "Unmount", but probably only in terminal.

    But because of adding "umount /dev/freecomHD" to the script, the drive icon disappears, and the usb-device icon appears, which I can unmount.

    So basically, I can't unmount it while doing the backup, which is good

    Yesssss, backupping never has been so much fun.

    For reference:
    /etc/udev/rules.d/81-local.rules:
    Code:
    SUBSYSTEMS=="usb", KERNEL=="sd?1", ATTRS{product}=="Freecom Network Drive", NAME="freecomHD", RUN+="/usr/bin/usb_backup.sh"
    /usr/bin/usb_backup.sh
    Code:
    #!/bin/bash
    
    mount /dev/freecomHD /media/freecom/
    su ruben alt-notify-send "Backup Message" "USB Backup device detected" 0
    sleep 5
    sbackupd
    su ruben alt-notify-send "Backup Message" "Your USB Backup is completed." 0
    umount /dev/freecomHD
    /usr/local/bin/alt-notify-send
    Code:
    #!/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
    Thanks!

    This makes this thread solved, I guess.

Page 2 of 5 FirstFirst 1234 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •