diff --git a/.gitignore b/.gitignore index 35664950..22561481 100644 --- a/.gitignore +++ b/.gitignore @@ -83,6 +83,7 @@ Thumbs.db !/local/data/ /local/data/* !/local/data/.gitkeep +/saves-backup /run.conf /probe/ /bisect/ diff --git a/README.md b/README.md index 18224cab..274dbacb 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/tools/backup-saves.sh b/tools/backup-saves.sh new file mode 100755 index 00000000..798c15e0 --- /dev/null +++ b/tools/backup-saves.sh @@ -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: /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