Add a script for managing backups
This commit is contained in:
parent
687c36d50c
commit
e6c8bbe9c6
119
scripts/Windows/Scripts/Restoration.ps1
Normal file
119
scripts/Windows/Scripts/Restoration.ps1
Normal file
|
@ -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;
|
||||
}
|
||||
};
|
Loading…
Reference in a new issue