For my backup , I mount and external hard disk to /mnt/mybackup and then I do an rsync to /mnt/mybackup
If for some reason the rsync fails, I want to prevent it from writing data on the server hard disk itself since the external hard disk will no longer be mounted on it.
I want /mnt/mybackup directory to be read-only and when my external device (e.g /dev/sda1) is mounted on it then I want it to be read/write-able by rsync
Could you create a subdirectory on the external drive and write to that? In this case, a write with the filesystem not mounted would get a "target not found" or equivalent message. It would mean that you external drive would have a directory and everything under that, so your recovery would have to reflect that too.
I used to use this type of check when I used external devices.
PARCHK=`df -k | grep mybackup | wc -l`
if [ ${PARCHK} -ne 1 ]
then
echo "The portable hard drive partitions are not mounted, please correct."
exit 1
else
echo "There are ${PARCHK} partitions mounted, backup starting."
fi
This should work for you if you change the messages to suit.
While the backup filesystem is unmounted, can't you do a chmod 0 /mnt/mybackup ? That should prevent accidental syncing when the backup filesystem is unmounted.
When you mount a filesystem on that directory, it should appear with the permissions of the root directory of the filesystem you have mounted (rather than the overlaid mount point directory) thereby allowing rsync to work as desired.
I thought that would make the mybackup directory inaccessible even by root.
please advise.
---------- Post updated at 11:30 AM ---------- Previous update was at 11:26 AM ----------
Thanks for the script.i already use a script to check if the external hard disk is REALLY mounted.
i created a file called "am-a-file-on-the-external-hd.txt" on the external hard disk and before backup i check if that file exists.
---------- Post updated at 11:37 AM ---------- Previous update was at 11:30 AM ----------
just for info, i had twice landed into the issue where the filesystem was 100% full. The customer forgot to plug the usb or the usb was a bit loose.
---------- Post updated at 11:57 AM ---------- Previous update was at 11:37 AM ----------
i tried running the rsync...the rsync process could write on the unmounted /mnt/mybackup directory...which is bad
Can you check with mount command then if exit code is 0 do the rsync, else don't rsync ?
Touching a file will work on both directory and mountpoint so that check is not good.
Something like
mount | grep "/your/mountpoint"
if [ $? -eq 0 ]; then
echo "will rsync"
else
echo "will not rsync, unable to find mountpoint"
fi
OK i understand..but this is before starting the backup..what if during the backup itself (assume the backup will take 3 hours) the external drive gets connected for some reason ?
If you are referring to drive being disconnected during backup, you can create something like this which will check if mountpoint is avalible every second, and if it is not it will kill the rsync proces.
set -x
mount | grep "/test" > /dev/null
if [ $? -eq 0 ]; then
echo "your rsync goes here" & RPID=$!
else
echo "will not rsync, unable to find mountpoint"
fi
e=0
while [ $e -eq 0 ]
do
mount | grep "/test" > /dev/null
if [ $? -gt 0 ]; then
echo "Cannot see mountpoint!, will abort"
e=$(( $e + 1 ))
kill $RPID
sleep 1
fi
done