udev rule to auto-backup USB drive not running full script

67 views Asked by At

I have a project in Linux to trigger scripts to run actions on a newly inserted USB drive. (Udev hook)

One script I want to run is a backup that copies the USB drive contents using a C program. The current script (triggered by udev rule):

#!/bin/bash
sleep 5

backuplocation=$(echo /home/student/backuplocation)
mountlocation=$(echo /home/student/mountlocation)

sudo mkdir -p /home/student/backuplocation
sudo mkdir -p /home/student/mountlocation
sudo mount /dev/sdb1 /home/student/mountlocation

sleep 5  
sudo ./backup /home/student/mountlocation /home/student/backuplocation
sleep 10
sudo umount /home/student/mountlocation

The udev rule:

SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="058f", RUN+="/usr/local/bin/trigger.sh" 

When running the script line-by-line, the backup works. But when inserting the USB drive, it only creates the directories with mkdir and does not run the "./backup" executable to copy contents.

Second problem, how can I automate this process fully without hardcoding the USB mount location? (currently uses default of /dev/sdb1). I need the location of the newly added USB without hardcoding it.

If i execute it line by line it works, but if i just connect the USB it just creates the 2 folders "backuplocation" and "mountlocation", without having something in them (empty).

0

There are 0 answers