Files

91 lines
3.9 KiB
Bash
Raw Permalink Normal View History

#!/bin/sh
# Restore an installation from a backup directory (made by backup.sh or update.sh).
#
# deploy/native/restore.sh BACKUP_DIR [--prefix DIR] [--yes] [--start]
#
# BACKUP_DIR e.g. /var/backups/ovpmon/20261005T101500Z
# --prefix install directory (default: from /etc/ovpmon/deploy.state, else /opt/OpenVPN-Monitoring-Simple)
# --start start the services after the restore (default: only print the command)
#
# The application services are stopped (after confirmation). The CURRENT state is first saved to
# <backups>/<timestamp>-pre-restore (OVPMON_NO_SAFETY_BACKUP=1 skips it); then code, databases,
# /etc/ovpmon, /etc/openvpn, PKI, client profiles, helper, service definitions, nginx site, UI and doas
# rules are put back from the backup. OpenVPN is not touched. The packages and the ovpmon user must
# exist (on a new host run install.sh first, then restore).
# See DOCS/Deployment/System_Services/README.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
. "$SCRIPT_DIR/lib.sh"
usage() {
sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'
}
PREFIX=""
ASSUME_YES=0
START=0
SRC=""
FORCE=1
RESTORE_STAMP=$(stamp)
while [ $# -gt 0 ]; do
case "$1" in
--prefix) [ $# -ge 2 ] || die "--prefix needs a value"; PREFIX="$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=$(abs_dir "$SRC")
trap release_lock EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
need_root
check_os
require_init
check_gnu_tar
acquire_lock
[ -n "$PREFIX" ] || PREFIX=$(state_get OVPMON_PREFIX)
[ -n "$PREFIX" ] || PREFIX=$(manifest_get "$SRC" prefix)
[ -n "$PREFIX" ] || PREFIX="$DEFAULT_PREFIX"
valid_prefix "$PREFIX" || die "invalid install directory '$PREFIX'"
getent passwd "$SERVICE_USER" >/dev/null 2>&1 || die "user $SERVICE_USER does not exist: run install.sh first"
BK_PREFIX=$(manifest_get "$SRC" prefix)
if [ -n "$BK_PREFIX" ] && [ "$BK_PREFIX" != "$PREFIX" ]; then
die "the backup was made for install directory '$BK_PREFIX', this host uses '$PREFIX' (use --prefix $BK_PREFIX)"
fi
log "backup: $SRC"
[ -f "$SRC/MANIFEST" ] && sed 's/^/ /' "$SRC/MANIFEST"
[ -f "$SRC/COMPLETE" ] || warn "the backup is not marked COMPLETE (an interrupted backup?)"
[ "$(manifest_get "$SRC" init)" = "$INIT" ] || warn "the backup comes from a host with service manager '$(manifest_get "$SRC" init)', this one uses '$INIT': service definitions and the helper configuration are restored as saved"
log "verifying the backup"
verify_backup "$SRC" || die "the backup is damaged or incomplete: nothing was changed"
log "code, databases, configuration, PKI and client profiles will be REPLACED by this backup (the current state is saved first)"
confirm "Stop the services and restore?" || die "aborted by the user"
stop_app_services || die "could not stop the application services"
if [ "${OVPMON_NO_SAFETY_BACKUP:-0}" != 1 ]; then
_safe="$BACKUP_ROOT/$(stamp)-pre-restore"
log "saving the current state first: $_safe"
mkdir -p "$BACKUP_ROOT"; chmod 700 "$BACKUP_ROOT"
make_backup "$_safe" pre-restore "$PREFIX" || die "the safety backup failed: nothing was changed"
fi
restore_all "$SRC" "$PREFIX" full || die "the restore failed (see above). The backup is intact: $SRC"
log "restore complete"
if [ "$START" = 1 ]; then
svc_defs_reload || true
start_app_services || die "the services did not start after the restore"
health_check "$(state_get OVPMON_HTTPS_PORT)" || die "the health checks failed after the restore"
else
log "start the services when ready: $(for _s in $SERVICES; do printf '%s ' "$_s"; done)(rc-service <name> start / systemctl start <name>)"
fi