Compare commits

...

2 commits

3 changed files with 90 additions and 48 deletions

View file

@ -4,6 +4,7 @@ source "$dir/../../lib/action.fish"
function backupAction -V dir function backupAction -V dir
source "$dir/../../lib/hooks.fish" source "$dir/../../lib/hooks.fish"
source "$dir/../../lib/restoration.fish"
if not type -q getDeploymentScript if not type -q getDeploymentScript
function getDeploymentScript function getDeploymentScript
@ -13,45 +14,7 @@ function backupAction -V dir
end end
set -l deployScript (getDeploymentScript) set -l deployScript (getDeploymentScript)
initConfig --action backup
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
runHook backupSoftware || begin runHook backupSoftware || begin
echo "Backing up software..." echo "Backing up software..."

View file

@ -55,6 +55,9 @@ function runSetupUserAction -V dir
set -l sudoConfig "/etc/sudoers.d/PortValhalla" set -l sudoConfig "/etc/sudoers.d/PortValhalla"
begin begin
if id -u "$name" &>/dev/null
echo "Setup user already exists!"
else
echo "Creating setup user" echo "Creating setup user"
and useradd \ and useradd \
@ -64,6 +67,7 @@ function runSetupUserAction -V dir
--create-home \ --create-home \
--uid (getOSConfig setupUser.id --json) \ --uid (getOSConfig setupUser.id --json) \
"$name" "$name"
end
and begin and begin
echo "$name ALL=(ALL:ALL) NOPASSWD: ALL" echo "$name ALL=(ALL:ALL) NOPASSWD: ALL"

View file

@ -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 ~/.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 function backupFiles
if [ -n "$VALHALLA_BACKUP_DIR" ] if [ -n "$VALHALLA_BACKUP_DIR" ]
argparse -i "base-directory=" -- $argv argparse -i "base-directory=" -- $argv