PortValhalla/scripts/lib/restoration.fish

82 lines
1.9 KiB
Fish
Raw Normal View History

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
2024-11-23 13:10:30 +00:00
sudo tar $tarArgs -cv (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
2024-11-23 13:10:30 +00:00
sudo $sudoArgs tar $tarArgs -xv
end
end
end
function createArchive -a path
runRestorationCommand tee "$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
ssh $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