-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLozBackupModule.psm1
More file actions
363 lines (298 loc) · 13.9 KB
/
Copy pathLozBackupModule.psm1
File metadata and controls
363 lines (298 loc) · 13.9 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
function Get-RunBackup
{
param
(
$InformationPreference = "Continue",
$WarningPreference = "Continue",
[Int]$BackupOperation = $null,
[String]$PathStatementBackup = "",
[Object[][]]$TotalArrayBackup = @(),
$SourceArrayBackup = @(),
$DestinationArrayBackup = @(),
[Int]$FinalCount = $null
)
#Write-Host "Function: Get-RunBackup | BackupOperation: $BackupOperation | PathStatementBackup: $PathStatementBackup | TotalArrayBackup: $($TotalArrayBackup | ForEach-Object { $_ -join ', ' }) | SourceArrayBackup: $SourceArrayBackup | DestinationArrayBackup: $DestinationArrayBackup | FinalCount: $FinalCount" -ForegroundColor Green
function Backup-DataOne
{
[Int]$BaseCount = 0
[Int]$UserInputLoop = 0
[Int]$WriteOutCount = $BaseCount + 1
while ($BaseCount -lt $FinalCount)
{
$FinalSourceDir = $SourceArrayBackup[$BaseCount]
$FinalDestDir = $DestinationArrayBackup[$BaseCount]
if (((($FinalSourceDir.StartsWith('"') -and $FinalSourceDir.EndsWith('"')) -or (($FinalSourceDir.StartsWith('"')) -or ($FinalSourceDir.EndsWith('"')))) -or ($FinalSourceDir.StartsWith("'") -and $FinalSourceDir.EndsWith("'"))) -or (($FinalSourceDir.StartsWith("'")) -or ($FinalSourceDir.EndsWith("'"))))
{
$FinalSourceDir = $FinalSourceDir.Trim('"', "'" )
}
if (((($FinalDestDir.StartsWith('"') -and $FinalDestDir.EndsWith('"')) -or (($FinalDestDir.StartsWith('"')) -or ($FinalDestDir.EndsWith('"')))) -or ($FinalDestDir.StartsWith("'") -and $FinalDestDir.EndsWith("'"))) -or (($FinalDestDir.StartsWith("'")) -or ($FinalDestDir.EndsWith("'"))))
{
$FinalDestDir = $FinalDestDir.Trim('"', "'" )
}
[Bool]$TestTheSource = Test-Path -Path $FinalSourceDir
if ($TestTheSource -eq $false)
{
[Int]$UserInputLoop = 0
while ($UserInputLoop -eq 0)
{
[Console]::ForegroundColor = [ConsoleColor]::Yellow
Write-Information -MessageData "Backup $WriteOutCount. $FinalSourceDir does not exist!"
Write-Information -MessageData "Do you want to create this directory?"
[Console]::ResetColor()
Write-Information -MessageData "1. Yes"
Write-Information -MessageData "2. No"
$UserInputLoop = Get-UserInputBackup
}
[Int]$MakeDirChoice = $UserInputLoop
if ($MakeDirChoice -eq 1)
{
New-Item -Path $FinalSourceDir -ItemType Directory -Force
}
elseif ($MakeDirChoice -eq 2)
{
[Console]::ForegroundColor = [ConsoleColor]::Red
Write-Information -MessageData "Backup $WriteOutCount. $FinalSourceDir to $FinalDestDir failed!"
[Console]::ResetColor()
Write-Information -MessageData ""
$BaseCount = $BaseCount + 1
$WriteOutCount = $WriteOutCount + 1
continue
}
else
{
Invoke-ReadWrite -OperationChoice 1 -PathStatementReadWrite $PathStatementBackup -LogType 3 -Message "(54)A. Parent Function: Get-RunBackup | Child Function: Backup-DataOne | Variable MakeDirChoice: $MakeDirChoice (Should equal 1 or 2)"
Exit-CaTScheduler
Exit
}
}
[Bool]$TestTheDestination = Test-Path -Path $FinalDestDir
if ($TestTheDestination -eq $false)
{
[Int]$UserInputLoop = 0
while ($UserInputLoop -eq 0)
{
[Console]::ForegroundColor = [ConsoleColor]::Yellow
Write-Information -MessageData "Backup $WriteOutCount. $FinalDestDir does not exist!"
Write-Information -MessageData "Do you want to create this directory?"
[Console]::ResetColor()
Write-Information -MessageData "1. Yes"
Write-Information -MessageData "2. No"
$UserInputLoop = Get-UserInputBackup
}
[Int]$MakeDirChoice = $UserInputLoop
if ($MakeDirChoice -eq 1)
{
New-Item -Path $FinalDestDir -ItemType Directory -Force
}
elseif ($MakeDirChoice -eq 2)
{
[Console]::ForegroundColor = [ConsoleColor]::Red
Write-Information -MessageData "Backup $WriteOutCount. $FinalSourceDir to $FinalDestDir failed!"
[Console]::ResetColor()
Write-Information -MessageData ""
$BaseCount = $BaseCount + 1
$WriteOutCount = $WriteOutCount + 1
continue
}
else
{
Invoke-ReadWrite -OperationChoice 1 -PathStatementReadWrite $PathStatementBackup -LogType 3 -Message "(54)B. Parent Function: Get-RunBackup | Child Function: Backup-DataOne | Variable MakeDirChoice: $MakeDirChoice (Should equal 1 or 2)"
Exit-CaTScheduler
Exit
}
}
$SourceItems = Get-ChildItem -Path $FinalSourceDir -Recurse
$DestItems = Get-ChildItem -Path $FinalDestDir -Recurse
$DestItemLookup = @{}
foreach ($Item in $DestItems)
{
$RelativePath = Get-RelativePath -BasePath $FinalDestDir -FullPath $Item.FullName
$DestItemLookup[$RelativePath] = $Item
}
foreach ($SourceItem in $SourceItems)
{
$RelativePath = Get-RelativePath -BasePath $FinalSourceDir -FullPath $SourceItem.FullName
if ($SourceItem.PSIsContainer)
{
if (-not $DestItemLookup.ContainsKey($RelativePath))
{
$DestinationPath = Join-Path -Path $FinalDestDir -ChildPath $RelativePath
New-Item -Path $DestinationPath -ItemType Directory -Force
Write-BackupLog -LogTypeX 1 -MessageX "(One time backup) The directory $DestinationPath was created within $FinalDestDir in order to complete the backup of $FinalSourceDir to $FinalDestDir"
}
}
else
{
if ($DestItemLookup.ContainsKey($RelativePath))
{
$DestItem = $DestItemLookup[$RelativePath]
$SourceHash = Get-FileHash -Path $SourceItem.FullName
$DestHash = Get-FileHash -Path $DestItem.FullName
if ($SourceHash.Hash -ne $DestHash.Hash)
{
Copy-Item -Path $SourceItem.FullName -Destination $DestItem.FullName -Force
Write-BackupLog -LogTypeX 1 -MessageX "(One time backup) The file $($SourceItem.FullName) was copied to $($DestItem.FullName) in order to complete the backup of $FinalSourceDir to $FinalDestDir"
}
}
else
{
$DestinationPath = Join-Path -Path $FinalDestDir -ChildPath $RelativePath
$DestinationDir = Split-Path -Path $DestinationPath -Parent
if (-not (Test-Path -Path $DestinationDir))
{
New-Item -Path $DestinationDir -ItemType Directory -Force
Write-BackupLog -LogTypeX 1 -MessageX "(One time backup) The directory $DestinationDir was created in order to complete the backup of $FinalSourceDir to $FinalDestDir"
}
Copy-Item -Path $SourceItem.FullName -Destination $DestinationPath -Force
Write-BackupLog -LogTypeX 1 -MessageX "(One time backup) The file $($SourceItem.FullName) was copied to $DestinationPath in order to complete the backup of $FinalSourceDir to $FinalDestDir"
}
}
}
[Console]::ForegroundColor = [ConsoleColor]::Cyan
Write-Information -MessageData "Backup $WriteOutCount. $FinalSourceDir to $FinalDestDir complete!"
[Console]::ResetColor()
Write-Information -MessageData ""
$BaseCount = $BaseCount + 1
$WriteOutCount = $WriteOutCount + 1
}
return $true
}
function Get-RelativePath
{
param
(
[string]$BasePath,
[string]$FullPath
)
return $FullPath.Substring($BasePath.Length).TrimStart("\")
}
function Write-BackupLog
{
param
(
[String]$MessageX = "",
[Int]$LogTypeX = $null
)
$LogFileOneX = "$PathStatementBackup\PowerLozLogs\LozInfoLog.log"
$TimestampX = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
if ($LogTypeX -eq 1)
{
Add-Content -Path $LogFileOneX -Value "INFO: $TimestampX"
Add-Content -Path $LogFileOneX -Value "$MessageX"
Add-Content -Path $LogFileOneX -Value " "
return $true
}
else
{
Invoke-ReadWrite -OperationChoice 1 -PathStatementReadWrite $PathStatementBackup -LogType 3 -Message "(55)A. Parent Function: Get-RunBackup | Child Function: Write-BackupLog | Variable LogTypeX: $LogTypeX (Should equal 1)"
Exit-CaTScheduler
Exit
}
}
function Get-UserInputBackup
{
[String]$WarningTen = "The input given was not valid. The options are 1 or 2."
$UserChoiceXX = Read-Host "Choice"
Write-Information -MessageData ""
if ($UserChoiceXX -eq "")
{
$WarningTen | Write-Warning
Write-Information -MessageData ""
return 0
}
[Bool]$IsItAnIntegerBackup = [Int]::TryParse($UserChoiceXX, [ref]$null)
if (-not $IsItAnIntegerBackup)
{
$WarningTen | Write-Warning
Write-Information -MessageData ""
return 0
}
try
{
[Int]$UserChoiceX = [Int]$UserChoiceXX
}
catch [System.Management.Automation.RuntimeException]
{
if ($_.Exception.Message -like "*Input string was not in a correct format.*")
{
$WarningTen | Write-Warning
Write-Information -MessageData ""
return 0
}
}
if (($UserChoiceX -eq 1) -or ($UserChoiceX -eq 2))
{
[Int]$ChosenUserChoice = $UserChoiceX
return $ChosenUserChoice
}
elseif ($UserInputX -eq 0)
{
$WarningTen | Write-Warning
Write-Information -MessageData ""
return 0
}
elseif (($UserChoiceX -lt 0) -or ($UserChoiceX -gt 2))
{
$WarningTen | Write-Warning
Write-Information -MessageData ""
return 0
}
else
{
$WarningTen | Write-Warning
Write-Information -MessageData ""
return 0
}
}
if ($BackupOperation -eq 1)
{
$SourceArrayBackup = $TotalArrayBackup[0]
$DestinationArrayBackup = $TotalArrayBackup[1]
[Int]$SourceCount = $SourceArrayBackup.Count
[Int]$DestCount = $DestinationArrayBackup.Count
if ($SourceCount -ne $DestCount)
{
Invoke-ReadWrite -OperationChoice 1 -PathStatementReadWrite $PathStatementBackup -LogType 3 -Message "(58)A. Parent Function: Get-RunBackup | Variable BackupOperation: $BackupOperation (Should equal 1)"
Exit-CaTScheduler
Exit
}
elseif ($SourceCount -eq $DestCount)
{
$FinalCount = $SourceCount
}
else
{
Invoke-ReadWrite -OperationChoice 1 -PathStatementReadWrite $PathStatementBackup -LogType 3 -Message "(58)B. Parent Function: Get-RunBackup | Variable BackupOperation: $BackupOperation (Should equal 1)"
Exit-CaTScheduler
Exit
}
[Bool]$IsBackupGood = Backup-DataOne
if ($IsBackupGood -eq $true)
{
Write-Information -MessageData "All backups complete!"
Write-Information -MessageData ""
Start-CaTScheduler -PathStatementStartup $PathStatementBackup -Start 1
}
elseif (-not ($IsBackupGood -eq $true))
{
[Console]::ForegroundColor = [ConsoleColor]::Red
Write-Information -MessageData "Backup(s) failed!"
[Console]::ResetColor()
Write-Information -MessageData ""
Start-CaTScheduler -PathStatementStartup $PathStatementBackup -Start 1
}
else
{
Invoke-ReadWrite -OperationChoice 1 -PathStatementReadWrite $PathStatementBackup -LogType 3 -Message "(56)A. Parent Function: Get-RunBackup | Variable BackupOperation: $BackupOperation (Should equal 1)"
Exit-CaTScheduler
Exit
}
}
else
{
Invoke-ReadWrite -OperationChoice 1 -PathStatementReadWrite $PathStatementBackup -LogType 3 -Message "(57)A. Parent Function: Get-RunBackup | Variable BackupOperation: $BackupOperation (Should not equal 1)"
Exit-CaTScheduler
Exit
}
}
Export-ModuleMember -Function Get-RunBackup