Automatically mount different backup disks to the same path


Recommended Posts

Your target

You want to use a single destination path for all your backup tools like scripts and plugins, but you don't want to use only a single eSATA/USB backup disk. Instead you want to swap them every now and then.

 

Problem

Each backup disk has a different name and by that the UD plugin mounts it at a different path.

 

Solution

- install the Unassigned Devices (UD) Plugin

- format your backup disks (Hint: Use ext4 as you can mount it with Windows 11 and OSX and instead of NTFS it fully supports linux file permissions!)

- change the disk names to BACKUP1, BACKUP2, etc or BACKUP_A, BACKUP_B, whatever you prefer. The only requirement is: Its name needs to start with "BACKUP"

- open the settings of your eSATA/USB backup disk in the UD list

- paste the following script into the device script content area

- enable automount

- mount the disk the first time

- the disk is now mounted at /mnt/disks/BACKUP1 (or whatever the disks name is)

- the script automatically does its job and creates a symlink of this disk at /mnt/disks/BACKUP

- use /mnt/disks/BACKUP for all your backup tools

 

Successfully tested with the appdata and vm backup plugins.

 

Script

#!/bin/bash

if [[ $ACTION != 'REMOVE' ]] && [[ $ACTION != 'ADD' ]]; then
  logger skip
  exit
fi

unset disk_found

while IFS= read -r -d '' disk_name; do
  if [[ $disk_name == /mnt/disks/BACKUP ]]; then
    continue
  fi
  if mountpoint "$disk_name"; then
    disk_found=true
    # symlink targets the correct HDD
    if [[ $(readlink -f /mnt/disks/BACKUP) == "$disk_name" ]]; then
      break
    fi
    # symlink is wrong
    if [[ -L /mnt/disks/BACKUP ]]; then
      rm -v /mnt/disks/BACKUP
    fi
    # a dir with the same name is not allowed
    if [[ -d /mnt/disks/BACKUP ]]; then
      logger "Error: /mnt/disks/BACKUP is a directory (should be symlink)!"
      break
    fi
    ln -v -s "$disk_name" /mnt/disks/BACKUP
    break
  fi
done < <(find /mnt/disks -name "BACKUP*" -print0)

if [[ ! $disk_found ]]; then
  Logger "No Disk found"
  if [[ -L /mnt/disks/BACKUP ]]; then
    rm -v /mnt/disks/BACKUP
  fi
fi

 

Example

image.png.d69db79209dc831dceaef40d95599588.png

 

Notes

- successfully tested with backup scripts and the vm and appdata backup plugins

- accidentally mounted multiple BACKUP disks? No problem. The script skips additional disks.

- accidentally created a disk with the name "BACKUP" (which would be the same as the symlink)? No problem. The script skips creating the symlink and writes an error to the logs.

Link to comment
  • mgutt changed the title to Automatically mount different backup disks to the same path

@mgutt May I ask some questions? I'm puzzled by the Device Script and the User Script fields.

 

To clarify: Consider three USB Devices. And consider three User Scripts for them. I named these devices BACKUP1 ... BACKUP3. The User Scripts are identical (a bunch of rsync commands) BUT the /mnt/disks/BACKUPn mount point. This is what I use currently.

 

Now I copy/paste your script in the "Device Script Content" field for all three devices (one at a time).

 

I got that til that point.

 

Now there's a User Script entryfield. It says:

 

Quote

If you have the User Scripts plugin installed, you can have Unassigned Devices show the running status of the User Script that will be using this device. This field will only be enabled when a Device Script File exists.

 

So to use a User Script and let Unassigned Devices track its process, the "Device Script Content" field is the wrong way to paste the code? I need to put your script somewhere on the flash and enter it's location in the "Device Script" field instead? Then I remove two of my User Scripts and consolidate them leaving one User Script that refers to /mnt/disks/BACKUP from now on.

 

Is this correct?

 

And by the way: Starting the User Script stays a manual activity? It does not start automatically on Automount?

 

Thanks for your valuable support.

 

Edited by hawihoney
Link to comment
16 hours ago, hawihoney said:

the "Device Script Content" field is the wrong way to paste the code?

No. Device Script Content is correct as you can see in my above screenshot.

 

16 hours ago, hawihoney said:

I need to put your script somewhere on the flash and enter it's location in the "Device Script" field instead?

I tried this, too, but wasn't successful.

 

16 hours ago, hawihoney said:

Now there's a User Script entryfield. It says:

Something which I don't understand, too. ^^ I added a "Hello world" script to the user scripts plugin, but I absolutely don't understand how they interact each other. 🤷‍♂️

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.