I have a cron job that backs up my computer from one internal hard drive to another internal hard drive every night.
I wanted to also backup to a portable hard drive if it happens to be plugged in when that script is run.
I found a udev tutorial that makes the portable hard drive appear as
/dev/500gb-USB-drive using its serial number as an identification to do it. That works great.
So, I figured if that device exists, go ahead and run the extra backup step. Here's the relevant part of the backup script:
Quote:
ls /dev/500*
if [ $? -eq 0 ]
then
mount /dev/500gb-USB-Drive
echo "********* Start USB Backup" >> rsync-log
echo "Result of ls/dev/500*" >> rsync-log
ls /dev/500* >> rsync-log
rsync -av /media/500gb /mnt/500gb-usb/ >> rsync-log
else
echo "********* USB Drive not sensed" >> rsync-log
fi
|
The idea is that the ls command should exit with a "1" if it doesn't see anything that starts with /dev/500. If it does see something that starts with that string then it exits with "0" and so the if statement runs (the drive is then mounted and rsynced to)
This script usually works. Every now and then (perhaps every 5th night) it doesn't though and it decides to try to mount the drive anyway (which fails) and then runs the rsync to the /mnt/500gb-usb folder. This results with the unmounted folder filling up because the root partition isn't that big.
Can anyone tell me why that if statement would still sometimes execute even if the portable drive isn't plugged in?
If not, is there a better way to do what I'm trying to do? (Only rsync if the drive is plugged in and mounted.)
In thinking this over, perhaps I can pipe the output to the mount command to grep and search for /mnt/500gb-usb. I think I'll try that next unless someone else has a better idea here.
Thanks!