PowerShell/docs/sync-folder.md

100 lines
3.1 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *sync-folder.ps1*
========================
2023-12-07 20:24:45 +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)!
Parameters
----------
```powershell
PS> ./sync-folder.ps1 [[-sourcePath] <String>] [[-targetPath] <String>] [<CommonParameters>]
-sourcePath <String>
Specifies the path to the source folder
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-targetPath <String>
Specifies the path to the target folder
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
Example
-------
```powershell
PS> ./sync-folder.ps1 C:\MyPhotos D:\Backups\MyPhotos
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
Syncronizes two folders
.DESCRIPTION
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
.EXAMPLE
PS> ./sync-folder.ps1 C:\MyPhotos D:\Backups\MyPhotos
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$sourcePath = "", [string]$targetPath = "")
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()
$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
robocopy.exe $robocopyParameters
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
}
```
2024-08-15 09:51:46 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of sync-folder.ps1 as of 08/15/2024 09:50:54)*