Skip to main content

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:

2025-05-17 18_01_57-Administrator_ Windows PowerShell ISE.png

It indeed copied the folders and files I needed:

2025-05-17 18_23_57-1TB SSD (E_) and 1 more tab - File Explorer.png

2025-05-17 18_24_25-screenshots and 1 more tab - File Explorer.png

I then created a task in the Task Scheduler by doing Win + R and entering taskschd.msc

2025-05-17 18_02_46-Run.png

Create New Task:

2025-05-17 18_03_12-Task Scheduler.png

Under General Tab create a Name and check the 2 boxes below:

2025-05-17 18_04_10-Create Task.png

Under the Triggers tab click New, Begin Task at Logon, and for Any User:

2025-05-17 18_04_43-Create Task.png

2025-05-17 18_05_30-New Trigger.png

Under the Actions Tab, Click New > Start a Program > powershell.exe

2025-05-17 18_08_34-New Action.png

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"

2025-05-17 18_09_29-New Action.png

Under Settings do the following:

2025-05-17 18_10_26-Create Task.png

Click OK and then it will prompt you for your logon password:

2025-05-17 18_11_31-Task Scheduler.png

Once you enter that correctly this will run on logon and make sure you have another offsite backup out of Syncthing.