-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackupDir.ps1
More file actions
246 lines (217 loc) · 8.53 KB
/
BackupDir.ps1
File metadata and controls
246 lines (217 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# BackupDir.ps1 - Refined and Robustified
param(
[Parameter(Mandatory=$true, Position=0)]
[string]$SourceDir,
[Parameter(Mandatory=$true, Position=1)]
[string]$DestDir,
[switch]$InPlace,
[switch]$OverwriteOlder,
[switch]$KeepNonexistent,
[switch]$DryRun,
[int]$NumCopies = 0,
[int]$MinDigits = 2,
[switch]$Execute,
[switch]$IsVerbose
)
function Get-CanonicalAbsolutePath {
param([string]$Path)
if ([System.IO.Path]::IsPathRooted($Path)) {
return [System.IO.Path]::GetFullPath($Path)
} else {
return [System.IO.Path]::GetFullPath((Join-Path -Path (Get-Location) -ChildPath $Path))
}
}
function Get-CopyName {
param(
[string]$BaseName,
[int]$Index,
[int]$Digits
)
return "$BaseName" + "_" + $Index.ToString("D$Digits")
}
function Remove-ExcessCopies {
param(
[string]$ParentDir,
[string]$BaseName,
[int]$NumCopies,
[int]$MinDigits,
[bool]$DryRun,
[bool]$VerboseMode
)
$maxExisting = 0
while ($true) {
$testName = Get-CopyName -BaseName $BaseName -Index ($maxExisting + 1) -Digits $MinDigits
$testPath = Join-Path $ParentDir $testName
if (Test-Path $testPath) {
$maxExisting++
} else {
break
}
}
for ($i = $NumCopies + 1; $i -le $maxExisting; $i++) {
$redundantName = Get-CopyName -BaseName $BaseName -Index $i -Digits $MinDigits
$redundantPath = Join-Path $ParentDir $redundantName
if (Test-Path $redundantPath) {
if ($VerboseMode) { Write-Output "Removing redundant backup copy: $redundantPath" }
if (-not $DryRun) {
Remove-Item -Path $redundantPath -Recurse -Force
}
}
}
}
function Copy-DirectoryRecursive {
param(
[string]$Source,
[string]$Destination,
[bool]$OverwriteOlder,
[bool]$KeepNonexistent,
[bool]$DryRun,
[bool]$VerboseMode
)
if (-not $DryRun -and -not (Test-Path -Path $Destination)) {
if ($VerboseMode) { Write-Output "Creating destination root: $Destination" }
New-Item -Path $Destination -ItemType Directory -Force | Out-Null
}
Get-ChildItem -Path $Source -Recurse -Force | ForEach-Object {
$relativePath = $_.FullName.Substring($Source.Length).TrimStart('\')
$targetPath = Join-Path -Path $Destination -ChildPath $relativePath
if ($_.PSIsContainer) {
if (-not (Test-Path $targetPath)) {
if ($VerboseMode) { Write-Output "Creating directory: $targetPath" }
if (-not $DryRun) {
New-Item -ItemType Directory -Path $targetPath -Force | Out-Null
}
}
} else {
$copyFile = $true
if ((Test-Path $targetPath) -and $OverwriteOlder) {
$srcTime = $_.LastWriteTime
$dstItem = Get-Item $targetPath
$dstTime = $dstItem.LastWriteTime
$srcSize = $_.Length
$dstSize = $dstItem.Length
if (($srcTime -eq $dstTime -and $srcSize -eq $dstSize) -or ($dstTime -gt $srcTime)) {
$copyFile = $false
}
}
if ($copyFile) {
if ($VerboseMode) { Write-Output "Copying file: $_ -> $targetPath" }
if (-not $DryRun) {
Copy-Item $_.FullName -Destination $targetPath -Force
}
}
}
}
if (-not $KeepNonexistent) {
Get-ChildItem -Path $Destination -Recurse -Force | ForEach-Object {
$relativePath = $_.FullName.Substring($Destination.Length).TrimStart('\')
$sourcePath = Join-Path -Path $Source -ChildPath $relativePath
if (-not (Test-Path $sourcePath)) {
if ($VerboseMode) { Write-Output "Removing extra item: $_" }
if (-not $DryRun) {
Remove-Item -Path $_.FullName -Force -Recurse
}
}
}
}
}
function Perform-InPlaceCopy {
param(
[string]$SourceDir,
[string]$TargetDir,
[bool]$OverwriteOlder,
[bool]$KeepNonexistent,
[bool]$DryRun,
[bool]$VerboseMode
)
if ($VerboseMode) { Write-Output "Starting in-place copy..." }
Copy-DirectoryRecursive -Source $SourceDir -Destination $TargetDir -OverwriteOlder:$OverwriteOlder -KeepNonexistent:$KeepNonexistent -DryRun:$DryRun -VerboseMode:$VerboseMode
}
function Perform-CompleteCopy {
param(
[string]$SourceDir,
[string]$TargetDir,
[int]$NumCopies,
[int]$MinDigits,
[bool]$DryRun,
[bool]$VerboseMode
)
$dirBase = Split-Path -Leaf $TargetDir
$parentDir = Split-Path -Parent $TargetDir
Remove-ExcessCopies -ParentDir $parentDir -BaseName $dirBase -NumCopies $NumCopies -MinDigits $MinDigits -DryRun:$DryRun -VerboseMode:$VerboseMode
for ($i = $NumCopies; $i -ge 1; $i--) {
$srcName = if ($i -eq 1) { $dirBase } else { Get-CopyName -BaseName $dirBase -Index ($i - 1) -Digits $MinDigits }
$dstName = Get-CopyName -BaseName $dirBase -Index $i -Digits $MinDigits
$srcPath = Join-Path $parentDir $srcName
$dstPath = Join-Path $parentDir $dstName
if ((Test-Path $dstPath) -and (-not $DryRun)) {
Remove-Item -Path $dstPath -Recurse -Force
}
if (Test-Path $srcPath) {
if ($VerboseMode) { Write-Output "Renaming $srcPath -> $dstPath" }
if (-not $DryRun) {
Rename-Item -Path $srcPath -NewName (Split-Path -Leaf $dstPath) -Force
}
}
}
if ($VerboseMode) { Write-Output "Copying fresh source to $TargetDir" }
Copy-DirectoryRecursive -Source $SourceDir -Destination $TargetDir -OverwriteOlder:$false -KeepNonexistent:$false -DryRun:$DryRun -VerboseMode:$VerboseMode
Remove-ExcessCopies -ParentDir $parentDir -BaseName $dirBase -NumCopies $NumCopies -MinDigits $MinDigits -DryRun:$DryRun -VerboseMode:$VerboseMode
}
function BackupDir {
param(
[string]$SourceDir,
[string]$DestDir,
[bool]$InPlace,
[bool]$OverwriteOlder,
[bool]$KeepNonexistent,
[bool]$DryRun,
[int]$NumCopies,
[int]$MinDigits,
[bool]$VerboseMode
)
$SourceDirAbs = Get-CanonicalAbsolutePath $SourceDir
$DestParentDirAbs = Get-CanonicalAbsolutePath $DestDir
$BackupName = Split-Path -Leaf $SourceDirAbs
$BackupDestDir = Join-Path $DestParentDirAbs $BackupName
if (-not (Test-Path -Path $SourceDirAbs)) {
Write-Error "Source directory does not exist: $SourceDirAbs"
return
}
if ($VerboseMode) {
Write-Output "BackupDir script invoked."
Write-Output "Parameters:"
Write-Output " SourceDir: $SourceDirAbs"
Write-Output " DestDir (Parent): $DestParentDirAbs"
Write-Output " Final TargetDir: $BackupDestDir"
Write-Output " InPlace: $InPlace"
Write-Output " OverwriteOlder: $OverwriteOlder"
Write-Output " KeepNonexistent: $KeepNonexistent"
Write-Output " DryRun: $DryRun"
Write-Output " NumCopies: $NumCopies"
Write-Output " MinDigits: $MinDigits"
}
if ($InPlace) {
Perform-InPlaceCopy -SourceDir $SourceDirAbs -TargetDir $BackupDestDir -OverwriteOlder:$OverwriteOlder -KeepNonexistent:$KeepNonexistent -DryRun:$DryRun -VerboseMode:$VerboseMode
} else {
Perform-CompleteCopy -SourceDir $SourceDirAbs -TargetDir $BackupDestDir -NumCopies:$NumCopies -MinDigits:$MinDigits -DryRun:$DryRun -VerboseMode:$VerboseMode
}
}
if ($IsVerbose) {
Write-Output "`nScript input parameters:"
Write-Output " SourceDir: $SourceDir"
Write-Output " DestDir: $DestDir"
Write-Output " InPlace: $InPlace"
Write-Output " OverwriteOlder: $OverwriteOlder"
Write-Output " KeepNonexistent: $KeepNonexistent"
Write-Output " DryRun: $DryRun"
Write-Output " NumCopies: $NumCopies"
Write-Output " MinDigits: $MinDigits"
Write-Output " Execute: $Execute"
Write-Output " IsVerbose: $IsVerbose"
}
if ($Execute -or ($PSBoundParameters.ContainsKey('Execute') -eq $false -and $SourceDir -and $DestDir)) {
BackupDir -SourceDir $SourceDir -DestDir $DestDir -InPlace:$InPlace -OverwriteOlder:$OverwriteOlder -KeepNonexistent:$KeepNonexistent -DryRun:$DryRun -NumCopies:$NumCopies -MinDigits:$MinDigits -VerboseMode:$IsVerbose
} else {
if ($IsVerbose) { Write-Output "BackupDir function defined but not executed (Execute=$Execute)." }
}