tl;dr
Made a PS 5.1 script that runs Duplicati GUI jobs based on the day of the week. The GUI is always up-to-date, users can change Source selections, advanced options or do restores from the GUI. The script will try to run the backup job for the current day of the week, if today’s drive is not found it uses the connected drive, if more than one drive is connected it uses today’s backup job or the least recently used backup job to help rotate backup sets. Drives can move from one drive letter to another without issue.
Good day, I’ve recently started using Duplicati and I quite enjoy it. Thank you to all those that help make Duplicati what it is.
I’m an IT consultant that supports mostly small businesses and I’m always being asked to implement a “better backup, for as little as possible”. With that, the Duplicati price tag fits the bill but most of these clients seem to only want to backup to local USB disks which Duplicati doesn’t do multiple disks without a bunch of workarounds.
Also many clients aren’t great at swapping the drive on the daily so I needed a way to run a backup regardless of which drive was connected or what day it was. I also wanted the clients to be able to check the status or do a restore on their own, for which the GUI is perfect.
I looked into the using CLI but users can’t easily edit them, still requires a “calling script” and then there’s the whole doesn’t update the GUI and restores are less friendly.
The main goal being daily encrypted backups to whichever backup drive is connected every day of the week. Most of the “servers” that will be backed up with this are Win7 or Win10 workstations that are sharing a few files, acting like a small file server.
I looked at Pectojin’s Duplicati Client but it turned out to be way more than I needed. I did use a PRTG-Duplicati script by TS-Steff as a base for my script. Thanks to both of them.
After a few days, I managed to get together what I think is a pretty handy script. I’ll post the script in a separate post, the heavily commented script with setup instructions is just over 25,000 characters in length which is probably over the character limiter per post here but I’ll try and if not I’ll trim out the comments or find somewhere to host the file and link back to it.
Verbose setup instructions in script. I’ll put a link to the script here once I get it posted. Edit: The next post down contains the whole script. Thanks to drwtsn32 for getting it formatted.
The basic setup is as follows:
Install Duplicati, run the Tray Icon and create a backup job in the GUI for each day of the week. Each job uses the day of the week as it’s name and each backup drive is labeled the same.
The Tray Icon HAS TO BE RUNNING for this to work.
Jobs are NOT scheduled within Duplicati, the script determines which GUI job will run.
Create and save backup jobs once before adding any of the Advanced options, seems to be a weird issue where they vanish if added during first creation. Once saved, edit the job and add the advance options
Use “alternate-target-paths” and “alternate-destination-marker” so each drive had a folder and a unique marker file to prevent the wrong job triggering with the wrong drive connected. Also avoids backing up your data to your kids/friends/co-workers USB stick that was left in your computer by mistake. Advanced option values per job are “*:\BackupFolder” and “DayOfTheWeek_BackupsGoHere.txt”, respectively. DayOfTheWeek being a place holder for the correct day of the week.
e.g. The Monday drive is labeled “Monday” and has a folder called “BackupFolder” and a marker file in that folder called “Monday_BackupsGoHere.txt”. The Tuesday drive is labeled “Tuesday” and has a folder called “BackupFolder” and a marker file in that folder called “Tuesday_BackupsGoHere.txt”
I’ve also added and enabled “use-block-cache” on all jobs. If backing up open files is an issue add “snapshot-policy” and set it to “ON”. Note: the “Duplicati.GUI.TrayIcon.exe” will need to be setup to “Run as an administrator” or you’ll get errors about snapshots when you run the job.
With that in-place the script can now run any of those GUI jobs and uses the following logic to determine which drive to use.
-Searches for connected drives and saves each drive’s label and drive letter to an array.
-Queries the Duplicati Tray Icon for all existing backup jobs and saves each job name and job id to an array.
-If there is one drive connected and it is today’s drive, the script runs the backup job for the current day of the week.
-If there is one drive connected and it is not today’s drive, the connected drive is used instead.
-If there is more than one drive connected and one of them is today’s drive then today’s drive will be used. (As I write this I’m thinking I should re-write the multiple drives determiner to use the LRU regardless of the current day but that’s a minor thing.)
-If there is more than one drive connected but none of them are today’s drive the least recently used backup will be run. LRU determined by checking each jobs LastBackupFinished date then sorting by oldest and using that backup.
-Once the correct job is found the /run command is issued to the REST server to start that backup job.
Features:
-Works with one to seven drives even if connected simultaneously.
-Uses “the right drive for the day” or “the right drive for yesterday” or “the right drive because it’s the only one currently connected” or “the right drive we haven’t backed up to recently”. Either way, it always has a “right drive” unless there isn’t one connected.
-The script can log all it’s output to a .txt file, that currently saves to the current users Desktop folder.
-There is a test mode that does everything but send the /run command for the backup job.
While I’ve done a fair bit of testing and it all seems good I can’t be sure that this will work for you (I hope it does) but it’s on you to test for yourself in your environment. The script may work (or could probably be made to work) with lower versions <5.1 of powershell but as it stands you can get/use PS 5.1 in Win7 and up, which should cover most of the likely usage scenarios. I’m pretty sure the logic/methods could be adapted to run a similar script for Linux or macOS, but that’s a task for another day.
FYI: I don’t write code for a living so expect my formatting and use of syntax to be a bit off along with my use of camelCase or PascalCase in my variables will vary. you’ve been warned.
You made it to the end, thanks for reading, I hope this post along with the script can help someone get the backups they need or maybe just help them understand some of the code that was used. If you have any questions don’t hesitate to ask.
Have a good one.