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

52 lines
1.6 KiB
Fish
Executable file

#!/usr/bin/env fish
# 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
if not set -q argv[1]
echo "No target file or directory provided"
exit 2
end
set target $argv[1]
if not test -d $target
echo "Target '$target' is not a directory"
exit 1
end
set remote_path
pushd (dirname $target) >/dev/null
# TODO: progress bar/status?
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"
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
# TODO: progress indicator?
tar cf - . |
zstd --ultra -T2 -22 |
gpg --encrypt --recipient daniel@lyte.dev |
ssh $BACKUP_REMOTE_HOST "cat - > $remote_path" || begin
echo "Failed to stream backup"
exit 4
end
popd >/dev/null
# TODO: checksum?
echo "Uploaded to host rift via ssh at $remote_path"
echo " Run 'unbackupify \"$backup_file_name$BACKUP_FILE_EXT\"' to restore!"