43 lines
1.2 KiB
Bash
Executable File
43 lines
1.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
usage() {
|
|
echo "usage: $0 backup <data-file> <backup-dir> | restore <backup-file> <data-file>" >&2
|
|
exit 2
|
|
}
|
|
|
|
[ "$#" -eq 3 ] || usage
|
|
command=$1
|
|
source=$2
|
|
target=$3
|
|
|
|
case "$command" in
|
|
backup)
|
|
[ -f "$source" ] || { echo "data file does not exist: $source" >&2; exit 1; }
|
|
mkdir -p "$target"
|
|
name="$(basename "$source").$(date -u +%Y%m%dT%H%M%SZ).manual.json"
|
|
temporary="$target/.wxagent-backup-$$.tmp"
|
|
trap 'rm -f "$temporary"' EXIT
|
|
install -m 600 "$source" "$temporary"
|
|
mv -f "$temporary" "$target/$name"
|
|
sha256sum "$target/$name"
|
|
;;
|
|
restore)
|
|
[ -f "$source" ] || { echo "backup file does not exist: $source" >&2; exit 1; }
|
|
directory=$(dirname "$target")
|
|
mkdir -p "$directory"
|
|
lock="$target.lock"
|
|
exec 9>"$lock"
|
|
flock -n 9 || { echo "data file is in use; stop the active control plane first" >&2; exit 1; }
|
|
temporary="$directory/.wxagent-restore-$$.tmp"
|
|
trap 'rm -f "$temporary"' EXIT
|
|
install -m 600 "$source" "$temporary"
|
|
python3 -m json.tool "$temporary" >/dev/null
|
|
mv -f "$temporary" "$target"
|
|
sha256sum "$target"
|
|
;;
|
|
*)
|
|
usage
|
|
;;
|
|
esac
|