/
GcloudBackup.ps1
480 lines (363 loc) · 16.6 KB
/
GcloudBackup.ps1
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# Name: Gcloud Backup
# Author: Alex López <[email protected]>
# Contributor: Iván Blasco
# Version: 10.6
########## Var & parms declaration #####################################################
param(
[Parameter(Mandatory = $false)][switch]$all = $false,
[Parameter(Mandatory = $false)][switch]$clean = $false,
[Parameter(Mandatory = $false)][switch]$removeOld = $false,
[Parameter(Mandatory = $false)][switch]$dryRun = $false, # This will cause script to run without making any changes
[Parameter(Mandatory = $false)][switch]$unattended = $false, # Turn this flag on if script is going tu run unattended
[Parameter(Mandatory = $false)][switch]$genCreds = $false, # Generate credentials only
[Parameter(Mandatory = $false)][System.IO.FileInfo]$confFile = '.\GcloudConf.ps1' # We may indicate an alternate conf file
)
Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)
# Conf loading
try {
if (Test-Path $confFile) {
. $confFile
}
else {
Write-Host 'Configuration file not found, please reinstall!' -ForegroundColor Red -BackgroundColor Black
Write-Output 'Configuration file not found, please reinstall!' >> $errorLog
exit 1
}
# Mandatory vars check
$confError = $false
if ([string]::IsNullOrEmpty($driveLetter)) {
$confError = $true
Write-Host "Please, check your configuration file ('$confFile'). The driveLetter var must not be empty!" -ForegroundColor Red -BackgroundColor Black
Write-Output "Please, check your configuration file ('$confFile'). The driveLetter var must not be empty!" >> $errorLog
}
if ($backupPaths.Count -eq 0) {
$confError = $true
Write-Host "Please, check your configuration file ('$confFile'). The backupPaths var must contain at least one path!" -ForegroundColor Red -BackgroundColor Black
Write-Output "Please, check your configuration file ('$confFile'). The backupPaths var must must contain at least one path!" >> $errorLog
}
if ([string]::IsNullOrEmpty($serverPath)) {
$confError = $true
Write-Host "Please, check your configuration file ('$confFile'). The serverPath var must not be empty!" -ForegroundColor Red -BackgroundColor Black
Write-Output "Please, check your configuration file ('$confFile'). The serverPath var must not be empty!" >> $errorLog
}
if ($confError) {exit 1}
#######################
}
catch {
Write-Host "Please, check your configuration file ('$confFile'), there's something incorrect in it. " -ForegroundColor Red -BackgroundColor Black
Write-Output "Please, check your configuration file ('$confFile'), there's something incorrect in it. " >> $errorLog
exit 1
}
#########################################################################################
# If we're debugging we set the $DebugPreference to continue to avoid Powershell asking annoyingly if we want to continue every time it finds a "Write-Debug"
if ($PSBoundParameters['Debug']) {
$DebugPreference = 'Continue'
}
function getTime() {
return Get-Date -UFormat "%d-%m-%Y @ %H:%M"
}
function createFolder($path) {
try {
mkdir "$path" -ErrorAction Continue 2>&1> $null
}
catch {
continue
}
}
function chkCredentials($usrFile, $pwFile) {
return (Test-Path -Path $usrFile) -and (Test-Path -Path $pwFile)
}
function genCredentials($usrFile, $pwFile) {
createFolder $credDir
if ([string]::IsNullOrEmpty($usrFile) -or [string]::IsNullOrEmpty($pwFile)){
$mailCred = [ChoiceDescription]::new('&Mail', 'Credential Type: Mail')
$shareCred = [ChoiceDescription]::new('&Share', 'Credential Type: Share')
$options = [ChoiceDescription[]]($mailCred, $shareCred)
$result = $host.UI.PromptForChoice("Credential Types", "What type of credential do you wish to create?", $options, 0)
switch ($result) {
0 { genCredentials "$mailUsrFile" "$mailPwFile"}
1 { genCredentials "$shareUsrFile" "$sharePwFile" }
Default {
Write-Host "Invalid option, try again." -ForegroundColor Red -BackgroundColor Black
genCredentials
}
}
}
else {
$creds = (Get-Credential)
$creds.UserName | Out-File $usrFile
$creds.Password | ConvertFrom-SecureString | Out-File $pwFile
}
Write-Host "Credentials generated succesfully!" -ForegroundColor green -BackgroundColor black
}
function getCredentials($usrFile, $pwFile) {
$chkCredentials = chkCredentials "$usrFile" "$pwFile"
if (!$chkCredentials){
if ($unattended) {
Write-Output "Credentials ('$usrFile', '$pwFile') not found, please run this script again in interactive mode (No unattended flag activated) to generate them." 1> $credErrorLog
Write-Output "Notice that whilst you do NOT delete '$credDir' your credentials will be safely secured with Windows Data Protection API (DPAPI) which can only be used in this machine." 1>> $credErrorLog
return $false # Creds not found and running in 'unattended mode' so we cannot send the email
}
else {
Write-Host "Credentials ('$usrFile', '$pwFile') not found, please introduce your login information." -ForegroundColor Yellow -BackgroundColor Black
Write-Host "Notice that whilst you do NOT delete '$credDir' your credentials will be safely secured with Windows Data Protection API (DPAPI) which can only be used in this machine." -ForegroundColor Blue -BackgroundColor Black
genCredentials "$usrFile" "$pwFile"
}
}
return New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList (Get-Content $usrFile), (Get-Content $pwFile | ConvertTo-SecureString)
}
function mailLogs($jobType, $server, $startedTime, $endTime, $attachment) {
# Mail Setup
$cred = getCredentials "$mailUsrFile" "$mailPwFile"
if ($cred -eq $false) { return $false}
$EmailTo = $mailTo
$EmailFrom = $cred.UserName
if ($jobType -eq "upload") {
$Subject = "[Completed] Gcloud Backups - $server"
$Body = "Salutations master, <br><br>Google Cloud '$server' upload job which started at $startedTime --> Finished at $endTime<br><br>Greetings, <br><br> <strong>Your automated, Gcloud Backup script.</strong>"
}
elseif ($jobType -eq "remove") {
$Subject = "[Completed] Gcloud Backups - Removing old cloud backups"
$Body = "Salutations master, <br><br>Google Cloud 'Removing old backup files' job which started at $startedTime --> Finished at $endTime<br><br>Greetings, <br><br> <strong>Your automated, Gcloud Backup script.</strong>"
}
elseif ($jobType -eq "sendErrorLog") {
$Subject = "[Failed] Gcloud Backups - $server"
$Body = "Bad news master, <br><br>Something just broke. I attach the error file.<br><br>Greetings, <br><br> <strong>Your automated, Gcloud Backup script.</strong>"
}
# SMTP Message
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPMessage.isBodyHTML = $true
if (! [string]::IsNullOrEmpty($attachment)) {
$attachThis = new-object Net.Mail.Attachment($attachment)
$SMTPMessage.Attachments.Add($attachThis)
}
# SMTP Client Setup
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.EnableSsl = $SMTPEnableSSL
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
$SMTPClient.Send($SMTPMessage)
return $true
}
function sendErrorLog($subject, $errorLogFile) {
if (Test-Path $errorLogFile) {
$fileContents = Get-Content $errorLogFile
if (! [string]::isNullOrEmpty($fileContents)) {
$ret = mailLogs "sendErrorLog" $subject "" "" $errorLogFile
}
}
return $ret
}
function getFileName([string]$file, [string]$nameLimitator) {
# We force $file into being an string because otherwise the .IndexOf() and the .Substring() functions will fail
$limitatorPosition = $file.IndexOf($nameLimitator)
return $file.Substring($limitatorPosition)
}
function cygWinCommand($command, $ForceRun) {
if ($null -eq $ForceRun) { $ForceRun = $false }
if (Test-Path $cygWinBash -PathType Leaf)
{
if ($dryRun) # If DryRun we say what we're going to do
{
& $cygWinBash --login -c "echo Running in CygWin: $command" | Out-Host #TestRun command
}
if ($ForceRun -or !$dryRun) # If we're running in DryRun we don't run the command unless we force it
{
& $cygWinBash --login -c "$cygWinSDKPath/$command" #Run command
}
}
else
{
Write-Output "CygWin bash file doesn't exist, incorrect path."
}
}
function manageShare([string]$action) {
if ($action -eq 'mount') {
Write-Debug "manageShare(): sharePath: $sharePath"
$driveLetterRoot = (Get-PSDrive -PSProvider FileSystem -Name ("$driveLetter" -Replace ":")).DisplayRoot
Write-Debug "manageShare(): driveLetterRoot: $driveLetterRoot"
if (-not ($driveLetterRoot -eq "$sharePath")) {
$newDriveLetter = ""
Write-Debug "manageShare(): driveLetter: $driveLetter"
if (-not (Test-Path -Path "$driveLetter")) {
# If the drive letter we want to use it's not being used already, we mount it
Write-Debug "Drive letter is not being used."
$creds = getCredentials "$shareUsrFile" "$sharePwFile"
$driveLetterName = $driveLetter -Replace ":" # We need to remove the semicolon for New-PSDrive
New-PSDrive -Name $driveLetterName -PSProvider FileSystem -Root "$sharePath" -Persist -Scope Global -Credential $creds > $null
}
else {
# We find next available drive letter and we mount it
# 68 - 90 are the Unicode represented characters of letters D..Z
$usedDriveLetters = @(Get-PSDrive -PSProvider FileSystem)
$newDriveLetter = (68..90 | ForEach-Object {$L=[char]$_; if ($usedDriveLetters.Name -notContains $L) {$L}})[0]
Write-Debug "Drive letter is being used already, next available drive letter is: ${newDriveLetter}:"
$creds = getCredentials "$shareUsrFile" "$sharePwFile"
New-PSDrive -Name $newDriveLetter -PSProvider FileSystem -Root "$sharePath" -Persist -Scope Global -Credential $creds > $null
# We warn the user in the log that he might want to change the drive letter in the conf
Write-Output "Drive letter: $driveLetter is not available. You might want to change the drive letter in GcloudConf.ps1 to ${newDriveLetter}:" >> $logFile
Write-Debug "Drive letter: $driveLetter is not available. You might want to change the drive letter in GcloudConf.ps1 to ${newDriveLetter}:"
$global:driveLetter = $newDriveLetter + ":"
}
}
}
elseif ($action -eq 'unmount') {
$driveLetterName = $driveLetter -Replace ":" # We need to remove the semicolon for Remove-PSDrive
Remove-PSDrive -PSProvider FileSystem -Name "$driveLetterName" -Force -ErrorAction Continue
}
}
function autoClean() {
$currYear = Get-Date -UFormat "%Y"
$prevYear = $currYear - 1
&{
if ($dryRun) {
Write-Output "Running in 'dryRun' mode: No changes will be made."
}
$timeNow = getTime
Write-Output ("Autocleaning started at " + $timeNow)
if (!$dryRun) {
Remove-Item "$logDir\*$prevYear*"
}
$timeNow = getTime
Write-Output ("Autocleaning finished at " + $timeNow)
} 2>> $errorLog 1>> $logFile
}
function removeOldBackups() {
if($useCygWin) # We run the CygWin implementation
{
Write-Debug "Obtaining files with CygWin"
$files = @(cygWinCommand "gsutil -m ls -lR `'$serverPath`'" $true | Select-String -Pattern "\..*$" | Select-String -Pattern "TOTAL" -NotMatch)
}
else
{
Write-Debug "Obtaining files without CygWin"
$files = @(gsutil -m ls -lR "$serverPath" | Select-String -Pattern "\..*$" | Select-String -Pattern "TOTAL" -NotMatch)
}
# We filter the results to only remove the backups we're currently processing with the current $confFile we're using
$files = $files | Where-Object {[string]$file = $_; @($backupPaths | Where-Object {$file.Contains($_)}).Count -gt 0}
if (! [string]::IsNullOrEmpty($files)) {
$timeNow = getTime
$startedTime = $timeNow
Write-Output ("Removing old backup files' job started at " + $timeNow) 1>> $logFile
&{
if ($dryRun) {Write-Output "Running in 'dryRun' mode: No changes will be made."}
foreach ($file in $files) {
# We force $file into being a string cause otherwise the .trim() function below will sometimes fail
$file = [string]$file
# We trim spaces, then replace multiple spaces with one space only and then we split it into variables
$fileSize,$fileDate = (($file.trim()) -Replace '\s+', ' ').Split(' ')[0,1]
[datetime]$fileDate = Get-Date -Date $fileDate -UFormat "%Y-%m-%d"
$nameLimitator = "gs://" # We use this variable to get the name
$filePath = getFileName $file $nameLimitator
$fileName = ($filePath -Split "/")[-1]
$fileExt = ($fileName -Split "\.")[-1]
if ($fileExt -ne "vbm") {
# We skip '.vbm' files since they are always the same and don't have date on it
Write-Debug "FilePath: $filePath | FileName: $fileName | FileDate: $fileDate | LastWeekDate: $lastWeek | FileExt: $fileExt"
[datetime]$dateToday = Get-Date -UFormat "%Y-%m-%d"
$timeElapsed = New-TimeSpan -Start $fileDate -End $dateToday
if ($timeElapsed.Days -gt $daysToKeepBK) {
Write-Output "The file: '$fileName' is older than $daysToKeepBK days... Wiping out!"
# Moved dryRun down because cygWinCommand() handles $dryRun differently
if($useCygWin) # We run the CygWin implementation
{
Write-Debug "Removing with CygWin: $filePath"
cygWinCommand("gsutil -m -q rm -a ""$filePath""")
}
elseif (!$dryRun)
{
Write-Debug "Removing without CygWin: $filePath"
gsutil -m -q rm -a "$filePath" # -m makes the operation multithreaded. -q causes gsutil to be quiet, basically: No progress reporting, only errors
}
}
if ($PSBoundParameters['Debug']) {
$daysOld = $timeElapsed.Days
Write-Debug "The file: '$fileName' is $daysOld days old. So is newer than $daysToKeepBK days... Not going to wipe out!"
}
}
}
} 2>> $removeErrorLog 1> $removeLogFile
$timeNow = getTime
Write-Output ("Removing old backup files' job finished at " + $timeNow) 1>> $logFile
if ($isMailingOn) {
$isMailingOn = mailLogs "remove" "" $startedTime $timeNow $removeLogFile
}
}
else {Write-Output "Could not get the files" 1>> $errorLog}
}
function doUpload() {
# If it is a shared path, we make sure it is mounted otherwise the job will fail
if ($mountShare) { manageShare "mount" }
# We wrap all the code so we can send all the stdout and stderr to files in a single line
&{
if ($dryRun) {
Write-Output "Running in 'dryRun' mode: No changes will be made."
}
$timeNow = getTime
Write-Output ("Uploading Backups to Gcloud... Job started at " + $timeNow)
foreach ($backupPath in $backupPaths) {
$dirName = $backupPath -replace '.*\\'
$fullPath = "$driveLetter\$backupPath"
$timeNow = getTime
Write-Output ("Uploading $dirName to Gcloud... Job started at " + $timeNow)
$startedTime = $timeNow
createFolder "$logDir\$dateLogs"
Write-Debug "doUpload(): fullPath: $fullPath"
if (Test-Path $fullPath) {
if (!$dryRun) {
# Changed back to rsync because copy does copy all the files whether they are changed or not
# But now, -d option is skipped since we deal with the old backup files manually with removeOldBackups
if($useCygWin) # We run the CygWin implementation
{
$cygWinPath = $fullPath -replace "\\","/" # Convert to UNIX path
cygWinCommand("gsutil -m -q rsync -r `'$cygWinPath`' `'$serverPath/$dirName`'")
}
else
{
gsutil -m -q rsync -r "$fullPath" "$serverPath/$dirName"
}
}
}
else {
Write-Error -Message "Cannot find backup path '$fullPath'"
}
$timeNow = getTime
Write-Output ("Uploading $dirName to Gcloud... Job finished at " + $timeNow)
if ($isMailingOn) {
$isMailingOn = mailLogs "upload" $dirName $startedTime $timeNow # In case that sending email fails, we switch off the mailing option until script is restarted
}
}
$timeNow = getTime
Write-Output ("Uploading Backups to Gcloud... Job finished at " + $timeNow)
# We unmount the temporary mounted drive
if (-not ($permanentShare)) {manageShare "unmount"}
} 2>> $errorLog 1>> $logFile
if ($isMailingOn) {
$isMailingOn = sendErrorLog "Upload job" $errorLog
}
}
try {
if ($clean) {
createFolder "$logDir\$dateLogs"
autoClean
}
elseif ($removeOld) {
createFolder "$logDir\$dateLogs"
removeOldBackups
}
elseif ($All) {
createFolder "$logDir\$dateLogs"
autoClean
doUpload
removeOldBackups
}
elseif ($genCreds) {
genCredentials
}
else {
createFolder "$logDir\$dateLogs"
doUpload
}
}
catch [System.IO.DirectoryNotFoundException] {
Write-Host 'Please, check that file paths are well configured' -ForegroundColor Red -BackgroundColor Black
}