2021-02-18 13:26:10 -06:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
|
2021-10-29 11:32:16 -05:00
|
|
|
# deps: fish, zstd, gpg, ssh
|
2021-02-18 13:26:10 -06:00
|
|
|
|
2021-10-29 11:32:16 -05:00
|
|
|
set -q HOST || set HOST (head -n 1 /etc/hostname | tr -d '\n')
|
|
|
|
|
|
|
|
# use the backup-host in the ~/.ssh/config as the default backup remote host
|
|
|
|
set -q BACKUP_REMOTE_HOST || set BACKUP_REMOTE_HOST backup-host
|
|
|
|
|
|
|
|
# use a user-specific dir by default to backup to
|
|
|
|
set -q BACKUP_REMOTE_DIR || set BACKUP_REMOTE_DIR "/storage/"(whoami)"/backups/$HOST"
|
|
|
|
|
|
|
|
set BACKUP_FILE_EXT .tar.zstd.gpg
|
|
|
|
|
|
|
|
set target $argv[1]
|
|
|
|
if test $target = '--list-backup-files'
|
|
|
|
ssh $BACKUP_REMOTE_HOST "command ls -1 $BACKUP_REMOTE_DIR"
|
|
|
|
exit 0
|
|
|
|
else if test (count $argv) -lt 1
|
|
|
|
echo "No remote backup filename provided (use --list-backup-files to show)"
|
|
|
|
exit 1
|
2021-02-18 13:26:10 -06:00
|
|
|
end
|
2021-10-29 11:32:16 -05:00
|
|
|
|
2021-11-11 11:44:26 -06:00
|
|
|
ssh $BACKUP_REMOTE_HOST "echo 1" &>/dev/null || begin
|
|
|
|
echo "Cannot ssh to $BACKUP_REMOTE_HOST"
|
|
|
|
exit 6
|
|
|
|
end
|
|
|
|
|
|
|
|
ssh $BACKUP_REMOTE_HOST "test -d $BACKUP_REMOTE_DIR/$target" || begin
|
|
|
|
echo "Remote directory $BACKUP_REMOTE_DIR/$target does not exist on $BACKUP_REMOTE_HOST (use --list-backup-files to show)"
|
|
|
|
exit 5
|
|
|
|
end
|
|
|
|
|
2021-02-18 13:26:10 -06:00
|
|
|
# TODO: autocomplete?
|
|
|
|
# TODO: checksum?
|
2021-10-29 11:32:16 -05:00
|
|
|
# TODO: progress indicator?
|
|
|
|
|
|
|
|
set local_dir (echo $target | awk -F. '{print $1}')
|
|
|
|
mkdir $local_dir || begin
|
|
|
|
echo "Local unbackup directory '$local_dir' exists"
|
|
|
|
exit 2
|
|
|
|
end
|
|
|
|
|
|
|
|
pushd $local_dir >/dev/null
|
|
|
|
|
|
|
|
ssh $BACKUP_REMOTE_HOST "cat $BACKUP_REMOTE_DIR/$target" |
|
|
|
|
gpg --decrypt 2>/dev/null |
|
|
|
|
zstd --ultra -T2 -22 -dc |
|
2021-11-11 11:44:26 -06:00
|
|
|
tar -xf - || begin
|
|
|
|
echo "Failed to stream backup"
|
|
|
|
exit 4
|
|
|
|
end
|
2021-10-29 11:32:16 -05:00
|
|
|
|
|
|
|
popd >/dev/null
|
|
|
|
|
|
|
|
echo "Restored to $local_dir"
|