This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/common/bin/unbackupify

44 lines
1.1 KiB
Text
Raw Normal View History

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-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 |
tar -xf -
popd >/dev/null
echo "Restored to $local_dir"