I began using a Synology NAS almost a decade ago, when I decided I wanted to do away with all of the one-off backup CDs and external drives for my computer files. Having a NAS as a central file hub in your house is great, and it streamlines and centralizes the backup of your files.
I could write many articles on how I leverage my Synology NAS box for central file storage and access, but here I want to focus on what was the most manual setup component for me – getting Synology Hyper Backup to weekly back up my data to USB drives externally connected to the NAS. Hyper Backup, which comes as part of the Synology ecosystem, allows you to regularly back up your files to almost anywhere, including the cloud if you so choose. In my use case, I wanted to backup my “documents” (including downloads, music, video, and pictures) to one externally-attached USB drive and my system backups (e.g. a separate backup I have for my Windows desktop OS) to another USB drive.
Further, and here is the key issue, as I only wanted to run these backups once a week, I did not want the externally-attached USB drives to be always on/always powered on. I only wanted them on for the backups themselves, and to turn off (or simply put into standby so the mechanics are not spinning) when the backup processes complete.
Synology Hyper Backup has (or had, I cannot find it now) an option to “detach” the external drive when a backup schedule completed. However, for me it never worked, and the USB drives would remain powered on. Further, there was no way to automatically “spin up” the drives prior to a backup. Once a USB drive attached to a Synology NAS drive is idle, it appears to be idle permanently as far as the GUI knows, and you cannot “turn it back on” via DiskStation Manager.
(For reference, my Synology NAS is model DS1019+. It is possible any issues talked about herein have been addressed in newer models, but I simply do not know.)
My procedure for backing up my data should be:
- Turn on the appropriate USB external drive in preparation for the next Hyper Backup schedule, a few minutes ahead of time.
- Run the Hyper Backup schedule automatically.
- Turn off the USB external drive after the schedule completes.
At this point, you could ask, “Why not unplug and re-plug in the USB wires around the backup schedule?” A fair question, but that defeats the point of having an automated backup. I want my backups to happen off-hours, in the middle of the night while I sleep. Plus, my NAS is not in a convenient place where I would want to be getting onto the floor several times a week to plug and unplug these drives. By nature and purpose of having a NAS, I deliberately placed in out of the way, in a location that is not readily accessible.
After many Internet searches, I discovered it possible to have shell scripts run directly on the Synology NAS operating system to manipulate the power of externally-attached USB drives. Synology, as far as I can tell, uses its own flavor (distro) of Linux. If you wish to try this method, I highly recommend being familiar with Linux and editing shell files.
Create User-Defined Schedules in Synology
Let’s get the easy part out of the way – how to execute a shell script from within the Synology GUI and front-end, DiskStation Manager. I am currently running DSM 7.1.1, but this has always worked for me for all prior DSM versions. To create a user-defined schedule, go to Control Panel and select Task Manager. Then click Create and choose Scheduled Tasks -> User-defined script.
Then when building the user-defined script schedule, for the “Run command” you simply paste in the script and path you wish to run, for example:
/var/services/homes/<YOUR ADMIN ACCOUNT>/usb-attach.sh
In my current Synology Hyper Backup setup, I am using two custom scripts, usb-attach.sh and usb-detach.sh. These are just the names I gave them; they can be named whatever you want.
- usb-attach.sh – This script turns on all USB drives connected to my Synology NAS.
- usb-detach.sh – This script turns off all USB drives connected to my Synology NAS.
“all” is the key word because I used to have more surgical scripts that could individually turn on and off one specific drive. However, for several reasons, this has become difficult with newer drive models, so I have resorted simply to turning them all on, running the needed Hyper Backup tasks, and then turning them all off at a designated time, well past when the tasks normally complete.
As far as schedule sets, I have two main groups:
- One night runs two Hyper Backup schedules to back up my documents/personal data to one USB drive, and a second Hyper Backup schedule to back up my system/OS files to a second USB drive.
- On the subsequent night, two corresponding Hyper Backup tasks run “integrity checks” on both USB drives, to ensure the data can be restored. Note that these must be scheduled from within the Hyper Backup application and not through Synology’s Task Manager.
A Note on Drive Makes and Models
Through my experience, the only external USB drives these scripts work with are from Western Digital. I tried Seagate drives but I could never figure out how to turn them off. For reference here are my current USB drives attached to my Synology NAS as shown via the lsusb command:
Western Digital My Passport 260D
Western Digital easystore 2647
How to Get to the Synology Command Line
In case you do not know, here is how to get to a terminal prompt to interface with your Synology Linux OS.
I highly recommend you enable the SSH service, and potentially change the SSH port from the default. You can do this in the Control Panel under Terminal & SNMP.
You can then use an application like PuTTy to log into your NAS with SSH and the port you chose. You will log in with your Synology admin account.
And generally, I keep the Telnet service disabled and even the SSH service disabled when I do not need it.
Script to Turn On All External USB Drives
Here is my “USB attach” script which will ensure all of my external USB drives are powered on. As mentioned prior, I have Synology NAS model DS1019+, one drive connected to the front USB port and one connected to the back USB port. Your model, setup, and needs could be different, but this can hopefully act as a rough template for you.
I know, that with my model, the USB drives drives will go to system blocks sdq and sdr (under /sys/block). A reason that I abandoned per-drive scripts is that with my newest WD drive, the 5TB My Passport 260D, I am unable to turn it on directly and instead need to turn it on at the “usb2” level, above both drive ports. Both of my drive ports, front and back, go to usb2, and are then designated as either 2-1 or 2-2.
Important to note is that I assume both drives are off, otherwise this script will momentarily turn the drives off when setting “authorized” to 0.
#!/bin/sh # *** ASSUMES TWO USB DRIVES ARE ATTACHED *** # *** ASSUMES THE TWO DRIVES WILL GO TO sdq AND sdr *** # # 05/05/2023 # Unlike prior scripts, now I just turn "everything" on under the usb2 tree. # Both drives will be turned on, after momentarily being turned off. echo 0 > "/sys/bus/usb/devices/usb2/authorized" sleep 3 echo 1 > "/sys/bus/usb/devices/usb2/authorized"
I run this script (as a Synology Task Manager User-defined script, see above) five minutes before a Synoloy Hyper Backup task starts, e.g. usb-attach.sh will run at 01:25 if my next Hyper Backup schedule runs at 01:30.
Script to Turn Off All External USB Drives
Here is the corresponding “turn off” script which I run well after the Hyper Backup task(s) should complete for a given night. I have not figured out a way to trigger this script, usb-detach.sh, immediately after the last night’s Hyper Backup task completes, as a dependency. So as an alternative, I simply choose a time the next morning, well past when I know the backups will end, and turn off all drives via this script.
#!/bin/sh # Assumes two USB drives are attached to the Synology box. # Assumes their designations are sdq and sdr. sync sleep 3 /usr/syno/bin/synousbdisk -umount sdq sleep 3 /usr/syno/bin/synousbdisk -umount sdr > /tmp/usbtab sleep 3 echo 1 > /sys/block/sdq/device/delete sleep 3 echo 1 > /sys/block/sdr/device/delete
Unlike the “attach” script, which merely authorizes (powers on) everything under usb2, this script unmounts and deletes the devices from Synology, which powers them off.
If you have questions about my method or the code, please write a comment below. If you have a better, more refined way to do these tasks, I would love to hear about your approach as well!
All instructions above are presented without warranty or guarantee they will work for your own needs and/or Synology NAS setup.