From e6c8bbe9c6d4f6eaf51d17fd32d6c6052cce0140 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 27 Aug 2024 21:05:51 +0200 Subject: [PATCH] Add a script for managing backups --- scripts/Windows/Scripts/Restoration.ps1 | 119 ++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 scripts/Windows/Scripts/Restoration.ps1 diff --git a/scripts/Windows/Scripts/Restoration.ps1 b/scripts/Windows/Scripts/Restoration.ps1 new file mode 100644 index 00000000..21c3ffc2 --- /dev/null +++ b/scripts/Windows/Scripts/Restoration.ps1 @@ -0,0 +1,119 @@ +. "$PSScriptRoot/../../Common/Scripts/System.ps1"; + +$null = New-Module { + $pathResolver = { + param( + [string] $User, + [string] $Path + ) + + [string] $result = $null; + + if ($User) { + $result = "Users/$User"; + } else { + $result = "System"; + } + + $result = Join-Path $result $Path; + return $result; + }; + + <# + .SYNOPSIS + Gets the name of the variable holding the path to the backup archive of the current machine. + #> + function Get-ArchiveVariableName { + return "BACKUP_ARCHIVE"; + } + + <# + .SYNOPSIS + Gets the path to the backup archive of the current machine. + #> + function Get-ValhallaBackupArchive { + (Get-Item "Env:\$(Get-ArchiveVariableName)").Value; + } + + <# + .SYNOPSIS + Sets the path to the backup archive of the current machine. + + .PARAMETER Path + The path to set. + #> + function Set-ValhallaBackupArchive { + param( + [string] $Path + ) + + Set-Item "Env:\$(Get-ArchiveVariableName)" $Path; + } + + <# + .SYNOPSIS + Adds files to the backup archive. + + .PARAMETER User + The user to add the files to. + + .PARAMETER Source + The file or directory to add to the backup archive. + + .PARAMETER Path + The path to the location to store the file or directory at. + + .PARAMETER ArgumentList + The arguments to pass to the `7z` command. + #> + function Add-BackupArtifacts { + param( + [string] $User, + [string] $Source, + [string] $Path, + [Parameter(ValueFromRemainingArguments)] + [string[]] $ArgumentList + ) + + $dir = New-TemporaryDirectory; + $targetPath = & $pathResolver @PSBoundParameters; + $targetPath = Join-Path $dir $targetPath; + $null = New-Item -ItemType Directory -Force $targetPath; + + Copy-Item -Force -Recurse $Source $targetPath; + 7z a (Get-ValhallaBackupArchive) "$dir/*" @ArgumentList; + Remove-Item -Recurse -Force $dir; + } + + <# + .SYNOPSIS + Extracts the specified backup artifacts to the specified target path. + + .PARAMETER User + The user to restore the files for. + + .PARAMETER Path + The path to restore the files from. + + .PARAMETER Target + The path to restore the files to. + + .PARAMETER ArgumentList + The arguments to pass to `7z`. + #> + function Expand-BackupArtifacts { + param( + [string] $User, + [string] $Path, + [string] $Target, + [Parameter(ValueFromRemainingArguments)] + [string[]] $ArgumentList + ) + + $dir = New-TemporaryDirectory; + $sourcePath = & $pathResolver @PSBoundParameters; + 7z x "-o$dir" (Get-ValhallaBackupArchive) $sourcePath @ArgumentList; + Copy-Item -Recurse (Join-Path $dir $sourcePath) $Target; + Remove-Item -Recurse -Force $dir; + } +};