Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'd like to know if it's possible to automatically mount, and fire up rsync to sync a USB drive with a directory? Specifically, I'd like to copy as much data as the drive can hold and only delete the oldest files if space is needed. I would assume I'd do something like this with a script, but my problem, is where to start! Any comments and suggestions appreciated.

Thank You

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You are probably going to want to look into a Udev rule to launch a script. What's good about udev rules is you can lock down what usb drive is used for backup so not every usb drive fires it off.

First step is to get your udev rule going:

Here is a great tutorial on writing udev rules
and here is another

Then you can move on to your scripting aspect. The first link covers a bit of launching the script from udev so then you just need to call rsync from it.

man:rsync has a lot of information on usage.

share|improve this answer
    
One more link i was about to post: wiki.archlinux.org/index.php/Udev The Tips&Tricks section has interesting examples. –  matthias krull Feb 17 '11 at 13:47
    
@mugen kenichi - You have to love the Arch documentation. –  Dan M. Feb 17 '11 at 13:51

Here's the guts of a backup job that checks for a mounted USB-based hard disk. It would be easy to modify it for other purposes and then keep calling it via cron or perhaps run it in the background with a sleep loop around the key parts. This script lets you specify where the usb disk is likely to be found (sdd), but it would be quite easy to modify it to look for a USB stick by drive ID.

The source of the backup is an NFS mounted drive but obviously that can be changed/removed as needed.

There is a for loop in the middle that specifies the locations to be backed up

 # V1.01
 # Uses variable for usbdisk partition

started=`date`
usbdisk='sdd1'

if [ $(mount | grep -c /mnt/usbdisk) != 1 ]; then
 echo "USB disk is not mounted - trying to mount..."
 mount /dev/$usbdisk /mnt/usbdisk
 if [ $(mount | grep -c /mnt/usbdisk) != 1 ]; then
   echo "USB disk still not mounted - alerting admins and quitting"
   echo "USB disk is not mounted on TUX9 so backup cannot continue" | mail -s "TUX9 backup problem" myemailaddress@here.tld
   exit 1
 fi
fi

if [ $(mount | grep -c /mnt/nas) != 1 ]; then
  echo "NAS NFS mount is not present - trying to mount..."
  mount -t nfs -o proto=tcp,port=2049 mynas3:nas /mnt/nas
  if [ $(mount | grep -c /mnt/nas) != 1 ]; then
    echo "NAS NFS mount is still not present - alerting admins and quitting"
    echo "NAS NFS mount is not present on TUX9 so backup cannot continue" | mail -s "ASLTUX9 backup problem" myemailaddress@here.tld
    exit 1
  fi
fi

echo "All mount points present - backup starting..."

for i in /boot / /home /var /mnt/nas
# for testing for i in /boot
do
        if [ $i = "/" ]; then
                rsync -axv --delete root@tux8.test.lan:$i /mnt/usbdisk/backup/root
        elif [ $i = "/mnt/nas" ]; then
                rsync -axv --delete $i/ /mnt/usbdisk/backup/nas
        else
                rsync -axv --delete root@tux8.test.lan:$i/ /mnt/usbdisk/backup$i
        fi
done

echo done, started at $started, finished at `date`
echo "Backup of NAS3 and TUX8 to USB disk on TUX9 done. Started at $started, finished at `date`" | mail -s "TUX9 backup completed" myemailaddress@here.tld
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.