Skip to content

Instantly share code, notes, and snippets.

@jimdiroffii
Created July 27, 2023 18:33
Show Gist options
  • Save jimdiroffii/c0a5d9eaa72f6207bcab035fb0bb15db to your computer and use it in GitHub Desktop.
Save jimdiroffii/c0a5d9eaa72f6207bcab035fb0bb15db to your computer and use it in GitHub Desktop.
Local Network Backup with PowerShell
# Define the variables
# The server or root directory
$localserver = "\\<localserver>"
# Share
$share = "\share"
# Source data folders you wish to backup
$dataFolders = @("\backups", "\pics") # You can add more folders to this array if needed
# Destination
$destinationRoot = ".\b1"
# Function to perform the robocopy
function PerformBackup($source, $destination) {
robocopy $source $destination /MIR /Z /W:5 /R:5
if ($? -eq $false) {
Write-Host "Error occurred while copying $source" -ForegroundColor Red
} else {
Write-Host "$source copied successfully!" -ForegroundColor Green
}
}
# Main script logic
foreach ($folder in $dataFolders) {
$sourcePath = $localserver + $share + $folder
$destinationPath = Join-Path -Path $destinationRoot -ChildPath $folder.TrimStart("\")
PerformBackup -source $sourcePath -destination $destinationPath
}
Write-Host "Backup complete!" -ForegroundColor Cyan
@jimdiroffii
Copy link
Author

Switch the robocopy command from \MIR to \E to prevent any deletion on the destination side, and still copy empty directories. Otherwise, this will perform a synchronization and delete anything that was removed from the source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment