Files
wx-win-agent/scripts/control-plane-data.sh
T
rogee 97cfba7f01
Build web service image / build (push) Successful in 40s
style: format deployment helpers
2026-09-12 12:05:24 +08:00

52 lines
1.1 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