Creating a PowerShell Backup Script for VPS Syncthing Folders
I'm using Robocopy to sync folders with logic to skip unchanged files.
# Define source and destination paths
$source1 = "C:\Users\aonat\BookStack-VPS-Backups"
$source2 = "C:\Users\aonat\Default Folder"
$destination = "E:\VPS-Backups"
# Ensure destination exists
if (!(Test-Path -Path $destination)) {
New-Item -Path $destination -ItemType Directory
}
# Mirror BookStack-VPS-Backups
Robocopy $source1 "$destination\BookStack-VPS-Backups" /MIR /Z /FFT /XA:H /W:5 /R:3 /XO
# Mirror Default Folder
Robocopy $source2 "$destination\Default Folder" /MIR /Z /FFT /XA:H /W:5 /R:3 /XO
The only thing that is confusing in the script is all the logic trailing the Robocopy commands. Here is the breakdown of that:
-
/MIR
– Mirrors folder (adds new, removes deleted files) -
/XO
– Excludes older files (does not overwrite newer destination files) -
/Z
– Enables restartable mode (safe for external drives) -
/FFT
– Treats file times as FAT-style (2-second tolerance; good for cross-OS syncing) -
/XA:H
– Skips hidden files -
/W:5
//R:3
– Waits 5 seconds and retries 3 times on errors
I tested and made sure the script worked as intended:
It indeed copied the folders and files I needed:
I then created a task in the Task Scheduler by doing Win + R and entering taskschd.msc
Create New Task:
Under General Tab create a Name and check the 2 boxes below:
Under the Triggers tab click New, Begin Task at Logon, and for Any User:
Under the Actions Tab, Click New > Start a Program > powershell.exe
Add this argument script in the Add arguments section replacing the path to the script:
-NoProfile -ExecutionPolicy Bypass -File "C:\Path\To\backup-vps-folders.ps1"
Under Settings do the following:
Click OK and then it will prompt you for your logon password:
Once you enter that correctly this will run on logon and make sure you have another offsite backup out of Syncthing.