I've got data on my HDD that needs to be backed up. Lets say that it's everything in folder a
. On a USB stick there's a copy of folder a
, that should be updated automatically whenever the stick is mounted. How can this be done automatically?
-
It would be good if there was a visible indication to the user that the copying has finished too...– Roberto TyleyMay 14, 2017 at 11:28
1 Answer
Create a udev rule for when the drive is inserted, and run your backup routine (rsync or other procedure on the device).
This is a crude example to get you started.
The rule to call your backup script (/etc/udev/rules.d/10-local.rules
):
ACTION=="add", RUN+="/bin/sh -c 'exec /home/userid/backupscript.sh & > /home/userid/Desktop/test.out'"
Replace userid
above with your userid, or place the script in a different path.
The backup script:
#!/bin/bash
templine=/tmp/line.$$
backuproutine () {
# backup rountine goes here
timestamp=$(date)
message="This is the Backup noice."
device=$(mount | egrep "sd.1")
echo -e "$timestamp:$message\n$device" > $templine
cat $templine >> /home/userid/Desktop/backupnotice.txt
}
backuproutine
rm $templine
This is a crude script, but something to get you started.
This command will provide details on how to use the udev rules.
man udev
-
Would this script attempt to backup to any new device inserted? It would seem like backing up might only be desired to a very specific USB stick (though @iam4k33m can probably clarify).– Taylor RJan 25, 2017 at 17:33
-
The purpose of the answer is to show an example to be used as a template for the user to actual program the actions. The backup function would analyze the mount and determine how to handle the results. All drives has unique identification properties. The user can manually label the partition and have the script identify it by that label or use the UUID of the partition. Put a pendrive in your computer and look at the output of
lsblk -o name,mountpoint,label,size,fstype
. Jan 25, 2017 at 17:44