-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ResticGameSnapshots.ps1
753 lines (637 loc) · 28.8 KB
/
Get-ResticGameSnapshots.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
<#
.SYNOPSIS
List the snapshots of a game in Restic
.DESCRIPTION
Ask you the game you want to see snapshots in Restic (get them from Restic directly).
After your choice, show you all the information and details about the snapshots
.PARAMETER Game
Set directly the game name to filter without the script ask you the game.
.PARAMETER CountOnly
Show you the game list with the number of snapshots only
.PARAMETER Listing
Show snapshots list and stop
.OUTPUTS
Logs actions in case of crash
.EXAMPLE
.\Get-ResticGameSnapshots.ps1
.NOTES
Name : Get-ResticGameSapshots
Version : 3.0-Beta.2
Created by : Chucky2401
Date Created : 25/07/2022
Modify by : Chucky2401
Date modified : 19/05/2023
Change : Exclude parameter
.LINK
https://github.com/Chucky2401/Restic-Scripts/blob/main/README.md#get-resticgamesnapshots
#>
#---------------------------------------------------------[Script Parameters]------------------------------------------------------
Using namespace System.Management.Automation.Host
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Low", DefaultParameterSetName = 'None')]
Param (
# Parameter help description
[Parameter(Mandatory = $False, ParameterSetName = "GameName")]
[ValidateNotNullOrEmpty()]
[Alias("g")]
[String]$Game,
[Parameter(Mandatory = $False, ParameterSetName = "None")]
[Parameter(Mandatory = $False, ParameterSetName = "GameName")]
[Alias("l")]
[Switch]$Listing,
[Parameter(Mandatory = $False, ParameterSetName = "Count")]
[Alias("c")]
[Switch]$CountOnly
)
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$global:ErrorActionPreference = "Stop"
$global:DebugPreference = 'SilentlyContinue'
If ($PSBoundParameters['Debug']) {
$global:DebugPreference = 'Continue'
}
Update-FormatData -AppendPath "$($PSScriptRoot)\inc\format\ResticControl.format.ps1xml"
$PSStyle.Progress.MaxWidth = ($Host.UI.RawUI.WindowSize.Width)
Import-Module -Name ".\inc\modules\Tjvs.Settings"
Import-Module -Name ".\inc\modules\Tjvs.Message", ".\inc\modules\Tjvs.Process", ".\inc\modules\Tjvs.Restic"
#Set-PowerShellUICulture en-US
Import-LocalizedData -BindingVariable "Message" -BaseDirectory "local" -FileName "Get-ResticGameSnapshots.psd1"
#-----------------------------------------------------------[Functions]------------------------------------------------------------
#TODO: Help header
function Read-GameChoice {
<#
.SYNOPSIS
Summary of the script
.DESCRIPTION
Script description
.PARAMETER param1
Parameter description
.INPUTS
Pipeline input data
.OUTPUTS
Output data
.EXAMPLE
.\template.ps1 param1
.NOTES
Name : Script-Name
Version : 1.0.0.1
Created by : Chucky2401
Date Created : 27/07/2022
Modify by : Chucky2401
Date modified : 27/07/2022
Change : Creation
Copy : Copy-Item .\Script-Name.ps1 \Final\Path\Script-Name.ps1 -Force
.LINK
http://github.com/UserName/RepoName
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String[]]$Choices,
[System.String]$Title = [string]::Empty,
[System.String]$Message
)
$selection = -1
$bFirstLoop = $True
$nConsoleWidth = (Get-Host).UI.RawUI.MaxWindowSize.Width
$nMiddle = [Math]::Ceiling($Choices.Count/2)
$nSubstract = $nMiddle+1
$aLines = @()
$nGreaterSnapshotsCount = ($htGameSnapshotsCount.GetEnumerator() | Select-Object Value | Measure-Object -Property Value -Maximum).Maximum.ToString().Length
If ($Title -ne [String]::Empty) {
Write-Host "`n`n"
Write-CenterText "===== $($Title) ====="
Write-Host "`n"
}
$counter = 1
$Choices | ForEach-Object {
$sGame = $PSItem
$nSnapshots = $htGameSnapshotsCount[$sGame]
$sString = "[$(($counter).ToString())] $($sGame) ($($nSnapshots))"
If ($counter -le $nMiddle) {
$aLines += $sString.PadRight(($nConsoleWidth/3)+$nGreaterSnapshotsCount, " ")
} Else {
$nIndice = $counter-$nSubstract
$aLines[$nIndice] += $sString
}
$counter++
}
$aLines | ForEach-Object {
Write-Host $PSItem
}
ShowMessage "OTHER" ""
do {
If ($bFirstLoop) {
$bFirstLoop = $False
} Else {
Write-Host $Script:Message.Err_GameChoice -ForegroundColor Red
}
$inputValue = Read-Host -Prompt $Message
If ($inputValue -eq "q") {
Return $inputValue
}
Try {
$selection = [int]$inputValue
} Catch {
$selection = -1
}
} While ($selection -lt 1 -or $selection -gt $Choices.Count)
Return $selection-1
}
#TODO: Help header
function Read-SnapshotChoice {
<#
.SYNOPSIS
Summary of the script
.DESCRIPTION
Script description
.PARAMETER param1
Parameter description
.INPUTS
Pipeline input data
.OUTPUTS
Output data
.EXAMPLE
.\template.ps1 param1
.NOTES
Name : Script-Name
Version : 1.0.0.1
Created by : Chucky2401
Date Created : 27/07/2022
Modify by : Chucky2401
Date modified : 27/07/2022
Change : Creation
Copy : Copy-Item .\Script-Name.ps1 \Final\Path\Script-Name.ps1 -Force
.LINK
http://github.com/UserName/RepoName
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Object[]]$Choices,
[System.String]$Title = [string]::Empty,
[System.String]$Message
)
$selection = -1
$bFirstLoop = $True
$iNoChoices = $Choices.Count
If ($Title -ne [String]::Empty) {
Write-Host "`n`n"
Write-CenterText "===== $($Title) ====="
Write-Host "`n"
}
$counter = 1
$aTags = @()
$Choices | ForEach-Object {
If ($null -eq $PSItem.Tags) {
$PSItem.Tags = ""
} ElseIf ($PSItem.Tags.GetType().Name -eq "Object[]") {
$PSItem.Tags | ForEach-Object { $aTags += $PSItem }
$PSItem.Tags = [String]::Join(";", $aTags)
} Else {
$PSItem.Tags = $PSItem.Tags
}
}
$iMaxLengthNumber = If ($iNoChoices.ToString().Length -gt 3) { $iNoChoices.ToString().Length } Else { 3 }
$iMaxLengthShortId = ((($Choices.ShortId | Where-Object { $null -ne $PSItem }) | Measure-Object -Maximum -Property Length).Maximum, (("ShortId" | Where-Object { $null -ne $PSItem }) | Measure-Object -Maximum -Property Length).Maximum | Measure-Object -Maximum).Maximum
$iMaxLengthDateTime = ((($Choices.DateTime | Where-Object { $null -ne $PSItem }) | ForEach-Object { $PSItem.ToString() } | Measure-Object -Maximum -Property Length).Maximum, (("DateTime" | Where-Object { $null -ne $PSItem }) | ForEach-Object { $PSItem.ToString() } | Measure-Object -Maximum -Property Length).Maximum | Measure-Object -Maximum).Maximum
$iMaxLengthTags = ((($Choices.Tags | Where-Object { $null -ne $PSItem }) | Measure-Object -Maximum -Property Length).Maximum, ((("Tags") | Where-Object { $null -ne $PSItem }) | Measure-Object -Maximum -Property Length).Maximum | Measure-Object -Maximum).Maximum
$iMaxLengthTotalFileBackup = ((($Choices.TotalFileBackup | Where-Object { $null -ne $PSItem }) | ForEach-Object { $PSItem.ToString() } | Measure-Object -Maximum -Property Length).Maximum, (("No. Files" | Where-Object { $null -ne $PSItem }) | ForEach-Object { $PSItem.ToString() } | Measure-Object -Maximum -Property Length).Maximum | Measure-Object -Maximum).Maximum
$iMaxLengthFileSize = ((($Choices.FileSizeInString() | Where-Object { $null -ne $PSItem }) | Measure-Object -Maximum -Property Length).Maximum, (("Total Files Size" | Where-Object { $null -ne $PSItem }) | Measure-Object -Maximum -Property Length).Maximum | Measure-Object -Maximum).Maximum
$iMaxLengthTotalBlob = ((($Choices.TotalBlob | Where-Object { $null -ne $PSItem }) | ForEach-Object { $PSItem.ToString() } | Measure-Object -Maximum -Property Length).Maximum, (("No. Blobs" | Where-Object { $null -ne $PSItem }) | ForEach-Object { $PSItem.ToString() } | Measure-Object -Maximum -Property Length).Maximum | Measure-Object -Maximum).Maximum
$iMaxLengthBlobSize = ((($Choices.BlobSizeInString() | Where-Object { $null -ne $PSItem }) | Measure-Object -Maximum -Property Length).Maximum, (("Total Blobs Size" | Where-Object { $null -ne $PSItem }) | Measure-Object -Maximum -Property Length).Maximum | Measure-Object -Maximum).Maximum
$iMaxLengthRatio = ((($Choices.Ratio | Where-Object { $null -ne $PSItem }) | ForEach-Object { $PSItem.ToString() } | Measure-Object -Maximum -Property Length).Maximum, (("Ratio" | Where-Object { $null -ne $PSItem }) | ForEach-Object { $PSItem.ToString() } | Measure-Object -Maximum -Property Length).Maximum | Measure-Object -Maximum).Maximum
Write-Host " No. | $("ShortId".PadRight($iMaxLengthShortId, " ")) | $("DateTime".PadRight($iMaxLengthDateTime, " ")) | $("Tags".PadRight($iMaxLengthTags, " ")) | $("No. Files".PadRight($iMaxLengthTotalFileBackup, " ")) | $("Total Files Size".PadRight($iMaxLengthFileSize, " ")) | $("No. Blobs".PadRight($iMaxLengthTotalBlob, " ")) | $("Total Blobs Size".PadRight($iMaxLengthBlobSize, " ")) | $("Ratio".PadRight($iMaxLengthRatio, " "))" -ForegroundColor Green
Write-Host " --- | $("-".PadRight($iMaxLengthShortId, "-")) | $("-".PadRight($iMaxLengthDateTime, "-")) | $("-".PadRight($iMaxLengthTags, "-")) | $("-".PadRight($iMaxLengthTotalFileBackup, "-")) | $("-".PadRight($iMaxLengthFileSize, "-")) | $("-".PadRight($iMaxLengthTotalBlob, "-")) | $("-".PadRight($iMaxLengthBlobSize, "-")) | $("-".PadRight($iMaxLengthRatio, "-"))" -ForegroundColor Green
$Choices | ForEach-Object {
Write-Host " $(($counter++).ToString().PadLeft($iMaxLengthNumber, " ")) | $($PSItem.ShortId.PadRight($iMaxLengthShortId, " ")) | $($PSItem.DateTime.ToString().PadLeft($iMaxLengthDateTime, " ")) | $($PSItem.Tags.PadRight($iMaxLengthTags, " ")) | $($PSItem.TotalFileBackup.ToString().PadLeft($iMaxLengthTotalFileBackup, " ")) | $($PSItem.FileSizeInString().PadRight($iMaxLengthFileSize, " ")) | $($PSItem.TotalBlob.ToString().PadLeft($iMaxLengthTotalBlob, " ")) | $($PSItem.BlobSizeInString().PadRight($iMaxLengthBlobSize, " ")) | $($PSItem.Ratio.ToString().PadRight($iMaxLengthRatio, " "))"
}
ShowMessage "OTHER" ""
do {
If ($bFirstLoop) {
$bFirstLoop = $False
} Else {
Write-Host "`nBad choice! Please type a number of the 'No.' column" -ForegroundColor Red
}
$selection = [int](Read-Host -Prompt $Message)
} While ($selection -lt 1 -or $selection -gt $Choices.Count)
Return $selection-1
}
#TODO: Help header
function Get-SnapshotsCount {
<#
.SYNOPSIS
Summary of the script
.DESCRIPTION
Script description
.PARAMETER param1
Parameter description
.INPUTS
Pipeline input data
.OUTPUTS
Output data
.EXAMPLE
.\template.ps1 param1
.NOTES
Name : Script-Name
Version : 1.0.0.1
Created by : Chucky2401
Date Created : 27/07/2022
Modify by : Chucky2401
Date modified : 27/07/2022
Change : Creation
Copy : Copy-Item .\Script-Name.ps1 \Final\Path\Script-Name.ps1 -Force
.LINK
http://github.com/UserName/RepoName
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[Object[]]$ResticOutObject
)
# Select
$oSelectTags = @{Label = "tags" ; Expression = {$PSItem.tags[0]}}
$htGameSnapshotsCount = [ordered]@{}
$ResticOutObject | Select-Object $oSelectTags | Sort-Object tags | Group-Object -Property tags | ForEach-Object {
$htGameSnapshotsCount.Add($PSItem.Name, $PSItem.Count)
}
return $htGameSnapshotsCount
}
#TODO: Help header
function Get-SnapshotsList {
<#
.SYNOPSIS
Summary of the script
.DESCRIPTION
Script description
.PARAMETER param1
Parameter description
.INPUTS
Pipeline input data
.OUTPUTS
Output data
.EXAMPLE
.\template.ps1 param1
.NOTES
Name : Script-Name
Version : 1.0.0.1
Created by : Chucky2401
Date Created : 27/07/2022
Modify by : Chucky2401
Date modified : 27/07/2022
Change : Creation
Copy : Copy-Item .\Script-Name.ps1 \Final\Path\Script-Name.ps1 -Force
.LINK
http://github.com/UserName/RepoName
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Game
)
$oSnapshotsList = Start-Command "Restic Stats" restic "$($sCommonResticArguments) snapshots --tag `"$($Game)`" --json"
$jsonSnapshotsList = ConvertFrom-Json -InputObject $oSnapshotsList.stdout
<#
# hostname NoteProperty string hostname=Zoukzouk
# id NoteProperty string id=806c12af27b5a275b33bedd940af1ac5ad4af7b9e82305f4ff68fa63d78b91f3
# parent NoteProperty string parent=a972fc773f3c4caa4cb2f76e5bd46e50894233ba6129006a70f07d9966d214cb
# paths NoteProperty Object[] paths=System.Object[]
# short_id NoteProperty string short_id=806c12af
# tags NoteProperty Object[] tags=System.Object[]
# time NoteProperty datetime time=24/01/2022 13:06:58
# tree NoteProperty string tree=983c0e2f2a2754b1d9ea14818b83f57bc172a9fb8d07f840a92a8926a27a346b
# username NoteProperty string username=ZOUKZOUK\The Black Wizard
#>
$jsonSnapshotsList | ForEach-Object {
$game = $PSItem.tags | Select-Object -First 1
$id = $PSItem.id
$shortId = $PSItem.short_id
$tree = $PSItem.tree
$parent = $PSItem.parent
$dateTime = $PSItem.time
$tags = $PSItem.tags | Select-Object -Skip 1
$paths = $PSItem.paths
$hostname = $PSItem.hostname
$username = $PSItem.username
If ($null -eq $parent) {
$parent = "#N/A"
}
$PSItem.PSObject.Properties.Remove('username')
$PSItem.PSObject.Properties.Remove('tree')
$PSItem.PSObject.Properties.Remove('time')
$PSItem.PSObject.Properties.Remove('tags')
$PSItem.PSObject.Properties.Remove('short_id')
$PSItem.PSObject.Properties.Remove('paths')
$PSItem.PSObject.Properties.Remove('parent')
$PSItem.PSObject.Properties.Remove('id')
$PSItem.PSObject.Properties.Remove('hostname')
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name Game -Value $game
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name Id -Value $id
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name ShortId -Value $shortId
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name Tree -Value $tree
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name Parent -Value $parent
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name DateTime -Value $dateTime
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name Tags -Value $tags
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name Paths -Value $paths
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name Hostname -Value $hostname
Add-Member -InputObject $PSItem -MemberType NoteProperty -Name Username -Value $username
}
return $jsonSnapshotsList
}
#TODO: header
function Get-SnapshotDetails {
<#
.SYNOPSIS
Summary of the script
.DESCRIPTION
Script description
.PARAMETER param1
Parameter description
.INPUTS
Pipeline input data
.OUTPUTS
Output data
.EXAMPLE
.\template.ps1 param1
.NOTES
Name : Script-Name
Version : 1.0.0.1
Created by : Chucky2401
Date Created : 27/07/2022
Modify by : Chucky2401
Date modified : 27/07/2022
Change : Creation
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Object]$Snapshot,
[Parameter(Mandatory = $True)]
[Int]$Number
)
$oSnapshotStats = Get-ResticStats -SnapshotId $Snapshot.Id
$sbFileSizeInString = {
$sSizeInString = ""
$deBest = $this.TotalFileSize.GetEnumerator() | Where-Object { $_.Value -lt 1024 -and $_.Value -ge 1 }
$sUnity = $deBest.Key
$sValue = $deBest.Value
switch ($sUnity) {
"SizeInByte" { $sSizeInString = "$($sValue) B" }
"SizeInKByte" { $sSizeInString = "$($sValue) KiB" }
"SizeInMByte" { $sSizeInString = "$($sValue) MiB" }
"SizeInGByte" { $sSizeInString = "$($sValue) GiB" }
Default { $sSizeInString = "$($this.TotalFileSize.SizeInGByte) GiB" }
}
return $sSizeInString
}
$sbBlobSizeInString = {
$sSizeInString = ""
$deBest = $this.TotalBlobSize.GetEnumerator() | Where-Object { $_.Value -lt 1024 -and $_.Value -ge 1 }
$sUnity = $deBest.Key
$sValue = $deBest.Value
switch ($sUnity) {
"SizeInByte" { $sSizeInString = "$($sValue) B" }
"SizeInKByte" { $sSizeInString = "$($sValue) KiB" }
"SizeInMByte" { $sSizeInString = "$($sValue) MiB" }
"SizeInGByte" { $sSizeInString = "$($sValue) GiB" }
Default { $sSizeInString = "$($this.TotalBlobSize.SizeInGByte) GiB" }
}
return $sSizeInString
}
# Pram Add-Member
$hFileSizeInString = @{
MemberType = "ScriptMethod"
Name = "FileSizeInString"
Value = $sbFileSizeInString
}
$hBlobSizeInString = @{
MemberType = "ScriptMethod"
Name = "BlobSizeInString"
Value = $sbBlobSizeInString
}
$oSnapshotDetails = [PSCustomObject]@{
PSTypeName = 'Tjvs.Restic.SnapshotsStats'
Number = $Number
Game = $Snapshot.Game
Id = $Snapshot.Id
ShortId = $Snapshot.ShortId
Tree = $Snapshot.Tree
Parent = $Snapshot.Parent
DateTime = $Snapshot.DateTime
Tags = $Snapshot.Tags
Paths = $Snapshot.Paths
Hostname = $Snapshot.Hostname
Username = $Snapshot.Username
TotalFileBackup = $oSnapshotStats.TotalFileBackup
TotalFileSize = $oSnapshotStats.TotalFileSize
TotalBlob = $oSnapshotStats.TotalBlob
TotalBlobSize = $oSnapshotStats.TotalBlobSize
Ratio = $oSnapshotStats.Ratio
}
$oSnapshotDetails | Add-Member @hFileSizeInString
$oSnapshotDetails | Add-Member @hBlobSizeInString
return $oSnapshotDetails
}
function Invoke-Clean {
<#
.SYNOPSIS
Summary of the script
.DESCRIPTION
Script description
.PARAMETER param1
Parameter description
.INPUTS
Pipeline input data
.OUTPUTS
Output data
.EXAMPLE
.\template.ps1 param1
.NOTES
Name : Script-Name
Version : 1.0.0.1
Created by : Chucky2401
Date Created : 27/07/2022
Modify by : Chucky2401
Date modified : 27/07/2022
Change : Creation
#>
[CmdletBinding()]
Param(
)
$includeFilters = $listTags | Out-GridView -OutputMode Multiple -Title ($Message.View_ChooseFilters -f "include")
If ($includeFilters.Count -ge 1) {
$availableExcludeFilter = $listTags | Where-Object { $PSItem -notmatch [String]::Join("|", $includeFilters) }
}
If ($availableExcludeFilter.Count -ge 1) {
$excludeFilters = $availableExcludeFilter | Out-GridView -OutputMode Multiple -Title ($Message.View_ChooseFilters -f "exclude")
}
$bFirstLoop = $True
do {
If ($bFirstLoop) {
$bFirstLoop = $False
} Else {
Write-Host $Message.Err_SnapshotsTokeep -ForegroundColor Red
}
$inputValue = Read-Host -Prompt ($Message.Que_SnapshotsToKeep -f $global:settings.Snapshots.ToKeep)
If ([String]::IsNullOrEmpty($inputValue)) {
$inputValue = $global:settings.Snapshots.ToKeep
}
Try {
$selection = [int]$inputValue
} Catch {
$selection = -1
}
} While ($selection -lt 0)
$snapshotsToKeep = $selection
$snapshotsRemoved = .\Clean-Restic.ps1 -Game $sChooseGame -IncludeTag $includeFilters -ExcludeTag $excludeFilters -SnapshotToKeep $snapshotsToKeep -NoStats -FromGet -LogFile $sLogFile -Debug:($PSBoundParameters['Debug'] -eq $True)
If ($null -ne $snapshotsRemoved) {
$delete = [String]::Join("|", $snapshotsRemoved.SnapshotId)
$i = 1
$newList = $aSnapshotListDetails | Where-Object { $PSItem.ShortId -notMatch $delete }
$newList | Where-Object { $PSItem.ShortId -notMatch $delete } | ForEach-Object {
$PSItem.Number = $i
$i++
}
$aSnapshotListDetails = $newList
Return $aSnapshotListDetails
}
}
#----------------------------------------------------------[Declarations]----------------------------------------------------------
# Info
## Hashtable
$htGameSnapshotsCount = [ordered]@{}
# Logs
$sLogPath = "$($PSScriptRoot)\logs"
$sLogName = "Restic_List_Snaps-$(Get-Date -Format 'yyyy.MM.dd')-$(Get-Date -Format 'HH.mm').log"
If ($PSBoundParameters['Debug']) {
$sLogName = "DEBUG-$($sLogName)"
}
$sLogFile = "$($sLogPath)\$($sLogName)"
$aSnapshotListDetails = @()
$cntDetails = 0
$snapshotsNumber = 1
# Table select
$selectTotalFileSize = @{Label = "TotalFileSize" ; Expression = {$PSItem.FileSizeInString()}}
$selectTotalBlobSize = @{Label = "TotalBlobSize" ; Expression = {$PSItem.BlobSizeInString()}}
# Menu
$Title = $Message.Oth_TitleActionMenu
$Question = $Message.Que_ActionMenu
$clean = [ChoiceDescription]::new($Message.Men_CleanTitle, $Message.Men_CleanDescription)
$delete = [ChoiceDescription]::new($Message.Men_DeleteTitle, $Message.Men_DeleteDescription)
$quit = [ChoiceDescription]::new($Message.Men_QuitTitle, $Message.Men_QuitDescription)
$options = [ChoiceDescription[]]($clean, $delete, $quit)
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Write-CenterText "*********************************" $sLogFile
Write-CenterText "* *" $sLogFile
Write-CenterText "* List Game Snapshot *" $sLogFile
Write-CenterText "* $(Get-Date -Format 'yyyy.MM.dd') *" $sLogFile
Write-CenterText "* Start $(Get-Date -Format 'HH:mm') *" $sLogFile
Write-CenterText "* *" $sLogFile
Write-CenterText "*********************************" $sLogFile
ShowLogMessage -type "OTHER" -message "" -sLogFile ([ref]$sLogFile)
# List games
ShowLogMessage -type "INFO" -message $Message.Inf_GetGames -sLogFile ([ref]$sLogFile)
$oResticProcess = Start-Command -Title "Restic Snapshots" -FilePath restic -ArgumentList "$($sCommonResticArguments) --json snapshots"
If ($oResticProcess.ExitCode -eq 0) {
$jsResultRestic = $oResticProcess.stdout | ConvertFrom-Json
# Array of games
$aListGames = $jsResultRestic | Select-Object tags | ForEach-Object {
$PSItem.tags[0]
} | Select-Object -Unique | Sort-Object
# Hashtable of games and snapshots count per game
$htGameSnapshotsCount = Get-SnapshotsCount -ResticOutObject $jsResultRestic
ShowLogMessage -type "SUCCESS" -message $Message.Suc_GetGames -sLogFile ([ref]$sLogFile)
} Else {
ShowLogMessage -type "ERROR" -message $Message.Err_GetGames -variable $($oResticProcess.ExitCode) -sLogFile ([ref]$sLogFile)
If ($PSBoundParameters['Debug']) {
ShowLogMessage -type "DEBUG" -message $Message.Dbg_ErrDetail -sLogFile ([ref]$sLogFile)
$oResticProcess.stderr | Where-Object { $PSItem -ne "" } | ForEach-Object {
ShowLogMessage -type "OTHER" -message "`t$($PSItem)" -sLogFile ([ref]$sLogFile)
}
}
PAUSE
exit 1
}
If ($CountOnly) {
$htGameSnapshotsCount.GetEnumerator() | Select-Object @{ Label = "Game" ; Expression = {$PSItem.Name} }, @{ Label = "Snapshots" ; Expression = {$PSItem.Value} }
ShowMessage -type "OTHER" -message ""
Remove-Module Tjvs.*
exit 0
}
ShowLogMessage -type "OTHER" -message "" -sLogFile ([ref]$sLogFile)
If ([String]::IsNullOrEmpty($Game)) {
$gameIndice = Read-GameChoice -Title $Message.Que_GameChoiceTitle -Message $Message.Que_GameChoiceMsg -Choices $aListGames
If ($gameIndice -eq "q") {
ShowMessage "OTHER" ""
Remove-Module Tjvs.*
exit 0
}
}
If (-not [String]::IsNullOrEmpty($Game)) {
$gameIndice = $aListGames.IndexOf($Game)
}
If ($gameIndice -eq -1) {
ShowMessage -type "ERROR" -message $Message.Err_GameChoiceParam -variable $($Game)
ShowMessage -type "OTHER" -message ""
Remove-Module Tjvs.*
exit 1
}
$sChooseGame = $aListGames[$gameIndice]
ShowLogMessage -type "OTHER" -message "" -sLogFile ([ref]$sLogFile)
ShowLogMessage -type "INFO" -message $Message.Inf_GetSnaps -variable $($sChooseGame) -sLogFile ([ref]$sLogFile)
$oSnapshotsList = Get-SnapshotsList -Game $sChooseGame
$oSnapshotsList | ForEach-Object {
$iPercentComplete = [Math]::Round(($cntDetails/$oSnapshotsList.Length)*100,2)
Write-Progress -Activity $($Message.Prg_Activity -f $($sChooseGame), $($cntDetails), $($oSnapshotsList.Length), $($iPercentComplete)) -PercentComplete $iPercentComplete -Status $($Message.Prg_Status -f $($PSItem.ShortId))
$oSnapshotDetailStats = Get-SnapshotDetails -Snapshot $PSItem -Number $snapshotsNumber
$aSnapshotListDetails += $oSnapshotDetailStats
$cntDetails++
$snapshotsNumber++
}
Write-Progress -Activity $Message.Prg_Complete -Completed
If ($Listing) {
$aSnapshotListDetails
Remove-Module Tjvs.*
exit 0
}
$listTags = ($aSnapshotListDetails | Select-Object -ExpandProperty Tags -Unique)
$availableExcludeFilter = $listTags
do {
Clear-Host
ShowLogMessage -type "OTHER" -message $Message.Oth_ListSnaps -variable $sChooseGame -sLogFile ([ref]$sLogFile)
$aSnapshotListDetails | Select-Object -Property Number, ShortId, DateTime, Tags, TotalFileBackup, $selectTotalFileSize, TotalBlob, $selectTotalBlobSize | Format-Table -AutoSize
$result = $host.ui.PromptForChoice($Title, $Question, $options, 0)
ShowMessage -type "OTHER" -message ""
switch ($result) {
0 {
$aSnapshotListDetails = Invoke-Clean
Break
}
1 {
$snapshotsChoose = $aSnapshotListDetails | Select-Object -Property Number, ShortId, DateTime, Tags, TotalFileBackup, $selectTotalFileSize, TotalBlob, $selectTotalBlobSize | Out-GridView -OutputMode Multiple -Title "Choose snapshots to delete"
$snapshotsRemoved = .\Remove-ResticSnapshots.ps1 -ShortIds $snapshotsChoose.ShortId -FromGet -LogFile ([ref]$sLogFile) -Debug:($PSBoundParameters['Debug'] -eq $True)
If ($null -ne $snapshotsRemoved) {
$delete = [String]::Join("|", $snapshotsRemoved.SnapshotId)
$i = 1
$newList = $aSnapshotListDetails | Where-Object { $PSItem.ShortId -notMatch $delete }
$newList | Where-Object { $PSItem.ShortId -notMatch $delete } | ForEach-Object {
$PSItem.Number = $i
$i++
}
$aSnapshotListDetails = $newList
#Return $aSnapshotListDetails
}
Break
}
2 {
Break
}
Default {
ShowMessage -type "ERROR" -message $Message.Err_GenericChoice
}
}
} while ($result -ne 2)
Remove-Module Tjvs.*