Hello friends
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.
Regards and thank you all in advance Kshitiz
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
-------------------------------------------------- From: "Raj Mathur (??? ?????)" raju@linux-delhi.org
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.
Assuming your USB device is /dev/hdb1 and it gets mounted on /media/hdb1, you can try the following:
- Add an entry in /etc/fstab:
/dev/hdb1 /media/hdb1 auto user 0 0
- 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
Dear Raj & rest of my linuxer friends
Thank you for your efforts and help. I am pasting my code which gives me syntax error unexpected end on line 17. Need help with why and correction? Am missing something and cant figure out what?
Urgent help will be really really really appreciated.
-------------
if ! mount -t ntfs | grep /dev/sdc1 | grep /backup > /dev/null then mount /dev/sdc1 fi if mount -t ntfs | grep /dev/sdc1 | grep /backup > /dev/null then #is a previous rsync process still running? ps -ef | grep 'rsync' | grep -v 'grep rsync'| grep -v 'rsync.sh'> /dev/null if [ $? -eq 1 ]; then #rsync isn't running - let's launch it now rsync -avz -e --delete /home/ /backup else echo "rsync is still running ...please wait and try again later" fi fi --------------
Regards and thanks in advance. Kshitiz
2010/12/2 Kshitiz kshitij_kotak@hotmail.com:
Thank you for your efforts and help. I am pasting my code which gives me syntax error unexpected end on line 17. Need help with why and correction? Am missing something and cant figure out what?
Quite obviously. The right way to ask for help debugging shellscripts is to run it with "bash -x script.sh", copy/paste the entire output to pastebin.com or something and then post a link here.
ps -ef | grep 'rsync' | grep -v 'grep rsync'| grep -v 'rsync.sh'> /dev/null
Wtf is all this?? Haven't heard of pidof(1)?
Even if you don't have pidof, you could do:
ps -ef | grep '[r]sync'
and get rid of that pipeline of greps.
Binand
On Thursday 02 December 2010 09:33 PM, Kshitiz wrote:
if ! mount -t ntfs | grep /dev/sdc1 | grep /backup> /dev/null then mount /dev/sdc1 fi if mount -t ntfs | grep /dev/sdc1 | grep /backup> /dev/null then
Instead of the above 'grep bhai grep' lines simply do an
if grep -q "/mount_point" /etc/mtab
then echo "The device is mounted"
else
mount /mount_point # Whatever you defined in /etc/fstab for your backup device.
# Now continue further with the remaining rsync script again using whatever Binad said in the other mail.
if
ps -ef | grep '[r]sync'
then
# whatever.
if [ $? -eq 1 ]; then #rsync isn't running - let's launch it now rsync -avz -e --delete /home/ /backup
rsync --delete /home /backup?
If someone has deleted a file from the source dir, you are making sure that the available backup is also deleted? Not a very pleasant situation.
Assuming that your script is for usb devices and runs automatically, is there any protection to ensure that someone does not plug in a personal disk and walk away with the data so neatly backed up?
2010/11/22 Kshitiz kshitij_kotak@hotmail.com:
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.
All mounted filesystems have an entry in /proc/mounts. Your script can take advantage of this.
if ! grep -q /backup /proc/mounts; then if ! mount -t ext3 /dev/sdb1 /backup; then exit 1; # Mount failed, bail out fi fi
# Regular stuff here
On Mon, Nov 22, 2010 at 7:49 AM, Binand Sethumadhavan binand@gmail.com wrote:
All mounted filesystems have an entry in /proc/mounts. Your script can take advantage of this.
if ! grep -q /backup /proc/mounts; then if ! mount -t ext3 /dev/sdb1 /backup; then exit 1; # Mount failed, bail out fi fi
another non-portable way of checking it is df -l.
Regards, NMK.
-------------------------------------------------- From: "Nadeem M. Khan" nadeem.m.khan@gmail.com Sent: Monday, November 22, 2010 9:47 AM To: "GNU/Linux Users Group, Mumbai, India" linuxers@mm.ilug-bom.org.in Subject: Re: [ILUG-BOM] running script only if USB is mounted
On Mon, Nov 22, 2010 at 7:49 AM, Binand Sethumadhavan binand@gmail.com wrote:
All mounted filesystems have an entry in /proc/mounts. Your script can take advantage of this.
if ! grep -q /backup /proc/mounts; then if ! mount -t ext3 /dev/sdb1 /backup; then exit 1; # Mount failed, bail out fi fi
another non-portable way of checking it is df -l.
Regards, NMK. -- http://mm.glug-bom.org/mailman/listinfo/linuxers
Thank you Nadeem, will check out this script and revert in case of it not working, etc.
Regards Kshitiz
-------------------------------------------------- From: "Binand Sethumadhavan" binand@gmail.com Sent: Monday, November 22, 2010 9:19 AM To: "GNU/Linux Users Group, Mumbai, India" linuxers@mm.ilug-bom.org.in Subject: Re: [ILUG-BOM] running script only if USB is mounted
2010/11/22 Kshitiz kshitij_kotak@hotmail.com:
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.
All mounted filesystems have an entry in /proc/mounts. Your script can take advantage of this.
if ! grep -q /backup /proc/mounts; then if ! mount -t ext3 /dev/sdb1 /backup; then exit 1; # Mount failed, bail out fi fi
# Regular stuff here
Thank you Binand Reg Kshitiz