Skip to content

Commit

Permalink
PR: release v3.0-Beta.2 merge to main
Browse files Browse the repository at this point in the history
Version 3.0 Beta 2
  • Loading branch information
Chucky2401 authored May 21, 2023
2 parents 817987a + 84e4560 commit 6dc2dde
Show file tree
Hide file tree
Showing 23 changed files with 896 additions and 250 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
misc/
logs/*
doc/perso/
CHANGELOG_Release_v3.0.md

!/logs/.gitkeep
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Changelog

## 2023.05.21 - v3.0-Beta.2

### New

- (Clean-Restic): Add parameter `-ExcludeTag`
- (Get-ResticSnapshots): ability to choose tag to exclude before calling the script to clean
- (Get-ResticSnapshots): add parameter `-Listing` which show snapshots list and quit
- (Remove-ResticSnapshots): let you remove one or some specific snapshots!
- (Get-ResticSnapshots): let you call `Remove-ResticSnapshots` after you choose the snapshots to remove

### Change

- (Clean-Restic): rename parameter `-TagFilter` to `-IncludeTag`

### Changelog

*Only relevant commit*

- *ae30c7c* - feat(Clean-Restic): rename parameter -TagFilter to -IncludeTag.
- *5e86325* - feat: implement new parameter -ExcludeTag to Clean-Restic
- *eeab12e* - feat(Get-ResticGameSnapshots): add possibility to choose exclude tag filter
- *322ecfc* - feat(Get-ResticGameSnapshots): add -Listing parameter which show snapshots list and quit.
- *bef2b18* - feat: script to remove specific Restic snapshots!
- *f306443* - feat(Get-ResticGameSnapshots): implement Remove in the menu!

## 2023.05.15 - v3.0-Beta.1

### New
Expand Down
95 changes: 62 additions & 33 deletions Clean-Restic.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
This script permit to remove restic snapshots for a game and to keep a certain amout of snapshots (by default: 5)
.PARAMETER Game
Game name of snapshots to delete
.PARAMETER TagFilter
.PARAMETER IncludeTag
A filter on the snapshots to retrieve for the game
.PARAMETER ExcludeTag
Tag to ignore
.PARAMETER SnapshotToKeep
Number of snapshots to keep (by default: 5)
.PARAMETER NoDelete
Expand All @@ -26,12 +28,12 @@
Will simulate removing of V Rising snapshots
.NOTES
Name : Clean-Restic
Version : 3.0-Beta.1
Version : 3.0-Beta.2
Created by : Chucky2401
Date Created : 30/06/2022
Modify by : Chucky2401
Date modified : 14/05/2023
Change : Feature if call from Get and enhancement for filter
Change : Exclude parameter fix
.LINK
https://github.com/Chucky2401/Restic-Scripts/blob/main/README.md#clean-restic
#>
Expand All @@ -45,20 +47,26 @@ Param (
[Alias("g")]
[string[]]$Game,
[Parameter(Mandatory = $False)]
[Alias("f")]
[string[]]$TagFilter = "",
[Alias("i")]
[string[]]$IncludeTag = "",
[Parameter(Mandatory = $False)]
[Alias("stk")]
[Alias("e")]
[string[]]$ExcludeTag = "",
[Parameter(Mandatory = $False)]
[Alias("s")]
[int]$SnapshotToKeep,
[Parameter(Mandatory = $False)]
[Alias("nd")]
[Alias("n")]
[Switch]$NoDelete,
[Parameter(Mandatory = $False)]
[Alias("ns")]
[Alias("t")]
[Switch]$NoStats,
[Parameter(Mandatory = $False)]
[Alias("fg")]
[Switch]$FromGet
[Alias("r")]
[Switch]$FromGet,
[Parameter(Mandatory = $False)]
[Alias("l")]
[String]$LogFile = $null
)

BEGIN {
Expand All @@ -72,7 +80,7 @@ BEGIN {
}

Update-FormatData -AppendPath "$($PSScriptRoot)\inc\format\ResticControl.format.ps1xml"
$PSStyle.Progress.MaxWidth = ($Host.UI.RawUI.WindowSize.Width)-5
$PSStyle.Progress.MaxWidth = ($Host.UI.RawUI.WindowSize.Width)

Import-LocalizedData -BindingVariable "Message" -BaseDirectory "local" -FileName "Clean-Restic.psd1"

Expand All @@ -91,25 +99,47 @@ BEGIN {
}

## Common restic to use
$sFilter = "--tag `"$Game`""
$messageTagFilter = $Message.Oth_MessageFilter -f $([String]::Join($Message.Oth_Or, $TagFilter))

#If ($TagFilter -ne "") {
If (-not [String]::IsNullOrEmpty($TagFilter)) {
$sFilter = ""
$TagFilter | ForEach-Object {
$sFilter += " --tag `"$Game,$($PSItem)`""
$includeFilter = "--tag `"$Game`""
$sharedFilter = @()
$joinedTag = @()
$messageTagFilter = ""

If (-not [String]::IsNullOrEmpty($IncludeTag)) {
$includeFilter = ""
$IncludeTag | ForEach-Object {
$includeFilter += " --tag `"$Game,$($PSItem)`""
}

$joinedTag += "$($Message.Oth_Include): $([String]::Join($Message.Oth_Or, $IncludeTag))"
}
$includeFilter = $includeFilter.Trim()

If (-not [String]::IsNullOrEmpty($ExcludeTag)) {
If (-not [String]::IsNullOrEmpty($IncludeTag)) {
$sharedFilter = (Compare-Object $IncludeTag $ExcludeTag -IncludeEqual -ExcludeDifferent).InputObject
}

If ($sharedFilter.Count -ge 1) {
Write-Message -Type "ERROR" -Message $Message.Err_ShaFilt -Variables ([String]::Join(" ; ", $sharedFilter)) -LogFile ([ref]$sLogFile)
Write-Message -Type "OTHER"

exit 0
}

$joinedTag += "$($Message.Oth_Exclude): $([String]::Join($Message.Oth_Or, $ExcludeTag))"
}
$sFilter = $sFilter.Trim()
$messageTagFilter = $Message.Oth_MessageFilter -f $([String]::Join(" ; ", $joinedTag))

# Logs
$sLogPath = "$($PSScriptRoot)\logs"
$sLogName = "Restic-Clean_old_backup-$(Get-Date -Format 'yyyy.MM.dd')-$(Get-Date -Format 'HH.mm').log"
If ($PSBoundParameters['Debug']) {
$sLogName = "DEBUG-$($sLogName)"
$sLogFile = $LogFile
If ([String]::IsNullOrEmpty($LogFile)) {
$sLogPath = "$($PSScriptRoot)\logs"
$sLogName = "Restic-Clean_old_backup-$(Get-Date -Format 'yyyy.MM.dd')-$(Get-Date -Format 'HH.mm').log"
If ($PSBoundParameters['Debug']) {
$sLogName = "DEBUG-$($sLogName)"
}
$sLogFile = "$($sLogPath)\$($sLogName)"
}
$sLogFile = "$($sLogPath)\$($sLogName)"

# Init Var
$oDataBefore = $null
Expand Down Expand Up @@ -138,7 +168,7 @@ BEGIN {
}

##! Demo purpose only!
#$NoStats = $True
#$NoStats = $True
#$NoDelete = $True
##! Demo purpose only!
}
Expand All @@ -150,7 +180,7 @@ PROCESS {

foreach ($sGame in $Game) {
ShowLogMessage -type "INFO" -message $Message.Inf_GetSnaps -variable $($sGame) -sLogFile ([ref]$sLogFile)
$oResticProcess = Start-Command -Title "Restic - Get $($sGame) snapshots" -FilePath restic -ArgumentList "snapshots $($sFilter) --json"
$oResticProcess = Start-Command -Title "Restic - Get $($sGame) snapshots" -FilePath restic -ArgumentList "snapshots $($includeFilter) --json"

If ($oResticProcess.ExitCode -eq 0) {
ShowLogMessage -type "SUCCESS" -message $Message.Suc_GetSnaps -sLogFile ([ref]$sLogFile)
Expand All @@ -163,13 +193,12 @@ PROCESS {
ShowLogMessage -type "OTHER" -message "`t$($PSItem)" -sLogFile ([ref]$sLogFile)
}
}

PAUSE

exit 1
}

$numberSnapshotsTotal = $jsResultRestic.Count
$numberSnapshotsToRemove = ($jsResultRestic | Sort-Object time | Select-Object -SkipLast $SnapshotToKeep).Count
$numberSnapshotsToRemove = ($jsResultRestic | Where-Object { $PSItem.tags -notcontains $ExcludeTag } | Sort-Object time | Select-Object -SkipLast $SnapshotToKeep).Count

ShowLogMessage -type "OTHER" -message "" -sLogFile ([ref]$sLogFile)

Expand All @@ -178,7 +207,7 @@ PROCESS {
} Else {
ShowLogMessage -type "INFO" -message $Message.Inf_DelSnaps -variable $numberSnapshotsToRemove,$numberSnapshotsTotal,$($sGame),$messageTagFilter -sLogFile ([ref]$sLogFile)
}
$jsResultRestic | Sort-Object time | Select-Object -SkipLast $SnapshotToKeep | ForEach-Object {
$jsResultRestic | Where-Object { $PSItem.tags -notcontains $ExcludeTag } | Sort-Object time | Select-Object -SkipLast $SnapshotToKeep | ForEach-Object {
$iPercentComplete = [Math]::Round(($cntDetails/$numberSnapshotsToRemove)*100,2)
$sSnapshotId = $PSItem.short_id

Expand All @@ -202,12 +231,12 @@ PROCESS {
}
}
} Else {
#ShowLogMessage -type "OTHER" -message $Message.Dbg_DelSnaps -variable $($sSnapshotId) -sLogFile ([ref]$sLogFile)
ShowLogMessage -type "OTHER" -message $Message.Dbg_DelSnaps -variable $($sSnapshotId) -sLogFile ([ref]$sLogFile)
$aSnapshotRemoved += $sSnapshotId
##! Demo purpose only!
#$aSnapshotRemoved += [PSCustomObject]@{ SnapshotId = $sSnapshotId ; Detail = [String]::Join("//", "OK!") }
#Start-Sleep -Seconds 2
##! Demo purpose only!
$aSnapshotRemoved += $sSnapshotId
}
$cntDetails++
}
Expand Down
Loading

0 comments on commit 6dc2dde

Please sign in to comment.