Skip to content

Instantly share code, notes, and snippets.

@dbowling
Created May 4, 2018 16:31
Show Gist options
  • Save dbowling/8e3a530e2ffc6450ee6a56dd1843d713 to your computer and use it in GitHub Desktop.
Save dbowling/8e3a530e2ffc6450ee6a56dd1843d713 to your computer and use it in GitHub Desktop.
PowerShell script to wrap Restic backup allowing Volume Shadow Copy.
#
# This script must be run with Administrator privs in order to create VSS snapshot!!!!
#
# Windows PowerShell Script to use restic to backup files using the Volume Shadow Copy Service, allowing
# that are in use to be backed up. The script must be run with elevated privileges.
# The Volume Shadow Copy Service must be enabled for the disk volume that contains the files to be backed up.
#
# Parameters
$resticExe = 'C:\bin\restic.exe'
$resticRepository = 'b2:bucket-name:folder'
$rootVolume = "C:\"
# List of folders to backup, separated by commas
$foldersToBackup = @(
'Users\'
)
# Your Restic password should be available in the RESTIC_PASSWORD environment variable.
# If not uncomment the following line with the actual password
$env:RESTIC_PASSWORD = "CORRECT-HORSE-BATTERY-STAPLE"
$env:B2_ACCOUNT_ID = "ACCOUNT ID"
$env:B2_ACCOUNT_KEY = "YOUR ACCOUNT KEY"
###### No need to modify anything below this line ######
$ShadowPath = $rootVolume + 'shadowcopy\'
# Create a volume shadow copy and make it accessible
$s1 = (Get-WmiObject -List Win32_ShadowCopy).Create($rootVolume, "ClientAccessible")
$s2 = Get-WmiObject Win32_ShadowCopy | Where-Object { $_.ID -eq $s1.ShadowID }
$s1 = (Get-WmiObject -List Win32_ShadowCopy).Create($rootVolume, "ClientAccessible")
$s2 = Get-WmiObject Win32_ShadowCopy | Where-Object { $_.ID -eq $s1.ShadowID }
$device = $s2.DeviceObject + "\"
# Create a symbolic link to the shadow copy
cmd /c mklink /d $ShadowPath "$device"
# Run Restic on the data files in the shadowcopy
ForEach ($folderToBackup in $foldersToBackup) {
cmd /c $resticExe backup -r $resticRepository ($ShadowPath + $folderToBackup)
}
# Delete the shadow copy and remove the symbolic link
$s2.Delete()
cmd /c rmdir $ShadowPath
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment