When a filesystem is mounted the mount point acquires the permissions of the root inode of that filesystem. If a filesystem is not mounted the directory acting as the mount point is just another directory on the parent filesystem.
You can use this fact to your advantage to prevent accidentally writing to a mount point which may not be mounted:
- Unmount the target filesystem.
- Change the permissions of the mountpoint to something unfriendly.
Something like chmod 0000 /path/to/mountpoint
should work nicely.
- Remount the target filesystem
The permissions on the mountpoint should change to match the permissions of the root inode of the mounted filesystem.
Note that this doesn't work so well if root
is doing the writing and the usual permissions checks are bypassed.
You may be able to do something similar with immutable
flags (schg
or uchg
on BSD systems, the i
attribute on Linux systems) but I've not tested the behavior of filesystem attributes personally. Intuitively they should work the same as filesystem permissions do though.
Note that ideally you'd want to modify your backup scripts to ensure that the appropriate filesystem is mounted, otherwise you're going to have to catch and handle the errors the solutions above will generate.
Detecting that the appropriate filesystem(s) aren't in the output of mount
may be a more robust solution.