PowerShell/scripts/sync-folder.ps1

43 lines
1.6 KiB
PowerShell
Raw Normal View History

2023-11-22 12:03:42 +01:00
<#
2023-11-22 07:27:21 +01:00
.SYNOPSIS
2023-11-22 12:03:42 +01:00
Syncronizes two folders
2023-11-22 07:27:21 +01:00
.DESCRIPTION
2023-11-22 12:03:42 +01:00
This PowerShell script synchronizes (mirrors) the content of 2 directory trees by using Robocopy.
Typical use cases are backups: at first everything is copied (full backup), afterward only changes are copied (incremental backup).
IMPORTANT NOTE: Make sure the target path is correct because the content gets replaced (DATA LOSS)!
.PARAMETER sourcePath
Specifies the path to the source folder
.PARAMETER targetPath
Specifies the path to the target folder
2023-11-22 07:27:21 +01:00
.EXAMPLE
2023-11-22 12:03:42 +01:00
PS> ./sync-folder.ps1 C:\MyPhotos D:\Backups\MyPhotos
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
2023-11-22 07:27:21 +01:00
#>
2023-11-22 12:03:42 +01:00
param([string]$sourcePath = "", [string]$targetPath = "")
2023-11-22 07:27:21 +01:00
2023-11-22 12:03:42 +01:00
try {
if ($sourcePath -eq "") { $sourcePath = Read-Host "Enter the path to the source folder" }
if ($targetPath -eq "") { $targetPath = Read-Host "Enter the path to the target folder" }
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2023-11-22 07:27:21 +01:00
2023-11-22 12:03:42 +01:00
$robocopyParameters = $sourcePath, $targetPath, '/MIR', '/FFT', '/NDL', '/NP', '/NS'
# /MIR = mirror a directory tree
# /FFT = assume FAT file times (2-second granularity)
# /NDL = don't log directory names
# /NP = don't display percentage copied
# /NS = don't log file sizes
2023-11-22 07:27:21 +01:00
2023-11-22 12:03:42 +01:00
robocopy.exe $robocopyParameters
if ($lastExitCode -gt 3) { throw 'Robocopy failed.' }
2023-11-22 07:27:21 +01:00
2023-11-22 12:03:42 +01:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✔️ Synced 📂$sourcePath to 📂$targetPath in $elapsed sec"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2023-11-22 07:27:21 +01:00
}