4

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?

iam4k33m
  • 57
  • 2

1 Answers1

1

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
L. D. James
  • 24,768
  • 10
  • 68
  • 116
  • 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 R Jan 25 '17 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`. – L. D. James Jan 25 '17 at 17:44