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/backupify

53 lines
1.6 KiB
Plaintext
Raw Permalink 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
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
2021-02-18 13:26:10 -06:00
if not set -q argv[1]
2021-10-29 11:32:16 -05:00
echo "No target file or directory provided"
2021-02-18 13:26:10 -06:00
exit 2
end
2021-10-29 11:32:16 -05:00
set target $argv[1]
if not test -d $target
echo "Target '$target' is not a directory"
2021-02-18 13:26:10 -06:00
exit 1
end
2021-10-29 11:32:16 -05:00
set remote_path
pushd (dirname $target) >/dev/null
2021-03-25 09:29:48 -05:00
# TODO: progress bar/status?
2021-10-29 11:32:16 -05:00
set friendly_target (pwd)
set path_friendly_target (pwd | cut -c2- | string replace -a / %)
set backup_file_name (date +%Y-%m-%d_%H-%M-%S)_"$path_friendly_target"
set remote_path "$BACKUP_REMOTE_DIR/$backup_file_name$BACKUP_FILE_EXT"
echo "Streaming PGP encrypted backup of $friendly_target to SSH host $BACKUP_REMOTE_HOST at $remote_path"
2021-11-11 11:44:26 -06:00
ssh $BACKUP_REMOTE_HOST "mkdir -p $BACKUP_REMOTE_DIR" || begin
echo "Failed to SSH to $BACKUP_REMOTE_HOST and create remote dir $BACKUP_REMOTE_DIR"
exit 3
end
2021-10-29 11:32:16 -05:00
# TODO: progress indicator?
tar cf - . |
zstd --ultra -T2 -22 |
gpg --encrypt --recipient daniel@lyte.dev |
2021-11-11 11:44:26 -06:00
ssh $BACKUP_REMOTE_HOST "cat - > $remote_path" || begin
echo "Failed to stream backup"
exit 4
end
2021-10-29 11:32:16 -05:00
popd >/dev/null
2021-02-18 13:26:10 -06:00
# TODO: checksum?
2021-10-29 11:32:16 -05:00
echo "Uploaded to host rift via ssh at $remote_path"
echo " Run 'unbackupify \"$backup_file_name$BACKUP_FILE_EXT\"' to restore!"