mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-05-07 17:14:39 +02:00
Update sync-folder.ps1
This commit is contained in:
parent
b455a05458
commit
a9e08a6c3d
@ -1,31 +1,43 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Sync's two folders
|
Syncronizes two folders
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
This PowerShell script synchronizes two folders via Robocopy (useful for e.g. backups).
|
This PowerShell script synchronizes (mirrors) the content of 2 directory trees by using Robocopy.
|
||||||
NOTE: Make sure the target folder is correct because the content gets replaced!
|
Typical use cases are backups: at first everything is copied (full backup), afterward only changes are copied (incremental backup).
|
||||||
.PARAMETER Source
|
IMPORTANT NOTE: Make sure the target path is correct because the content gets replaced (DATA LOSS)!
|
||||||
Path to the source folder
|
.PARAMETER sourcePath
|
||||||
.PARAMETER Destination
|
Specifies the path to the source folder
|
||||||
Path to the destination folder
|
.PARAMETER targetPath
|
||||||
|
Specifies the path to the target folder
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
sync-folder -source C:\Folder01 -destination C:\Folder02
|
PS> ./sync-folder.ps1 C:\MyPhotos D:\Backups\MyPhotos
|
||||||
|
.LINK
|
||||||
|
https://github.com/fleschutz/PowerShell
|
||||||
|
.NOTES
|
||||||
|
Author: Markus Fleschutz | License: CC0
|
||||||
#>
|
#>
|
||||||
|
|
||||||
function Sync-Folder
|
param([string]$sourcePath = "", [string]$targetPath = "")
|
||||||
{
|
|
||||||
|
|
||||||
[CmdletBinding()]
|
try {
|
||||||
param (
|
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()
|
||||||
|
|
||||||
[Parameter(Mandatory)][string]$Source,
|
$robocopyParameters = $sourcePath, $targetPath, '/MIR', '/FFT', '/NDL', '/NP', '/NS'
|
||||||
[Parameter(Mandatory)][string]$Destination
|
# /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
|
||||||
|
|
||||||
$RobocopyParams = $Source, $Destination, '/MIR', '/FFT', '/NDL', '/NP', '/NS'
|
robocopy.exe $robocopyParameters
|
||||||
|
if ($lastExitCode -gt 3) { throw 'Robocopy failed.' }
|
||||||
robocopy.exe $RobocopyParams
|
|
||||||
|
|
||||||
if ($LastExitCode -gt 3) { throw 'Robocopy failed.' }
|
|
||||||
|
|
||||||
|
[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
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user