update.sh: back up saves with one shared tool

The tool discovers local/data/*, writes to saves-backup/, keeps the newest 10 per data directory, and update.sh stops if the backup fails. No home-directory or title-ID assumptions.

(cherry picked from commit 45591b0f6d)
This commit is contained in:
goldfishdev
2026-12-03 11:36:00 -06:00
committed by Tim-van-Oudheusden
parent a87e3b7366
commit da0e53ddf7
3 changed files with 92 additions and 1 deletions

1
.gitignore vendored
View File

@ -83,6 +83,7 @@ Thumbs.db
!/local/data/
/local/data/*
!/local/data/.gitkeep
/saves-backup
/run.conf
/probe/
/bisect/

View File

@ -170,7 +170,9 @@ them, as they are, from the same place in the other installation. Folders
from an older VIctory, without the `~`, are converted the next time the game
starts.
To back up your saves, copy that folder somewhere else. `git pull` and
To back up your saves, run `./tools/backup-saves.sh`: it writes the saves and
the game's settings to `saves-backup/` in this checkout and keeps the newest
10 (`--help` for more). Or copy that folder somewhere else. `git pull` and
`./setup.sh` never touch it: git ignores everything under `local/` except the
empty `local/data` itself.

88
tools/backup-saves.sh Executable file
View File

@ -0,0 +1,88 @@
#!/bin/bash
# Back up the title's saves and settings.
# usage: tools/backup-saves.sh [--dest DIR] [--keep N] [--data DIR]
set -euo pipefail
umask 077
root=$(cd "$(dirname "$(readlink -f "$0")")/.." && pwd)
dest=${SAVES_BACKUP_DIR:-$root/saves-backup}
keep=${SAVES_BACKUP_KEEP:-10}
data=${SAVES_DATA_DIR:-}
usage() {
cat <<'EOF'
usage: tools/backup-saves.sh [--dest DIR] [--keep N] [--data DIR]
--dest DIR where the archives go (default: <checkout>/saves-backup)
--keep N keep the newest N archives per data directory (default: 10; 0 keeps all)
--data DIR one title data directory instead of local/data/*
The environment variables SAVES_BACKUP_DIR, SAVES_BACKUP_KEEP and
SAVES_DATA_DIR set the same defaults.
EOF
}
while (( $# )); do
case $1 in
--dest) dest=${2:?--dest needs a directory}; shift 2;;
--keep) keep=${2:?--keep needs a number}; shift 2;;
--data) data=${2:?--data needs a directory}; shift 2;;
-h|--help) usage; exit 0;;
*) usage >&2; exit 2;;
esac
done
if ! [[ $keep =~ ^[0-9]+$ ]]; then
echo "saves: --keep must be a number" >&2
exit 2
fi
if [ -n "$data" ]; then
sources=("$data")
where=$data
else
shopt -s nullglob
sources=("$root"/local/data/*)
shopt -u nullglob
where=$root/local/data
fi
found=0
for src in "${sources[@]}"; do
[ -d "$src" ] || continue
files=()
[ -d "$src/saves" ] && files+=(saves)
[ -e "$src/storage/settings" ] && files+=(storage/settings)
(( ${#files[@]} )) || continue
found=1
id=$(basename "$src")
id=${id//[^A-Za-z0-9._-]/_}
mkdir -p "$dest"
out=$dest/victory-saves-$id-$(date +%Y%m%d-%H%M%S).tar.gz
if [ -e "$out" ]; then
out=$dest/victory-saves-$id-$(date +%Y%m%d-%H%M%S)-$$.tar.gz
fi
if ! tar czf "$out.part" -C "$src" "${files[@]}"; then
rm -f "$out.part"
echo "saves: backup of $src failed; not updating" >&2
exit 1
fi
mv "$out.part" "$out"
chmod 600 "$out"
echo "saves: $out"
echo "saves: restore with: tar xzf \"$out\" -C \"$src\""
if (( keep > 0 )); then
(
cd "$dest"
mapfile -t old < <(ls -1t victory-saves-$id-*.tar.gz 2>/dev/null | tail -n +$((keep + 1)))
if (( ${#old[@]} )); then
rm -f -- "${old[@]}"
fi
)
fi
done
if (( ! found )); then
echo "saves: none found under $where, nothing to back up"
fi