103 lines
3.8 KiB
Bash
103 lines
3.8 KiB
Bash
#!/bin/sh
|
|||
|
|
# Back up everything that matters of a system-services installation (usable from cron).
|
||
|
|
#
|
||
|
|
# deploy/native/backup.sh [--prefix DIR] [--stop] [--keep N]
|
||
|
|
#
|
||
|
|
# --prefix install directory (default: from /etc/ovpmon/deploy.state, else /opt/OpenVPN-Monitoring-Simple)
|
||
|
|
# --stop stop the application services during the backup and start them again afterwards
|
||
|
|
# (OpenVPN keeps running). The databases are copied with the SQLite backup API either way, so
|
||
|
|
# they are consistent without --stop; --stop only makes the PKI/client files and the databases
|
||
|
|
# one single moment in time
|
||
|
|
# --keep N afterwards keep only the newest N backups (default: keep all)
|
||
|
|
#
|
||
|
|
# Result: /var/backups/ovpmon/<UTC timestamp>/ (mode 700, files mode 600; OVPMON_BACKUP_DIR changes the root):
|
||
|
|
# database copies, /etc/ovpmon, /etc/openvpn, PKI (easy-rsa), client profiles, code with virtualenvs, helper,
|
||
|
|
# service definitions, nginx site, UI files, doas rules, MANIFEST, SHA256SUMS and COMPLETE (written last).
|
||
|
|
# Exit code 0 only if the backup was written and verified. Restore: deploy/native/restore.sh <directory>
|
||
|
|
# The backup holds private keys and password hashes: keep it private and store copies off the host.
|
||
|
|
# See DOCS/Deployment/Scenario2_System_Services.md
|
||
|
|
set -eu
|
||
|
|
|
||
|
|
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
||
|
|
. "$SCRIPT_DIR/lib.sh"
|
||
|
|
|
||
|
|
usage() {
|
||
|
|
sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//'
|
||
|
|
}
|
||
|
|
|
||
|
|
PREFIX=""
|
||
|
|
STOP=0
|
||
|
|
KEEP=""
|
||
|
|
STOPPED=0
|
||
|
|
FORCE=1
|
||
|
|
|
||
|
|
cleanup() {
|
||
|
|
_rc=$?
|
||
|
|
set +e
|
||
|
|
if [ "$STOPPED" = 1 ]; then
|
||
|
|
log "starting the application services again"
|
||
|
|
start_app_services >/dev/null 2>&1 || warn "could not start all services again: start them (ovpmon-api, ovpmon-gatherer, ovpmon-profiler)"
|
||
|
|
STOPPED=0
|
||
|
|
fi
|
||
|
|
release_lock
|
||
|
|
exit "$_rc"
|
||
|
|
}
|
||
|
|
|
||
|
|
while [ $# -gt 0 ]; do
|
||
|
|
case "$1" in
|
||
|
|
--prefix) [ $# -ge 2 ] || die "--prefix needs a value"; PREFIX="$2"; shift 2 ;;
|
||
|
|
--stop) STOP=1; shift ;;
|
||
|
|
--keep) [ $# -ge 2 ] || die "--keep needs a value"; KEEP="$2"; shift 2 ;;
|
||
|
|
-h|--help) usage; exit 0 ;;
|
||
|
|
*) usage >&2; die "unknown option: $1" ;;
|
||
|
|
esac
|
||
|
|
done
|
||
|
|
case "$KEEP" in
|
||
|
|
""|[1-9]|[1-9][0-9]*) ;;
|
||
|
|
*) die "--keep must be a positive number" ;;
|
||
|
|
esac
|
||
|
|
|
||
|
|
trap cleanup 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="$DEFAULT_PREFIX"
|
||
|
|
valid_prefix "$PREFIX" || die "invalid install directory '$PREFIX'"
|
||
|
|
[ -d "$PREFIX/APP_CORE" ] && [ -d "$PREFIX/APP_PROFILER" ] && [ -f "$ENV_FILE" ] \
|
||
|
|
|| die "no installation found (need $PREFIX/APP_CORE, $PREFIX/APP_PROFILER and $ENV_FILE): wrong --prefix?"
|
||
|
|
|
||
|
|
log "backup of the installation in $PREFIX"
|
||
|
|
mkdir -p "$BACKUP_ROOT"
|
||
|
|
chmod 700 "$BACKUP_ROOT"
|
||
|
|
_kb=$(du -sk "$PREFIX/APP_CORE" "$PREFIX/APP_PROFILER" "$DATA_DIR" "$ETC_DIR" /etc/openvpn "$WEB_ROOT" 2>/dev/null | awk '{ s += $1 } END { print s + 0 }')
|
||
|
|
_need=$(( _kb / 1024 + 50 ))
|
||
|
|
[ "$_need" -ge "$MIN_FREE_BACKUP_MB" ] || _need="$MIN_FREE_BACKUP_MB"
|
||
|
|
check_free "$BACKUP_ROOT" "$_need" "the backup"
|
||
|
|
|
||
|
|
BACKUP_DIR="$BACKUP_ROOT/$(stamp)"
|
||
|
|
[ ! -e "$BACKUP_DIR" ] || die "backup directory already exists: $BACKUP_DIR (retry in a second)"
|
||
|
|
mkdir -p "$BACKUP_DIR"
|
||
|
|
chmod 700 "$BACKUP_DIR"
|
||
|
|
|
||
|
|
if [ "$STOP" = 1 ]; then
|
||
|
|
_any=0
|
||
|
|
for _s in $SERVICES; do svc_active "$_s" && _any=1; done
|
||
|
|
if [ "$_any" = 1 ]; then
|
||
|
|
log "stopping the application services for a single-moment copy"
|
||
|
|
STOPPED=1
|
||
|
|
stop_app_services || die "could not stop the services"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
make_backup "$BACKUP_DIR" backup "$PREFIX" || die "the BACKUP FAILED (incomplete files in $BACKUP_DIR are never used by restore or rollback)"
|
||
|
|
|
||
|
|
if [ -n "$KEEP" ]; then
|
||
|
|
prune_backups "$KEEP"
|
||
|
|
fi
|
||
|
|
log "backup complete and verified: $BACKUP_DIR"
|