diff --git a/scripts/Common/OS/backup.fish b/scripts/Common/OS/backup.fish index 9ed54d82..16a9e849 100644 --- a/scripts/Common/OS/backup.fish +++ b/scripts/Common/OS/backup.fish @@ -4,6 +4,7 @@ source "$dir/../../lib/action.fish" function backupAction -V dir source "$dir/../../lib/hooks.fish" + source "$dir/../../lib/restoration.fish" if not type -q getDeploymentScript function getDeploymentScript @@ -13,45 +14,7 @@ function backupAction -V dir end set -l deployScript (getDeploymentScript) - - if [ -z "$VALHALLA_BACKUP_DIR" ] - if fish "$dir/../../../lib/modules/partition/confirm.fish" "Do you wish to store the backup on an SSH server?" n - set -l keyVar VALHALLA_BACKUP_SERVER_KEY - set -l keyPath ~root/.config/port-valhalla/valhalla - read -xP "Please specify the host name of the SSH server: " VALHALLA_BACKUP_SERVER - read -xP "Please specify the port of the SSH server (default 22): " VALHALLA_BACKUP_SERVER_PORT - read -xP "Please specify the name of the user to log in to the SSH server: " VALHALLA_BACKUP_SERVER_USER - set -l host "$VALHALLA_BACKUP_SERVER" - - if [ -z "$$keyVar" ] - set -gx "$keyVar" "$keyPath" - sudo mkdir -p (dirname "$keyPath") - sudo ssh-keygen -f "$$keyVar" -N "" - end - - if [ -z "$VALHALLA_BACKUP_SERVER_PORT" ] - set -x VALHALLA_BACKUP_SERVER_PORT 22 - end - - if [ -n "$VALHALLA_BACKUP_SERVER_USER" ] - set host "$VALHALLA_BACKUP_SERVER_USER@$host" - end - - echo - echo "$(tput setaf 3)==== WARNING ====$(tput sgr0)" - echo "For a seamless experience, please make sure that you are able to establish an unattended ssh connection using key authentication." - echo - echo "Your public key is:" - sudo cat "$VALHALLA_BACKUP_SERVER_KEY.pub" - echo - echo "$(tput bold)This command should succeed without user interaction:$(tput sgr0)" - echo "sudo ssh -o PasswordAuthentication=no -i $(string escape -- "$VALHALLA_BACKUP_SERVER_KEY") -p $VALHALLA_BACKUP_SERVER_PORT $host true" - read -P "Press enter once you're done: " - echo - end - - read -xP "Please specify the path to the directory to save the backup to: " VALHALLA_BACKUP_DIR - end + initConfig --action backup runHook backupSoftware || begin echo "Backing up software..." diff --git a/scripts/lib/restoration.fish b/scripts/lib/restoration.fish index 84475373..1b20b5b4 100644 --- a/scripts/lib/restoration.fish +++ b/scripts/lib/restoration.fish @@ -1,3 +1,78 @@ +set -l dir (status dirname) + +function initConfig -V dir -d "Fetches the configuration by prompting the user to " + set -l disabled VALHALLA_BACKUP_DISABLED + + if [ -z "$$disabled" ] + if [ -z "$VALHALLA_BACKUP_DIR" ] + argparse -i "action=" -- $argv + set -l index + set -l confirm fish "$dir/../../../lib/modules/partition/confirm.fish" + + switch "$_flag_action" + case backup + set index 1 + case restore + set index 2 + case '*' + set index 3 + end + + if [ "$_flag_action" != restore ] || $confirm "Do you wish to restore a backup?" + set -l keyVar VALHALLA_BACKUP_SERVER_KEY + + set -l keyPath ~root/.config/port-valhalla/valhalla + set -l server (string repeat --count 3 (printf "%s\n" "Please specify the host name of the SSH server: ")) + set -l port (string repeat --count 3 (printf "%s\n" "Please specify the port of the SSH server (default 22): ")) + set -l user (string repeat --count 3 (printf "%s\n" "Please specify the name of the user to log in to the SSH server: ")) + + + set -l ssh \ + "Do you wish to store the backup on an SSH server?" \ + "Do you wish to restore the backup from an SSH server?" \ + "Do you wish to backup or restore files on an SSH server?" + + set -l path \ + "Please specify the path to the directory to save the backup to: " \ + "Please specify the path to the directory to load the backup from: " \ + "Please specify the path to the backup directory: " + + if [ -z "$$keyVar" ] + set -gx "$keyVar" "$keyPath" + ssh-keygen -f "$keyPath" -N "" + end + + if fish $confirm $ssh[$index] n + read -gxP $server[$index] VALHALLA_BACKUP_SERVER + read -gxP $port[$index] VALHALLA_BACKUP_SERVER_PORT + read -gxP $user[$index] VALHALLA_BACKUP_SERVER_USER + end + + if [ -n "$VALHALLA_BACKUP_SERVER" ] + set -l sshArgs + + if [ -n "$VALHALLA_BACKUP_SERVER_PORT" ] + set -a sshArgs -p "$VALHALLA_BACKUP_SERVER_PORT" + end + + echo + echo "$(tput setaf 3)==== WARNING ====$(tput sgr0)" + echo "For a seamless experience, please make sure that you are able to establish an unattended ssh connection using key authentication." + echo + echo "$(tput bold)This command should succeed without user interaction:$(tput sgr0)" + echo "sudo ssh -o PasswordAuthentication=no -i $(string escape -- "$VALHALLA_BACKUP_SERVER_KEY") $sshArgs $VALHALLA_BACKUP_SERVER true" + read -P "Press enter once you're done: " + echo + end + + read -gxP $path[$index] VALHALLA_BACKUP_DIR + else + set -gx "$disabled" 1 + end + end + end +end + function backupFiles if [ -n "$VALHALLA_BACKUP_DIR" ] argparse -i "base-directory=" -- $argv