Files
OpenVPN-Monitoring-Simple/deploy/docker/restore.sh
T

80 lines
3.1 KiB
Bash
Raw Normal View History

#!/bin/sh
# Restore the five data volumes from a backup directory (made by backup.sh or update.sh).
#
# deploy/docker/restore.sh BACKUP_DIR [--variant a|b] [--project NAME] [--yes] [--start]
#
# BACKUP_DIR e.g. backups/20261005T101500Z
# --start start the stack after the restore (default: only print the command)
#
# The application containers are stopped (after confirmation); the CURRENT content of the volumes is
# first saved to backups/<timestamp>-pre-restore (OVPMON_NO_SAFETY_BACKUP=1 skips it), then each volume
# is emptied and refilled from its archive. Volumes are never removed. .env is not touched: the copy
# in the backup is only reported. To also go back to the previous images use: update.sh --rollback
# See DOCS/Deployment/Scenario1_Containers.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
. "$SCRIPT_DIR/lib.sh"
usage() {
sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//'
}
ARG_VARIANT=""
ARG_PROJECT=""
ASSUME_YES=0
START=0
SRC=""
while [ $# -gt 0 ]; do
case "$1" in
--variant) [ $# -ge 2 ] || die "--variant needs a value"; ARG_VARIANT="$2"; shift 2 ;;
--project) [ $# -ge 2 ] || die "--project needs a value"; ARG_PROJECT="$2"; shift 2 ;;
--yes|-y) ASSUME_YES=1; shift ;;
--start) START=1; shift ;;
-h|--help) usage; exit 0 ;;
-*) usage >&2; die "unknown option: $1" ;;
*) [ -z "$SRC" ] || die "only one backup directory can be given"; SRC="$1"; shift ;;
esac
done
[ -n "$SRC" ] || { usage >&2; die "BACKUP_DIR is required"; }
[ -d "$SRC" ] || die "not a directory: $SRC"
SRC=$(cd "$SRC" && pwd)
trap release_lock EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
need_docker
resolve_stack
acquire_lock
# The archives belong to the volumes of one project
BK_PROJECT=$(sed -n 's/^project=//p' "$SRC/MANIFEST" 2>/dev/null | tail -n 1 || true)
if [ -n "$BK_PROJECT" ] && [ "$BK_PROJECT" != "$PROJECT" ]; then
die "the backup belongs to project '$BK_PROJECT', this stack uses '$PROJECT' (use --project $BK_PROJECT)"
fi
log "backup: $SRC"
[ -f "$SRC/MANIFEST" ] && sed 's/^/ /' "$SRC/MANIFEST"
[ -f "$SRC/COMPLETE" ] || warn "the backup is not marked COMPLETE (an interrupted backup?)"
log "verifying the backup"
verify_backup "$SRC" || die "the backup is damaged or incomplete: nothing was changed"
if [ -f "$SRC/.env" ] && [ -f "$ENV_FILE" ] && ! cmp -s "$SRC/.env" "$ENV_FILE"; then
warn ".env differs from the copy in the backup ($SRC/.env); it is NOT restored. A different JWT_SECRET logs everybody out."
fi
log "the content of the volumes of project $PROJECT will be REPLACED by this backup (the current content is saved first)"
confirm "Stop the stack and restore?" || die "aborted by the user"
if stack_running; then
log "stopping the application containers"
dc stop
fi
restore_volumes "$SRC" 1 || die "the restore failed (see above). The backup is intact: $SRC"
log "restore complete"
if [ "$START" = 1 ]; then
dc up -d
wait_healthy || die "the stack did not become healthy after the restore"
else
log "start the stack when ready: $DC_BIN -p $PROJECT -f $(basename "$COMPOSE_FILE") up -d"
fi