PortValhalla/scripts/lib/restoration.fish

80 lines
1.8 KiB
Fish

function backupFiles
if [ -n "$VALHALLA_BACKUP_DIR" ]
argparse -i "u/user=" "n/name=" "base-directory=" -- $argv
set -l tarArgs
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
tar $tarArgs -cvz (fd $argv) | createArchive $argv --user "$_flag_user" --name "$_flag_name"
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
set -l path (getArchivePath $argv)
if [ ! -d (dirname "$path") ]
mkdir -p (dirname "$path")
end
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" $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