Matthew's Dev Blog

Using Synology NAS "USB Copy" to backup multiple shared folders

Acknowledgement:
The idea to use bind mounts comes from this blogpost. Thank-you, "Thunderbyte Computer"!

Scripts are available in this GitLab snippet. Contributions welcome :)

Synology NAS has a "USB Copy" feature which can automatically copy data from the NAS to a USB drive - but it forces the user to select a single shared folder as the source. Here's how to backup everything.

Overview

"Bind mounts", in a Unix system, allow you to mount an already-mounted filesystem to another location. This post describes how to use bind mounts to map multiple shared folders into a single shared folder, and then backing up that single shared folder to a USB drive.

Process

Create a shared folder to contain the bind mounts

Start by opening the Control Panel and creating a new Shared Folder:

Create a new Shared Folder

I named mine _BackupUSB, made it hidden, and disabled the Recycle Bin.

Give the new Shared Folder a name

Create two new Triggered Tasks

In the Task Scheduler section of Control Panel, create a new Triggered Task:

Create a new Triggered Task

Name the task mountbind, run as user root, and set the event to "Boot-up":

Creating a boot-up task

Then paste in the contents of this script:

#!/usr/bin/env sh

SOURCE_FOLDER=/volume1
DESTINATION_FOLDER_NAME=_BackupUSB

DESTINATION_FOLDER=$SOURCE_FOLDER/$DESTINATION_FOLDER_NAME

if [[ ! -d $DESTINATION_FOLDER ]]
then
    echo "$DESTINATION_FOLDER_NAME shared folder does not exist."
	exit 1
fi

cd $SOURCE_FOLDER
find . -maxdepth 1 -type d -not -name "@*" -not -name "." -not -name "$DESTINATION_FOLDER_NAME" | while read folder
do
  echo "Binding folder $folder"
  mkdir -p "$DESTINATION_FOLDER/$folder"
  mount -o bind "$folder" "$DESTINATION_FOLDER/$folder"
done
Creating a boot-up task

This script will run whenever the Synology boots.

Next, create a second Triggered Task. Name this task unmoundbind, run as user root, and set the event to "Shutdown":

Creating a shutdown task

Then paste in the contents of this script:

#!/usr/bin/env sh

SOURCE_FOLDER=/volume1
DESTINATION_FOLDER_NAME=_BackupUSB

DESTINATION_FOLDER=$SOURCE_FOLDER/$DESTINATION_FOLDER_NAME

if [[ ! -d $DESTINATION_FOLDER ]]
then
    echo "$DESTINATION_FOLDER_NAME shared folder does not exist."
	exit 1
fi

cd $DESTINATION_FOLDER
find . -maxdepth 1 -type d -not -name "@*" -not -name "." | while read folder
do
  echo "Unbinding folder $folder"
  umount "$DESTINATION_FOLDER/$folder"
done
Creating a shutdown task

This second script will run whenever the Synology shuts down.

Reboot the NAS

Reboot the NAS. After rebooting, open File Station, and look inside the _BackupUSB folder. You should see folders inside which match your other Shared Folders, and their contents should also be visible on the right-hand side. (See red highlight)

File Station contents after rebooting the NAS

Prepare a USB drive (or two)

Skip this step if you already have a formatted USB drive.

Plug your USB drive into the front USB socket on the NAS, and open the External Devices section in Control Panel.

Locate the USB disk in Control Panel

From there the disk can be formatted as EXT4.

Format the USB Disk as EXT4

Create a USB Copy export task

Now create a USB Copy export task. Open USB Copy from the Main Menu, and press the blue "Create" button (+). Give the task a name (I created two tasks for two external disks and called them Offsite A and Offsite B), select /_BackupUSB as the source and /usbshare1 as the destination. Choose your copy mode. (I went for "Incremental", because subsequent backups are much faster)

Creating an export task

Trigger the copy operation whenever the USB drive is plugged in, and eject the drive when the copy completes:

Set the start and stop behaviour

And complete the setup by selecting all filetypes for backup:

Select all filetypes for backup

Configure USB Copy notifications

Next, we can use the Control Panel to set up some notifications. Go to the "Notification" section, then select the "Rules" tab. Expand the "USB Copy" section - don't be concerned that it all appears to be disabled. From there, you can enable notifications for when the task completes.

Enable notifications for when the task completes

Start the first copy task

Use the eject button, in the right-hand corner of the DSM task bar to unmount the USB disk. Then unplug and re-insert the USB drive to start the first copy task.

My offsite backup strategy

I bought two 5TB USB drives, and set up a separate export task for each drive. I'll do a weekly export to a drive, then take that drive to work and bring the other drive home.

Backup speed with my hardware

Approximately 10 hours per TB, for the initial backup.

Tagged with:

First published 20 October 2022