Files
seal-use/ops/backup-workspace-scripts.sh
T

49 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -uo pipefail
repo_root=$(git rev-parse --show-toplevel)
if [ -n "$(git -C "$repo_root" status --porcelain -- ops)" ]; then
echo "Refusing to back up uncommitted ops files; commit them first." >&2
exit 1
fi
commit=$(git -C "$repo_root" rev-parse --short=12 HEAD)
snapshot="commit-$commit"
stage=$(mktemp -d)
trap 'rm -rf "$stage"' EXIT
git -C "$repo_root" archive --format=tar HEAD ops | tar -xf - -C "$stage"
printf 'source_commit=%s\ncreated_at_utc=%s\n' \
"$(git -C "$repo_root" rev-parse HEAD)" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
> "$stage/BACKUP-MANIFEST.txt"
local_target="$HOME/Workspace/ops-backups/seal-use/$snapshot"
mkdir -p "$local_target"
cp -R "$stage/." "$local_target/"
find "$local_target" -type f -name '._*' -delete
printf 'ok\tlocal\t%s\n' "$local_target"
sync_remote() {
local host="$1" relative_target="$2"
if ssh -o BatchMode=yes -o ConnectTimeout=8 "$host" \
"mkdir -p '$relative_target' && find '$relative_target' -type f -name '._*' -delete" \
&& scp -q -r "$stage/ops" "$stage/BACKUP-MANIFEST.txt" "$host:$relative_target/" \
&& ssh -o BatchMode=yes -o ConnectTimeout=8 "$host" \
"test -s '$relative_target/BACKUP-MANIFEST.txt' && test -f '$relative_target/ops/README.md'"; then
printf 'ok\t%s\t%s\n' "$host" "$relative_target"
return 0
fi
printf 'fail\t%s\t%s\n' "$host" "$relative_target" >&2
return 1
}
failures=0
sync_remote mac13 "Workspace/ops-backups/seal-use/$snapshot" || failures=$((failures + 1))
sync_remote wsl-dev "workspace/ops-backups/seal-use/$snapshot" || failures=$((failures + 1))
sync_remote aliyun "workspace/ops-backups/seal-use/$snapshot" || failures=$((failures + 1))
if [ "$failures" -gt 0 ]; then
printf '%s remote backup(s) failed\n' "$failures" >&2
exit 1
fi