Want to hire me?

If you would like to discuss how I can help you with mobile app development (Xamarin/Flutter), back-end development, C#, Dart, .NET Core, Azure, Firebase, Google Cloud Platform, or reactive programming then please get in touch.

Replacing Synology’s Data Replicator with Powershell and Robocopy

I recently upgraded (for some definition of “upgraded”) from Windows 7 to Windows 8. I had been using Data Replicator 3 to keep my machine backed up to my Synology server. However, I was keen to be rid of it because it is awfully slow and cumbersome. So I set out to do exactly that by leveraging Powershell and Robocopy, both of which are installed by default on Windows 8.

It’s important to note that the solution I provide here isn’t a like-for-like replacement of Data Replicator. It doesn’t provide any help restoring data – you would have to either do that manually or write another script to reverse the robocopy from server to client. But on the plus side, there’s nothing Synology-specific here either, so you can use this regardless of your server technology.

So here’s the Powershell script I concocted (obviously you’ll need to tweak the $Server and $PathsToBackup properties for your setup):

# Incrementally backs up files on a client to a server
 
# the name of your server
$Server = "SERVER_NAME"
 
# the local paths to back up to your server. All paths must be fully-qualified
$PathsToBackup = @("C:\Users", "C:\SOME_OTHER_PATH")
 
$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDir = Split-Path $ScriptPath
$Client = [System.Net.Dns]::GetHostName()
$LogPath = "$ScriptDir\BackupLog.txt"
 
if (Test-Path -path $LogPath)
{
    Remove-Item $LogPath
}
 
New-Item $LogPath -type file
 
foreach ($PathToBackup in $PathsToBackup)
{
    $PathQualifier = Split-Path -Qualifier $PathToBackup
    $PathQualifier = $PathQualifier.Replace(":", "")
    $PathToBackupWithoutQualifier = Split-Path -NoQualifier $PathToBackup
    $Command = "robocopy $PathToBackup \\$Server\Backups\$Client\$PathQualifier\$PathToBackupWithoutQualifier /FFT /MIR /NP /W:0.5 /R:0 >> $LogPath"
    Invoke-Expression $Command
}

I placed this script in my home directory with a name of Backup.ps1.

Next, I set up a scheduled task to run this script every day. Open the Task Scheduler and select the Task Scheduler Library. Right-click in the list of tasks and select Create New Task… Use the following screenshots to guide you configuring the task. I’ve also listed the changes textually in case the images stop working (or are blocked for you):

General Tab

General Tab

Configure for: I set mine to Windows 8, but not sure whether that’s necessary

Triggers Tab

Triggers Tab

Set whatever trigger you like, but I set mine to run daily.

Actions Tab

Actions Tab

Click OK to create the task. You will be prompted for a password, so enter that. Now you’ve got a scheduled task that will incrementally back up whatever local files you like to your server. Finally, note that the script produces a log (truncated on each run) called BackupLog.txt in the same directory as Backup.ps1, so this may come in handy if something isn’t backing up as expected.

comments powered by Disqus