Files
OpenVPN-Monitoring-Simple/DOCS/Deployment/System_Services/Operations.md
T

12 KiB

System services: update, backup, restore, rollback

Day-2 operations. Every update takes a verified backup first and rolls back automatically on failure. Install: System services.

Update

deploy/native/update.sh                                   # run from the checkout with the new version
deploy/native/update.sh --ui-dist /root/ui-dist.tar.gz    # install this UI (tarball or directory), always
deploy/native/update.sh --build-ui                        # build the UI from APP_UI on this host and install it
deploy/native/update.sh --source /root/ovpmon-src --yes   # another checkout, no questions
deploy/native/update.sh --no-pull                         # sources already in place / offline
deploy/native/update.sh --rollback                        # manual rollback to the newest backup

Run it from the checkout that holds the new version (the script uses the templates of that checkout). --prefix, the HTTPS port and the extra CSP origins (OVPMON_CSP_CONNECT_EXTRA) come from /etc/ovpmon/deploy.state; --csp-connect-extra "https://api.example.com" changes the origins ("" removes them) and re-renders the nginx site. A site that is not managed by the scripts (marker line removed) is left untouched. The update needs access to PyPI for the new virtualenvs.

Each phase is logged separately:

Phase What happens If it fails
0 Preflight installation present, no unfinished install, source checkout complete, an explicit UI source readable (--build-ui: builder usable), services state, databases found, free space (backup and new code) nothing was changed
1 Backup verified backup in /var/backups/ovpmon/<UTC timestamp>/ (see below) the update is refused, nothing was changed
2 Sources git pull --ff-only of the source checkout (skipped with --no-pull or without git) nothing was changed
3 Prepare new code copied to <prefix>.new, fresh virtualenvs with pip install -r requirements.txt, import check; the UI source is decided (after git pull: --ui-dist, --build-ui, the committed build; unchanged hash means no swap) and the new UI is staged next to the web root. The running system is not touched nothing was changed
4 Stop and swap the three application services are stopped (OpenVPN keeps running, VPN users stay connected); a final consistent snapshot; each code directory is renamed to <dir>.old-<time>, the new one moved in; PKI, client profiles, profiler.log, config.ini and any database files below the code directory are moved over, never replaced; virtualenv paths fixed automatic rollback
5 Database migration its own step, see below automatic rollback, nothing is started
6 Files UI swap (when a new UI was staged); helper; helper.conf (when missing); doas rules; service definitions; missing environment keys added (existing values never changed); nginx site; each replaced only when it differs and is managed by these scripts; units reloaded automatic rollback
7 Start services started, health checks, helper self-test automatic rollback
8 Finish the old code directories are removed (never one that still holds PKI data), the newest 5 backups kept, state recorded

