Last active
March 10, 2022 20:07
-
-
Save darylwright/f33e544d005767520266637c82fe5604 to your computer and use it in GitHub Desktop.
An opinionated backup script using Robocopy. This script will sync files onto a path that you specify. Must be run as an Administrator. Modify as per your use case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Requires -RunAsAdministrator | |
| param( | |
| [Parameter(Mandatory)] | |
| [string]$BackupPath | |
| ) | |
| # You can use this function on its own instead of using the entirety of this script | |
| function SyncFiles { | |
| param ( | |
| [Parameter(Mandatory)] | |
| [string]$SourcePath, | |
| [Parameter(Mandatory)] | |
| [string]$DestinationPath, | |
| [Parameter(Mandatory)] | |
| [string]$LogPath, | |
| [Parameter()] | |
| [string[]]$ExcludedDirs, | |
| [Parameter()] | |
| [switch]$DryRun | |
| ) | |
| robocopy "$SourcePath" "$DestinationPath" *.* ` | |
| /mir /fft /zb /sl /xo /xd $ExcludedDirs /w:5 /r:5 /unilog:"$LogPath" ` | |
| $(if ($DryRun) { "/l" } else { "" }) | |
| } | |
| $backupPathExists = Test-Path $BackupPath | |
| if ($backupPathExists) { | |
| try { | |
| Write-Host "Syncing files..." | |
| # Add or remove directories as necessary | |
| SyncFiles ` | |
| -SourcePath (Get-Item ~\Documents).FullName ` | |
| -DestinationPath "$BackupPath\Documents" ` | |
| -LogPath "$BackupPath\Documents.log" ` | |
| -ExcludedDirs "node_modules" | |
| SyncFiles ` | |
| -SourcePath (Get-Item ~\Pictures).FullName ` | |
| -DestinationPath "$BackupPath\Pictures" ` | |
| -LogPath "$BackupPath\Pictures.log" | |
| SyncFiles ` | |
| -SourcePath (Get-Item ~\Downloads).FullName ` | |
| -DestinationPath "$BackupPath\Downloads" ` | |
| -LogPath "$BackupPath\Downloads.log" ` | |
| -DryRun | |
| Write-Host "Sync completed!" | |
| } | |
| catch { | |
| Write-Host "A problem was encountered while syncing files:" | |
| Write-Host $_ | |
| } | |
| } | |
| else { | |
| Write-Host "Backup path $BackupPath can not be found." | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment