Created
July 27, 2023 18:33
-
-
Save jimdiroffii/c0a5d9eaa72f6207bcab035fb0bb15db to your computer and use it in GitHub Desktop.
Local Network Backup with PowerShell
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.