Udev hook to backup USB content not working, but script working

24 views Asked by At

I've included a rule script in /etc/udev/rules.d to automate backup my USB content in a folder when i plug it in.

student@student-VirtualBox:/etc/udev/rules.d$ cat 80-local.rules 
SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", RUN+="/home/student/psop/triggertools/bin/principal.sh" 

Script principal.sh:

#!/bin/bash

log_file="/home/student/psop/logs"

first_script="/home/student/psop/triggertools/bin/trigger.sh"

echo Starting scrip ... > "$log_file"

bash "$first_script"

Script trigger.sh:

#!/bin/bash

log_file="/home/student/psop/logs"
backup_dir="/home/student/psop/backupdir"
mount_dir="/home/student/psop/mountdir"
usb_device="/dev/sdb1"


if [ ! -d "$backup_dir" ]
then
    mkdir -p "$backup_dir"
fi

sudo systemd-mount --no-block --automount=no --collect "$usb_device" "$mount_dir" >> "$log_file" 2>&1
sleep 5

echo "Continut folder de montare: " >> "$log_file"
ls "$mount_dir" >> "$log_file"

if [ $? -eq 0 ]
then
    sudo /usr/local/bin/backup "$mount_dir" "$backup_dir" >> "$log_file" 2>&1
    sleep 5
    sudo umount "$usb_device" >> "$log_file"
else
    echo "Eroare la montarea dispozitivului!" >> "$log_file" 2>&1
fi

If I type the script line by line in bash terminal the mount / backup will work fine and if i type bash /home/student/psop/triggertools/bin/principal.sh works fine too. But if i just plug the stick in it won't work fine, the mount will eventually fail and the copy won't occur, but it just creates the folders with mkdir. What i'm missing?

0

There are 0 answers