Regularly Swapping Two USB Drives

Hello. I’m a new DietPi user with a headless RPi4 trying to replicate functionality I had on a now-defunct TonidoPlug device.

The main use of this device is to use rsync to copy remote files to an external USB drive connected to the Pi for backup purposes. I have two identical USB drives that I regularly swap out, keeping one attached to the device while storing the other offsite. The drives of course do not contain the boot partition.

In my previous TonidoPlug setup, both drives mounted to the same location, so I could use the same rsync command in a script. I don’t have a copy of that fstab file for reference, so as a first shot, I tried using dietpi-drive_manager to mount the two drives, and then editing the fstab lines to make them use the same mountpoint. Didn’t work. I also know that dietpi-drive_manager will overwrite this file if I run it again for any reason.

Here are my current /etc/fstab entries with each drive getting a unique mountpoint.

UUID=A0F65096F6506F12 /mnt/WD6TB_1 ntfs noatime,lazytime,rw,permissions,big_writes,nofail,noauto,x-systemd.automount

UUID=AE8C7B328C7AF465 /mnt/WD6TB_2 ntfs noatime,lazytime,rw,permissions,big_writes,nofail,noauto,x-systemd.automount

This seems to be ok as far as plugging in either drive and accessing it. It would be fine (maybe even better than the same mountpoint for each), except for two issues…

  1. I don’t know how to identify in a bash script for my rsync command which drive is currently connected. I found the following suggestion on a different forum, but both entries are in /proc/mounts, so it shows them both as mounted even when one isn’t connected.

grep -q ’ /mnt/WD6TB_1 ’ /proc/mounts && echo “mounted” || echo “not mounted”

  1. I know (or think) that x-systemd sort of mounts the device on the fly when it is first accessed. But when I try to cd into the directory of the drive that isn’t mounted, the shell locks up.

Can anyone offer some advice on how to either at least solve issue 1 (identify in a script which drive is connected)? Or help me figure out how to make it so both drives have the same mountpoint? I hope I’ve described things well enough, but let me know if I haven’t

Thanks in advance. I really appreciate it!

Welcome to our community.

Theoretically you could try using udev rules to check on the disk once connected and mount them automatically. Found following on a quick search Automount USB devices on Linux using UDEV and Systemd | Andrea Fortuna

Thank you for the lead on the UDEV rules. I’ll check that out.

I also did find a blkid command that cleanly reports just the UUID of the attached drive…

$> blkid -o value -s UUID /dev/sda1

I should be able to work with that in the script. It won’t be the most robust or elegant solution, but I’ve never been accused as such.

You were in the right track already: Remove the ,x-systemd.automount from both entries, else or tries to mount both onto the same mount point, hanging 90 seconds if one is not available.

When/how do you want to mount them? In theory, if those are attached to the same USB port, you could change the first parameter from UUID to /dev/sda1 (and have one entry for both drives only) e.g. and re-add ,x-systemd.automount to it or remove noauto, so whichever drive is attached at boot will be mounted automatically.

Yes, that seems to work great! I followed your suggestion of keeping a single fstab entry with the uuid replaced with /dev/sda1, while leaving the x-systemd.automount portion and both drives get mounted properly to the same destination when alternately plugged into the same USB port.

With this approach, I think I’ll need to reboot with each swap, which isn’t a problem. I’ll also need to keep in mind that the dietpi-drive_manager utility will overwrite this file if it is ever run in the future.

Much appreciated!

It should also be possible to unmount the drive with umount command, swap the drives and remount with mount command, without rebooting, or do I miss something here?

I’m not 100% sure whether if unplugging one drive and attaching a different one to the same USB port (without reboot), it has the same /dev/sdX path :thinking:. Worth to simply test this.

I will test that out and report back.

What I’m not sure about is the mount command that will mount the drive in the same way as it would when booting (via the fstab entry). Currently…

/dev/sda1 /mnt/Elements ntfs noatime,lazytime,rw,permissions,big_writes,nofail,noauto,x-systemd.automount