Files
OpenVPN-Monitoring-Simple/deploy/native/restore.sh
T
ayurishchevandClaude Sonnet 5.5 4abd2044f0 Scenario 2: install and update as system services (Alpine, Debian, Ubuntu)
- deploy/native: install.sh, update.sh (backup, new code prepared aside,
  stop services, separate DB migration phase, start, automatic rollback;
  OpenVPN keeps running), backup.sh, restore.sh, build-ui.sh (Node 20
  container), lib.sh OS adapter (OpenRC and systemd).
- Root helper reads /etc/ovpmon/helper.conf (service manager, unit, config
  path), validated strictly; process.py/server.py honour OVPMON_RUNTIME,
  OVPMON_OPENVPN_SERVICE, OVPMON_OPENVPN_CONFIG. Debian/Ubuntu use
  openvpn-server@server and /etc/openvpn/server/server.conf.
- Service definitions rewritten (systemd, OpenRC). Fixes found by real runs:
  no seccomp-based options in the profiler unit (they imply NoNewPrivileges
  and break setuid doas); OpenRC start_pre extends PATH instead of
  replacing it; helper syntax check works on a read-only tree.
- Verified in containers with real init systems (Debian 12, Ubuntu 24.04,
  Alpine 3.23): install, PKI/server config/OpenVPN start, update on
  pre-versioned databases, rollback, backup/restore.
- Test: test_helper_config.py. Docs: Scenario 2 guide, change summary,
  README, Index.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-05 10:45:24 +03:00

92 lines
3.9 KiB
Bash
Executable File

#!/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/Scenario2_System_Services.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