#!/usr/bin/env bash # Backs up a lore-forge deployment: lore-forge's database and loreserver's repository store, # taken at the same moment. # # scripts/backup.sh # # Each run writes // holding lore-forge.sqlite, lore-store/ and a # MANIFEST, then points /latest at it. Store files that have not changed # since the previous backup are hard links into it, so a run costs about what changed. Only the # newest KEEP backups are kept. # # loreserver writes its store to disk in the background and has no documented way to copy it # while it runs, so the store is copied once while loreserver runs, then loreserver is stopped # for a second copy of what changed meanwhile and for the database, and started again. The # outage lasts the second copy, usually seconds. Stopping loreserver releases every file lock, # which Lore keeps only in memory. # # The secret key is not copied: keep it apart from these backups, since the database holds the # token signing keys it encrypts. A restore needs both. See scripts/restore.sh. # # Settings, from the environment; the defaults match the layout deploy/install.sh makes: # # LORE_FORGE_DATA_DIR lore-forge's data directory /var/lib/lore-forge # LORE_STORE loreserver's store /var/lib/loreserver/store # LORE_FORGE_BIN the lore-forge binary /opt/lore-forge/bin/lore-forge # LORESERVER_BIN loreserver, for its version /opt/lore-forge/bin/loreserver # LORESERVER_STOP command that stops loreserver systemctl stop loreserver # LORESERVER_START command that starts it systemctl start loreserver # KEEP backups to keep 14 # # Needs rsync. set -euo pipefail say() { echo "backup: $*" >&2; } die() { say "$*"; exit 1; } [ $# -eq 1 ] || die "usage: $0 " DEST=$1 DATA_DIR="${LORE_FORGE_DATA_DIR:-/var/lib/lore-forge}" STORE="${LORE_STORE:-/var/lib/loreserver/store}" FORGE_BIN="${LORE_FORGE_BIN:-/opt/lore-forge/bin/lore-forge}" LORESERVER_BIN="${LORESERVER_BIN:-/opt/lore-forge/bin/loreserver}" STOP="${LORESERVER_STOP:-systemctl stop loreserver}" START="${LORESERVER_START:-systemctl start loreserver}" KEEP="${KEEP:-14}" command -v rsync >/dev/null || die "rsync is not installed" [ -x "$FORGE_BIN" ] || die "no lore-forge binary at $FORGE_BIN (set LORE_FORGE_BIN)" [ -f "$DATA_DIR/lore-forge.sqlite" ] || die "no database in $DATA_DIR (set LORE_FORGE_DATA_DIR)" [ -d "$STORE" ] || die "no loreserver store at $STORE (set LORE_STORE)" case "$KEEP" in '' | *[!0-9]* | 0) die "KEEP must be a positive number" ;; esac mkdir -p "$DEST" DEST="$(cd "$DEST" && pwd)" NAME="$(date -u +%Y%m%dT%H%M%SZ)" [ ! -e "$DEST/$NAME" ] || die "$DEST/$NAME already exists" # Written under a temporary name, so a failed run never looks like a backup. PARTIAL="$DEST/.partial-$NAME" rm -rf "$PARTIAL" mkdir -p "$PARTIAL" LINK=() if [ -d "$DEST/latest/lore-store" ]; then LINK=(--link-dest="$(cd "$DEST/latest/lore-store" && pwd)") fi copy_store() { rsync -a --delete ${LINK[@]+"${LINK[@]}"} "$STORE/" "$PARTIAL/lore-store/"; } say "copying the store while loreserver runs" copy_store STOPPED="" restart() { if [ -n "$STOPPED" ]; then say "starting loreserver" sh -c "$START" || say "STARTING LORESERVER FAILED; start it by hand" STOPPED="" fi } trap 'restart; rm -rf "$PARTIAL"' EXIT say "stopping loreserver" STOPPED=1 sh -c "$STOP" started=$(date +%s) copy_store "$FORGE_BIN" backup-database --data-dir "$DATA_DIR" "$PARTIAL/lore-forge.sqlite" >/dev/null restart say "loreserver was stopped for $(($(date +%s) - started)) s" { echo "backup $NAME" echo "lore-forge $("$FORGE_BIN" --version | awk '{print $2}')" if [ -x "$LORESERVER_BIN" ]; then echo "loreserver $("$LORESERVER_BIN" --version | awk '{print $2}')"; fi echo "store $STORE" echo "data $DATA_DIR" } >"$PARTIAL/MANIFEST" mv "$PARTIAL" "$DEST/$NAME" trap - EXIT ln -sfn "$NAME" "$DEST/latest.new" && mv -f "$DEST/latest.new" "$DEST/latest" # The newest KEEP backups stay; names sort by time. all=$(find "$DEST" -mindepth 1 -maxdepth 1 -type d -name '[0-9]*T[0-9]*Z' | sort) count=$(printf '%s\n' "$all" | grep -c . || true) if [ "$count" -gt "$KEEP" ]; then for dir in $(printf '%s\n' "$all" | head -n $((count - KEEP))); do say "removing old backup $(basename "$dir")" rm -rf "$dir" done fi echo "$DEST/$NAME"