On Monday 22 Nov 2010, Kshitiz wrote:
I use rsync to copy my incremental data to an external harddisk every 5 minutes using cron. However, if the external harddisk, connected to the USB port is disconnected, the root partition fills up. I need your help to create a proper script that does the following:
Before running the rsync copying script, I want to a) Add some statement that would check if the external harddisk is attached and mounted. b) If not, it should be mounted before the copying script is run to copy only to this external disk. c) If it is not mounted, mount should be attempted, and if it does not mount, rsync copying command should not be executed.
Is there any better way to do this?
Under no circumstances, I want the rsync script to execute if the USB connection is not active.
Assuming your USB device is /dev/hdb1 and it gets mounted on /media/hdb1, you can try the following:
1. Add an entry in /etc/fstab:
/dev/hdb1 /media/hdb1 auto user 0 0
2. Make an rsync script of the form:
if ! mount | fgrep /dev/hdb1 | fgrep /media/hdb1 > /dev/null then mount /dev/hdb1 fi if mount | fgrep /dev/hdb1 | fgrep /media/hdb1 > /dev/null then rsync... else echo "USB device not mounted, aborting" >&2 exit 1 fi
This code is provided without any claim to working or warranty, and if it fails to run or causes your cat to be run over by a road roller or your wife to divorce you or bedbugs to infest your bedroom, I am not responsible.
Regards,
-- Raj