PortValhalla/scripts/lib/restoration.fish
2024-11-29 01:13:58 +01:00

81 lines
2 KiB
Fish

function backupFiles
if [ -n "$VALHALLA_BACKUP_DIR" ]
argparse -i "base-directory=" -- $argv
set -l tarArgs
set -l path (getArchivePath $argv)
set -l dir (dirname "$path")
argparse -i "u/user=" "n/name=" -- $argv
if runRestorationCommand test ! -d "$dir"
runRestorationCommand mkdir -p "$dir"
end
if [ -n "$_flag_base_directory" ]
set -a argv --base-directory "$_flag_base_directory"
set -a tarArgs -C "$_flag_base_directory"
else
set -a tarArgs -P
end
sudo tar $tarArgs -cvz (fd $argv) | createArchive "$path"
end
end
function restoreFiles
if [ -n "$VALHALLA_BACKUP_DIR" ]
argparse -i "user=" "base-directory=" -- $argv
set -l tarArgs
set -l sudoArgs
if [ -n "$_flag_base_directory" ]
set -a tarArgs -C "$_flag_base_directory"
else
set -a tarArgs -P
end
if [ -n "$_flag_user" ]
set -a sudoArgs -u "$_flag_user"
end
fetchArchive $argv | begin
sudo $sudoArgs tar $tarArgs -xvz
end
end
end
function createArchive -a path
runRestorationCommand sh -c "tee $(string escape -- "$path") >/dev/null"
end
function fetchArchive
set -l path (getArchivePath $argv)
runRestorationCommand cat "$path"
end
function runRestorationCommand
if [ -z "$VALHALLA_BACKUP_SERVER" ]
$argv
else
set -l args
if [ -n "$VALHALLA_BACKUP_SERVER_PORT" ]
set -a args -p $VALHALLA_BACKUP_SERVER_PORT
end
sudo ssh -i $(string escape -- "$VALHALLA_BACKUP_SERVER_KEY") $args "$VALHALLA_BACKUP_SERVER" (string escape -- $argv)
end
end
function getArchivePath
argparse -i "u/user=" "n/name=" -- $argv
set -l path "$VALHALLA_BACKUP_DIR"
if [ -b "$_flag_user" ]
set -a path Users "$_flag_user"
else
set -a path System
end
set -a path "$_flag_name.tar.gz"
realpath -m (string join / $path)
end