Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

This question already has an answer here:

I have an Ubuntu server where I'm automounting an external hard drive each boot.

To do this, I've created an empty folder on the root partition, and the drive gets mounted "inside" this folder.

However, what if I perform a backup to this path when the drive isn't properly mounted? The backup would instead fill up my root partition!

I can ensure that the drive is mounted each time by performing:

sudo mount -a

... before each backup.

However, what are the best practices to ensure that data is never written to the empty mount-folder (except when the external hard drive is truly mounted)?

Can this be solved without scripting? Say with permissions for example? What are the best practices?

share|improve this question

marked as duplicate by Iain, mdpc, Jenny D, Michael Hampton Jan 28 at 23:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
Use the mountpoint command serverfault.com/questions/436048/… –  Iain Jan 27 at 21:28

2 Answers 2

up vote 8 down vote accepted

I go a step further and always set the attributes of my mountpoint directories to immutable using chattr.

This is accomplished with chattr +i /mountpoint (with the mount unmounted).

This would error-out on new write activity and also protects the mount point in other situations.

But I suppose you could use the mountpoint command, too...

share|improve this answer
1  
This is a better solution; it requires no scripting to ensure nothing ever gets written to the root partition. To me, this seems like a best practice that should be done for all folders that will be exclusively used for mounting. –  LonnieBest Jan 27 at 22:33
1  
@LonnieBest Thank you. I strive to make sure any removable or NFS/CIFS or major data partitions are mounted this way. –  ewwhite Jan 27 at 22:35
1  
That is even better - I like it! –  fukawi2 Jan 28 at 2:22

To expand on the comment about using mountpoint, this is roughly what I put into scripts when I need to check these kind of things:

DEST='/mnt/backup'
if ! mountpoint -q "$DEST" ; then
    echo "Destination is not mounted; attempting to mount"
    mount $DEST
    if ! mountpoint -q "$DEST" ; then
        echo "Unable to mount $DEST; Aborting"
        exit 1
    fi
    echo "Mounted $DEST; Continuing backup"
fi

This assumes that $DEST exists in /etc/fstab; it doesn't matter if it is an auto or noauto mountpoint.

As per the mount man page:

If only directory or device is given, for example:

mount /dir

then mount looks for a mountpoint and if not found then for a device in the /etc/fstab file. It's possible to use --tar‐get or --source options to avoid ambivalent interpretation of the given argument. For example

mount --target /mountpoint
share|improve this answer
    
Is $DEST the path-location to the (potentially empty) mount-folder? How does the command "mount $DEST" know what to mount there? Could you edit this post to include an example value for the variable $DEST? –  LonnieBest Jan 27 at 22:24
1  
@LonnieBest Done. –  fukawi2 Jan 28 at 2:21
    
Thanks man. I wonder how the mount command knows which device is associated with /mnt/backup folder. Does it cross-reference with the fstab? –  LonnieBest Jan 28 at 3:59
1  
@LonnieBest Yes, if you only provide 1 argument to mount it will consult /etc/fstab to determine the rest of the information. I'll update the answer with an excerpt from the man page. –  fukawi2 Jan 28 at 4:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.