2

The tl;dr, how do I make Ubuntu Server 16 with no desktop GUI automatically mount a USB drive with a known drivelabel and UUID to an explicitly defined folder upon insertion?

I'm making a backup server where the production database server will backup to the backup server at /data/backup/OffsiteBackup. When the daily backup is finished, I want to copy the contents of /data/backup/OffsiteBackup/ to USB drives mounted on

/data/backup/OffsiteMon[A|B], 
/data/backup/OffsiteTue[A|B], 
/data/backup/OffsiteWed[A|B], 
/data/backup/OffsiteThu[A|B], 
/data/backup/OffsiteFri[A|B], 

where 'A' and 'B' correspond to the two week retention period we have.

This server will be a headless unit accessible solely via SSH. I want minimal user interaction, including the mounting of the USB drives on Friday, Saturday, or Sunday for the following week's worth of offsite backups. In Lubuntu, if you plug a USB drive in, it's automatically mounted at /media/$USER/$DRIVENAME (or /media/$USER/$UUID if $DRIVENAME doesn't exist). After the drive is ejected, the folder is deleted. I don't need that because I have all ten folders created. The script which will copy the offsite backup to the USB drive will also unmount the flash drive, thereby making it so no one has to umount the drive.

How do I make Ubuntu Server automatically mount a USB drive to a specific folder when it's plugged in? Is there a "preferences" or dictionary file of sorts where I can specify that a drive with UUID=ABCD-1234 be mounted at /data/backup/OffsiteMonA? I'd rather avoid making ten separate entries in /etc/fstab.

2

Unfortunately, no.

This is, in fact, the exact reason /etc/fstab exists: to facilitate the creation of specific and predictable mountpoints. Note, though, that /etc/fstab (obviously) does not need to only mount on nonexistent folders. If you have /data/backup/OffsiteMonA already existing, the system will have no problem with mounting anything else there, but any data in that folder pre-mount will be temporarily lost.

While you could use a script running as root to mount specific drive IDs (/dev/sdd1) to specific locations, this is often ill-advised because of how the system operates, and the fact that you have no guaranteed consistency that something will be mounted to a specific place.

Therefore, you should use /etc/fstab to specify all of these mount points, and then have your script to check that there's actually a mounted drive there. If so, run the backup. Else, throw a nice big error at someone.

Additionally, by using the /etc/fstab method, you can control mounting and unmounting as well. When you umount /data/backup/OffsiteMonA, it will unmount that specific drive, which may be remounted at any time by running mount /data/backup/OffsiteMonA.

  • Thanks. How is it then that Lubuntu can mount a USB drive on insertion? Is that something the desktop environment does? – user38537 Sep 21 '16 at 19:08
  • @user38537 Sorry, I just saw your comment. Lubuntu does this through GVFS and userspace filesystems, which is essentially a user-friendly way of mounting drives without needing sudo powers. If you want specific mountpoints, you need to use fstab (which effectively does the same thing as GVFS, but with root power). – Kaz Wolfe Sep 30 '16 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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