Score:2

How can I have an rsync backup script do the backup only when the external drive is mounted?

bd flag

I have an external SSD. Sometimes it's plugged in, sometimes not. I've gotten past the FSTAB issues of hanging on boot - all is good there. I have a script that uses RSYNC to back up my home drive to said drive.

  • When the drive is connected - works flawlessly.
  • When the drive is NOT connected, Fstab creates the mount point, and RSYNC backs up to the local drive.

I don't want that. I either want the mount point to NOT be created (if the drive isn't there), or to have RSYNC ONLY backup to the drive - not the mount point.

Since the mount point is technically mounted locally - I can't figure out how RSYNC would know the difference.

guiverc avatar
cn flag
You've provided no OS/release details, but why not have your backup script test for the drive's connection before it performs the `rsync`, and drop out (*abend*) if the drive isn't mounted... it's what my scripts do.
HuHa avatar
es flag
Check `/proc/mounts` in your script to see if the device is mounted.
Sam O'Riil avatar
kr flag
Check with `blkid` that your backups drive is mounted. Or using some other way, like checking for presence of special file from your backups drive under that mount point.
uz flag
Jos
Keep in mind that when your drive is mounted, the files that were backed up to the local drive will be unreachable (since the mount point is now used for the external drive), but they still take up space on your drive. You need to `umount` in order to access or remove this data.
Score:5
cn flag

No, fstab does not create the mount point. The mounts defined in fstab are executed during system startup. After that, fstab will do nothing. An external drive usually is not declared in fstab so it is handled by the automated mounting mechanism for removable drives.

To solve your issue, include a test in your script before proceeding with the rsync command. You may simply test if the target folder on the removal drive exists before proceeding to the rsync command. Alternatively, you can test whether the drive is mounted using findmnt or mountpoint, or mount (which is equivalent to cat /proc/mounts).

The_Chris avatar
bd flag
If I don't mount the drive in FSTAB, it doesn't get mounted automatically. I have to click on it in my file manager to mount it. My backup runs on boot. That's why I mount it in FSTAB.
Score:4
cn flag

My rsync script includes this check & mount as I have copied into / in error and data was then hidden under another mount point making it more difficult to find to delete. I also run some other commands in rsync backup to document system configuration & copy those files into sub-folders by version.

LOG=/var/log/backup.log
echo "* $(date -R) *" >> $LOG
# HOSTNAME=$(hostname -s)
SYSFOLDER="z690"
VERFOLDER="jammy"
if grep -qs /mnt/backup /proc/mounts; then
  echo "It's mounted."
else
  echo "It's not mounted."
  mount -t ext4 /dev/sda3 /mnt/backup
  if [ $? -eq 0 ]; then
   echo "Mount success!"
  else
   echo "Something went wrong with the mount..."
   exit $?
  fi
fi
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.