The new deploy/native/*.sh take effect on the next run (the running script is not replaced).

Database migration as its own step

After the services are stopped and the new code is in place, and before anything is started, update.sh runs for each database, with the new code, as user ovpmon and with the variables of /etc/ovpmon/env loaded:

Database Command (working directory)
Profiler APP_PROFILER/venv/bin/python migrate.py in <prefix>/APP_PROFILER
Monitoring APP_CORE/venv/bin/python migrate.py in <prefix>/APP_CORE

For each one: migrate.py --check (exit 0 up to date, 2 pending, 1 error or database newer than the code), the migration when 2, and a second check. The reports are printed. A failure or a "too new" database aborts before any service starts and triggers the rollback. Migrations are additive, run after an integrity check and a consistent copy <db>.bak-<UTC timestamp> next to the database, and are verified afterwards; details in Database migrations. The migration also runs by itself when a service starts, so a manual file replacement is safe too; update.sh only makes it a visible, controlled step.

Backup (phase 1, backup.sh)

deploy/native/backup.sh                 # backup into /var/backups/ovpmon/<UTC timestamp>/
deploy/native/backup.sh --stop          # services stopped during the copy, started afterwards
deploy/native/backup.sh --keep 14       # afterwards keep the newest 14

Cron example (nightly, 14 kept): 30 3 * * * /opt/OpenVPN-Monitoring-Simple/deploy/native/backup.sh --keep 14 >> /var/log/ovpmon-backup.log 2>&1.

Content File in the backup
both databases, consistent copies from the SQLite backup API (integrity-checked, owner and mode kept) db/monitor.db, db/profiler.db
ovpmon_backup.db (settings, destinations and encrypted secrets of the Backup and Restore page; next to the profiler database), same method; optional: skipped if the file does not exist db/backupcfg.db
/etc/ovpmon (environment, helper configuration, certificate, state) etc-ovpmon.tar.gz
/etc/openvpn (server.conf, crl.pem, scripts, ipp.txt) etc-openvpn.tar.gz
PKI APP_PROFILER/easy-rsa easy-rsa.tar.gz
client profiles APP_PROFILER/client-config client-config.tar.gz
APP_CORE and APP_PROFILER including the virtualenvs (needed for rollback) code.tar.gz
helper, service definitions, nginx site, sysctl file, doas file (Alpine) system.tar.gz
doas block (Debian/Ubuntu) doas-block.txt
UI files ui.tar.gz
checksums, description, completion marker SHA256SUMS, MANIFEST, COMPLETE

Every archive is verified (tar -tzf), the checksums are checked and each database copy passes PRAGMA integrity_check before COMPLETE is written; only a COMPLETE backup is ever used. update.sh backs up twice: in phase 1, and again as a final snapshot once the services are stopped (the code archive is reused). The backup holds private keys, password hashes and the signing secret: the directory is mode 700, the files mode 600. Keep copies off the host.

Restore

deploy/native/restore.sh /var/backups/ovpmon/20261005T101500Z            # asks, stops, saves the current state, restores
deploy/native/restore.sh /var/backups/ovpmon/20261005T101500Z --yes --start

It verifies the backup, stops the three application services, saves the current state to /var/backups/ovpmon/<time>-pre-restore (OVPMON_NO_SAFETY_BACKUP=1 skips this), then restores code, databases, /etc/ovpmon, /etc/openvpn, PKI, client profiles, helper, service definitions, nginx site, UI and doas rules. A replaced database is kept as <db>.failed-<time>; ovpmon_backup.db is restored from db/backupcfg.db if the backup has it (backups of older versions do not: the live file is kept). The local backup packages of the Backup and Restore page (/var/lib/ovpmon/backups) are not part of the host backup. OpenVPN is not stopped. On a new host run install.sh first, then restore.sh.

Manual equivalent (services stopped, paths for the default prefix):

B=/var/backups/ovpmon/20261005T101500Z
( cd $B && sha256sum -c SHA256SUMS )
tar -xzpf $B/code.tar.gz -C /opt/OpenVPN-Monitoring-Simple          # after moving the current APP_CORE and APP_PROFILER aside
tar -xzpf $B/easy-rsa.tar.gz -C /opt/OpenVPN-Monitoring-Simple/APP_PROFILER
tar -xzpf $B/client-config.tar.gz -C /opt/OpenVPN-Monitoring-Simple/APP_PROFILER
cp -p $B/db/profiler.db /var/lib/ovpmon/ovpn_profiler.db            # the backup was made with the sqlite backup API
cp -p $B/db/monitor.db  /var/lib/ovpmon/openvpn_monitor.db
cp -p $B/db/backupcfg.db /var/lib/ovpmon/ovpmon_backup.db           # only if the backup has it
tar -xzpf $B/etc-ovpmon.tar.gz -C / && tar -xzpf $B/etc-openvpn.tar.gz -C /
tar -xzpf $B/system.tar.gz -C /                                      # helper, service definitions, nginx site

A single database can also be put back from its migration copy <db>.bak-<time> in /var/lib/ovpmon: stop the services, copy it over the database, start the OLD release.

Automatic rollback and --rollback

On any failure from phase 4 on: the services are stopped, code, databases, /etc/ovpmon, /etc/openvpn, helper, doas rules, service definitions, nginx site and UI files are restored from the backup of this run, the services are started and checked, and the result is reported with the backup path.

How the code is restored: the current (new or half-swapped) code directories are renamed to <dir>.rollback-<time>, code.tar.gz is extracted (old code and old virtualenvs, so no Internet is needed), and the PKI and client profiles, which the update never replaced, are moved back from the directory that held them. They are restored from easy-rsa.tar.gz / client-config.tar.gz only if they are missing. The failed databases are kept as <db>.failed-<time>. If the rollback itself fails the message says where the backup is and how to retry (update.sh --rollback or restore.sh).

update.sh --rollback does the same by hand from the newest COMPLETE backup after a confirmation, with one difference: PKI and client profiles come from the backup too (one consistent set with the databases), and the current state is saved first to <time>-pre-restore. Use it when an update succeeded but the new version misbehaves. Data created after that backup is lost (it is in the -pre-restore copy).

Existing installations (adoption)

A host that was set up by hand (the layout of the old guide: code in /opt/OpenVPN-Monitoring-Simple, /etc/ovpmon/env, init scripts or units, helper and doas rules) can be updated with update.sh: it has no state file, so it is adopted and recorded after the first successful update. Files that these scripts do not manage are left as they are: init scripts, units, an nginx site and a doas file that do not carry the marker line managed by ovpmon deploy/native and differ from the shipped ones are only reported. The helper is replaced (the new one behaves like the old one without helper.conf), helper.conf is written when missing, and OVPMON_RUNTIME, OVPMON_OPENVPN_SERVICE and OVPMON_OPENVPN_CONFIG are added to the environment file only when absent. If the install directory is a git checkout (the source is the install directory), git pull changes the live files, therefore a rollback is armed from phase 2.

What is preserved on update

Item On update
Databases Kept in place, backed up twice (consistent copies), migrated additively with their own <db>.bak-* copy; restored automatically if the update fails after the stop
PKI (APP_PROFILER/easy-rsa): CA, keys, certificates, CRL Moved, never replaced or deleted, backed up
Client profiles (APP_PROFILER/client-config) Moved, never replaced or deleted, backed up
/etc/ovpmon/env (secret, DB paths) Kept; only missing keys are added; copied into every backup
/etc/ovpmon/helper.conf, TLS certificate and key Kept (helper.conf is rewritten only while it carries our marker and differs)
/etc/openvpn (server.conf, crl.pem, scripts, ipp.txt) Not touched by the update; backed up
OpenVPN process and VPN sessions Not stopped or restarted; connected users stay connected
config.ini of both components Kept; the shipped one of the new version is saved as config.ini.dist when it differs
profiler.log, other *.log / *.db files below the code directories Kept (moved)
Code, virtualenvs Replaced (fresh virtualenvs; the old ones are in the backup)
UI files Replaced with --ui-dist / --build-ui, or automatically when the committed build of the source differs from the installed one (.ui-source-hash); kept when there is no UI source; the previous ones are in the backup
Service definitions, nginx site, doas rules, helper Replaced when they differ and are managed by these scripts; the previous ones are in the backup
User sessions Stay valid while the secret is unchanged
Backups in /var/backups/ovpmon The newest 5 kept, older ones pruned after a successful update; -pre-restore copies are never pruned automatically
Data directories (/var/lib/ovpmon, PKI, client profiles) Never deleted by any script