Ensuring that rsync doesn't try to write to an unmounted drive

Kevin Korb kmk at sanitarium.net
Tue Sep 11 18:19:03 UTC 2018

Since the backup dir is within the mountpoint your existing check:
if [ ! -d ${BACKUP_DIR} ];then
    echo "Backup destination directory ${BACKUP_DIR} not exist."
        echo "run 'sudo mkdir ${BACKUP_DIR}' to create. "
    exit 1
Will work fine.  If the disk isn't mounted then the backup dir won't
exist (make sure you never mkdir it there) and the script will exit
before even trying to run rsync.  You might want to update your error
message to reflect the fact that "Backup disk not mounted" is probably
what is wrong rather than a need to sudo mkdir.

I would suggest adding --itemize-changes as -v isn't very useful without it.

I would also suggest looking into rsync's --link-dest as that is the
real power of rsync based backups.

On 09/11/2018 02:06 PM, Chris via rsync wrote:
> On Tue, 2018-09-11 at 12:56 -0400, Kevin Korb via rsync wrote:
>> --timeout is about network connection timeouts.  You aren't using the
>> network so it doesn't apply at all.  Even if you were networking an
>> unmounted filesystem is an empty directory as far as rsync is
>> concerned
>> and rsync would treat it that way with no idea that you intended to
>> have
>> something mounted there.
> Thanks Kevin, didn't realize that. Missed that when reading about --
> timeout.
>> Now, I see at the top of your script you check for the existence of
>> the
>> target directory.  If that isn't the root of a filesystem then you
>> are
>> good because you are already checking for that.  If it is the root of
>> the filesystem then it will exist either as an empty directory or a
>> mount point and you need to check for those possibilities.
> I hope I'm answering your question. The mount info on the drive is
> /dev/sdb1 on /media/chris/backup2 type ext4
> (rw,nosuid,nodev,relatime,data=ordered,uhelper=udisks2)
> However, in the script it's actually checking for the folder the backup
> is written to 'snapshot'
> BACKUP_DIR="/media/chris/backup2/snapshot/"
> if [ ! -d ${BACKUP_DIR} ];then
>     echo "Backup destination directory ${BACKUP_DIR} not exist."
>         echo "run 'sudo mkdir ${BACKUP_DIR}' to create. "
>     exit 1 
> So if I understand the script is checking for the 'snapshot' folder on
> the mount point 'backup2'. Is that correct? If rsync can't see the
> 'snapshot' folder would it still turn around and write or attempt to
> write the backup to the hard drive or would it gracefully fail?
> I had asked a question on LQ.org about the script earlier because I
> couldn't see why it wasn't making a log file. Note-this script was
> written by someone here on the list for me about 4 or so years ago.
> Yes, it took me that long to notice. One of the replies to my question
> suggested that I add this to the script:
> mount_point='/media/chris/backup2'
> df -h | grep $mount_point > /dev/null
> if [ $? -eq 0 ]
> then
>   rsync.........
> echo "mount point $mount_point exists, rsync started"
> else
>     echo "Error: mount point $mount_point does not exist, rsync
> operation skipped"
> So, would it be ok to leave it as is or do I need to add the above to
> check for the actual mount point?
>> On 09/11/2018 12:28 PM, Chris via rsync wrote:
>>> I have a script that runs nightly as a cronjob to backup my drive
>>> to a
>>> USB drive https://pastebin.com/yivqrGUC On the command line I use
>>> the
>>> --timeout option. Is this sufficient to ensure that if the external
>>> drive somehow becomes unmounted that rsync will gracefully fail
>>> without
>>> trying to write to the hard drive instead of the USB drive?
>>> rsync -vaWSHpl --timeout=15 --delete-excluded --filter "merge
>>> ${EXC_FILE}" / "${BACKUP_DIR}" > /home/chris/rsyncbackup.log 2>
>>> /home/chris/rsyncbackup.errors.log 
>>> If this is sufficient or would it be better if I lowered the
>>> 'timeout'
>>> to 5 seconds?
>>> Thanks for any suggestions/advice.
>>> Chris

	Kevin Korb			Phone:    (407) 252-6853
	Systems Administrator		Internet:
	FutureQuest, Inc.		Kevin at FutureQuest.net  (work)
	Orlando, Florida		kmk at sanitarium.net (personal)
	Web page:			https://sanitarium.net/
	PGP public key available on web site.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: OpenPGP digital signature
URL: <http://lists.samba.org/pipermail/rsync/attachments/20180911/7a15db8c/signature.sig>

More information about the rsync mailing list