Setup High Availability Fail Over
- 1 What this gives you
- 2 Scripts
- 3 Checklist A: before you start (every server)
- 4 Checklist B: WireGuard (off-site server as the hub)
- 5 Checklist C: build the cluster
- 6 Checklist D: monitoring and backups (every server)
- 7 Checklist E: nightly snapshot copy (optional, off-site server)
- 8 Checklist F: after ANY failover
- 9 Procedure: unplanned failover (primary is down)
- 10 Procedure: planned switchover or failback
- 11 Decisions to make for your site
- 12 Troubleshooting
- 13 Getting the scripts
- 14 Legacy procedure (old Python installer, 2024)
Status: applies to OpenELIS Global 3.x installed with a Docker Compose distro. Scripts tested on Oct 2, 2026 with openelis-distro-png (main @ e11b3a4, OpenELIS :develop images, PostgreSQL 14.24) on three VMs. The other Compose distros (openelis-docker, Madagascar, Indonesia, the distro template) use the same service and container names and should work the same way, but test on yours first. Replaces the 2024 procedure, which targeted the old Python installer.
Site runbooks that follow this page: CPHL High Availability Fail Over (Papua New Guinea). Record your own site's server names, addresses and open decisions on your country page, and keep this page generic.
What this gives you
High availability here means a second (and third) copy of the OpenELIS database that is never more than a moment behind, and a tested way to switch to it. It uses PostgreSQL streaming replication.
Primary: the one server that runs the full OpenELIS stack and accepts writes.
Standby: a server that runs only the database, continuously copying the primary. You can have several. A common layout is:
an on-site standby in the same building, on the same LAN, for fast failover when the primary hardware fails;
an off-site standby (a cloud VPS or a second building) for disaster recovery. It can also be the VPN hub for remote access and host a nightly snapshot copy (below).
OpenELIS cannot run against a read-only standby. To use a standby you promote it to primary, and the old primary must be fenced (stopped) first. Never run two writable servers.
Replication is asynchronous. On an unplanned failover you lose whatever had not reached the standby yet: under a second on a LAN, longer over a slow WAN link.
Replication is not a backup. A record deleted on the primary is deleted on every standby within a second. Keep nightly
scripts/backup.shbackups.Failover is manual and deliberate: a person decides, runs one script, and follows the "after failover" checklist. Nothing switches automatically.
What was tested
Drill | Result |
|---|---|
Move production from one server to another (old server becomes a standby) | OK, no data loss; rejoin needed no rewind |
On-site standby over the LAN, off-site standby over WireGuard | both streaming, 0 bytes behind |
Unplanned failover: primary hard power-off, promote on-site standby, re-point off-site standby | OK, last row written before the crash present |
Old primary boots as a primary after a failover | boot guard fenced it in about 8 s; rejoin rewound 53 MB |
Old primary cut off from the network (not rebooted) while a standby was promoted | periodic guard fenced it within 40 s of reconnecting |
Off-site link down while 50,000 rows were written | slot held the WAL, |
Full-stack planned switchover and failback | webapp healthy in about 2 min, admin login works, no configuration CSVs reloaded |
Real-divergence rewind immediately after a promotion | OK |
Nightly snapshot copy on the off-site server | rebuilt in 2 to 5 min, warning banner shown, login works, outbound integrations off |
Reboot ordering | PostgreSQL binds to the WireGuard address after a reboot |
Scripts
All in scripts/ha/ of the distro, run as root from the distro directory (for example sudo ./scripts/ha/status.sh). See Getting the scripts if your distro does not have them yet.
Script | Run on | What it does |
|---|---|---|
| primary | replicator role, WAL settings, pg_hba for cluster members, replication slots |
| new standby | full copy with pg_basebackup, starts the database only, as a standby |
| any | role, timeline, standbys, lag, slots; |
| old primary | stops and removes every container so the server cannot accept writes |
| a standby | promotes it, creates slots, starts OpenELIS, rebuilds the search index; safe to re-run |
| old primary, other standbys | pg_rewind and follow the new primary; falls back to a full copy |
| primary (cron) / promote | carries OpenELIS's configuration checksum files to the standbys through the database |
| every server | split-brain guard at boot and every 2 minutes; makes Docker wait for WireGuard |
| off-site server | rebuilds the nightly snapshot copy |
Checklist A: before you start (every server)
.env is identical on every server for OE_DB_PASSWORD, ADMIN_PASSWORD, OE_ADMIN_PASSWORD and SSL_*. A standby carries the primary's database, including its passwords.OE_DB_PASSWORD, ADMIN_PASSWORD, and POSTGRES_PASSWORD in configs/database/database.env) before port 5432 is published anywhere.HA_MAX_SLOT_WAL_KEEP (default 20 GB) on top of the database.docker save --platform linux/amd64 <image> | ssh <server> docker load..env on each server, and compose.ha.yaml and scripts/ha/ are present.sudo ./scripts/ha/install-guard.sh has been run on every server, then sudo sync (or reboot once).promote.sh, rejoin.sh and standby-init.sh inside tmux or screen, so a dropped SSH session does not stop them.# --- High availability (scripts/ha) ---
COMPOSE_FILE=docker-compose.yml:compose.ha.yaml # add :compose.letsencrypt.yaml if you use it
HA_NODE=site-a # this server's name in the cluster
# every server: name=address[,address] (WireGuard address first, then LAN address)
HA_MEMBERS="offsite=10.88.0.1 site-a=10.88.0.2,192.168.1.10 site-b=10.88.0.3,192.168.1.11"
HA_PG_BIND_WG=10.88.0.2 # this server's WireGuard address
HA_PG_BIND_LAN=192.168.1.10 # this server's LAN address (off-site server: 127.0.0.1)
HA_REPLICATION_PASSWORD=<long random value, same on all servers>List only servers that exist: the primary keeps a replication slot, and WAL, for every member in HA_MEMBERS. If a site has no off-site server and no WireGuard, set HA_PG_BIND_WG to the LAN address and leave HA_PG_BIND_LAN at 127.0.0.1.
Checklist B: WireGuard (off-site server as the hub)
Use this when a standby is off-site or the primary is behind NAT. Every on-site server, and every remote user, connects out to the off-site server, so nothing needs to be opened inbound at the site.
umask 077; wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey/etc/wireguard/wg0.conf:[Interface]
Address = 10.88.0.1/24
ListenPort = 51820
PrivateKey = <off-site private key>
# Docker sets the FORWARD policy to DROP; without these lines the hub cannot
# relay remote users to the on-site servers
PostUp = iptables -I FORWARD -i wg0 -o wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -o wg0 -j ACCEPT
[Peer] # site-a
PublicKey = <site-a public key>
AllowedIPs = 10.88.0.2/32
[Peer] # site-b
PublicKey = <site-b public key>
AllowedIPs = 10.88.0.3/32
[Peer] # remote user (one block per person or device, 10.88.0.10 and up)
PublicKey = <user public key>
AllowedIPs = 10.88.0.10/32echo net.ipv4.ip_forward=1 | sudo tee /etc/sysctl.d/90-wg.conf && sudo sysctl -p /etc/sysctl.d/90-wg.conf/etc/wireguard/wg0.conf:[Interface]
Address = 10.88.0.2/24 # .3 for site-b, .10 and up for users
PrivateKey = <this machine's private key>
[Peer]
PublicKey = <off-site public key>
Endpoint = <off-site public IP>:51820
AllowedIPs = 10.88.0.0/24
PersistentKeepalive = 25 # keeps the NAT mapping opensudo systemctl enable --now wg-quick@wg0ping 10.88.0.1 (off-site) and ping 10.88.0.2 (site-a, relayed through the hub).After WireGuard restarts on the hub, peers can take up to 2 minutes to reconnect.
Remote users reach the primary at its WireGuard address (or a name that resolves to it).
Checklist C: build the cluster
Option 1: the current production server stays the primary
☐ Primary: add the HA block to
.env, deploycompose.ha.yamlandscripts/ha/, thendocker compose up -d(publishes 5432 on the chosen addresses only).☐ Primary:
sudo ./scripts/ha/primary-init.sh, then in a quiet momentsudo ./scripts/ha/primary-init.sh --restart(database restart of a few seconds; the webapp reconnects by itself).☐ Each standby: install the same release, same
.envand HA block, thensudo ./scripts/ha/standby-init.sh <primary address>. On-site standbys use the primary's LAN address; off-site standbys its WireGuard address.☐ Primary:
sudo ./scripts/ha/status.shshows every standbystreaming,0 bytesbehind, all slots active.
Option 2: move production to a different server at the same time
For example from a cloud server to an on-site server. The new server first becomes a standby of the current one, then you switch roles in a short window. This doubles as your first failover rehearsal.
☐ Current server: steps 1 and 2 of Option 1.
☐ New server:
sudo ./scripts/ha/standby-init.sh <current server address>. This is a full copy of the database; over a slow link, check its size first (du -shthe database directory) and allow time.☐ Current server:
status.shshows the new serverstreaming,0 bytes.☐ Announce a short window. Current server:
sudo ./scripts/ha/fence.sh.☐ New server:
sudo ./scripts/ha/promote.sh. Wait for "webapp healthy".☐ Checklist F (users, analyzers, interfaces, certificates). Log in and check that the last sample entered before the window is there.
☐ Old server:
sudo ./scripts/ha/rejoin.sh <new server address>. It becomes a standby.☐ Add any further standbys with
standby-init.sh <new server address>.
Checklist D: monitoring and backups (every server)
Replace /opt/openelis with your distro directory.
*/5 * * * * root /opt/openelis/scripts/ha/status.sh --check || <alert command>. It reports: a standby not streaming, a disconnected slot, a standby silent for 2 minutes, a slot holding more than 2 GB of WAL, the webapp down on the primary, a webapp container on a standby.*/10 * * * * root /opt/openelis/scripts/ha/config-sync.sh push (does nothing on standbys). It stores OpenELIS's configuration checksum files in the database so a promoted standby does not reload every CSV and overwrite catalog changes made in the UI.30 1 * * * root cd /opt/openelis && [ "$(docker compose exec -T db.openelis.org psql -U postgres -Atc 'select pg_is_in_recovery()')" = f ] && ./scripts/backup.sh /srv/oe-backups. Copy /srv/oe-backups off the server. It contains .env; keep it mode 0700.Checklist E: nightly snapshot copy (optional, off-site server)
A second, complete, running OpenELIS on the off-site server (https://<off-site server>:9443/, reachable over the VPN), rebuilt every night from that server's standby. Useful for looking up results and running reports when the site is offline, and for training and demos.
Anything entered in the snapshot is deleted at the next rebuild and is never sent to the live system. Do not use it to enter or change data. Every page shows a red banner saying so.
sudo ./scripts/ha/snapshot-refresh.sh (about 10 minutes the first time). Check the banner and log in.0 2 * * * root /opt/openelis/scripts/ha/snapshot-refresh.sh >> /var/log/oesnap.log 2>&1Switched off in the snapshot: analyzer bridge, FHIR subscriber and remote-source push and poll, client registry, facility and provider list sync, Odoo, freezer monitoring polling. Check any email or SMS notification settings your site uses and switch those off too.
promote.shstops the snapshot automatically if the off-site server is ever promoted.
Checklist F: after ANY failover
promote.sh starts a rebuild; patient search can be incomplete for a few minutes.sudo ./scripts/ha/rejoin.sh <new primary> on each, then status.sh on the primary.Procedure: unplanned failover (primary is down)
Confirm the primary is really down (power, network,
docker compose ps). If it can be fixed in minutes, fix it instead: a failover has a cost.If you can reach it at all, run
sudo ./scripts/ha/fence.shon it. If you cannot, make sure it cannot quietly come back: the guard fences it automatically if it comes up while another server is primary on a newer timeline, but only if it can reach that server.On the chosen standby (the on-site one first; the off-site one if the whole site is down):
sudo ./scripts/ha/promote.sh. If it refuses because the old primary still answers, go back to step 2. Use--forceonly when you are certain the old primary is down.Checklist F.
When the old primary is repaired:
sudo ./scripts/ha/rejoin.sh <new primary>. It never comes back as primary by itself.
Procedure: planned switchover or failback
Current primary:
sudo ./scripts/ha/fence.sh(a clean shutdown sends the last WAL to every standby).Target:
sudo ./scripts/ha/promote.sh.Old primary and the other standbys:
sudo ./scripts/ha/rejoin.sh <new primary>.Checklist F.
Decisions to make for your site
HTTPS on servers without a public port 80. Let's Encrypt HTTP-01 needs port 80 reachable from the internet. For on-site servers behind NAT use DNS-01, a certificate from your organisation's own process, or have the off-site server obtain the certificate and copy it to the others.
How users find the primary after a failover: one DNS name with a short TTL that you move, or a service IP on the LAN that you move to the new primary. A moving address also saves re-pointing analyzers.
Notifications in the snapshot: list the email and SMS settings your site uses so they can be switched off in the snapshot.
Sizing: database size (first copy over the WAN), WAL headroom on the primary, memory on the off-site server.
Who watches the alerts from
status.sh --check, and who is allowed to runpromote.sh.
Troubleshooting
Symptom | Cause / fix |
|---|---|
| Network path down, the standby's address missing from |
| Password stored as md5. Re-run |
| Working as intended. Fence the old primary first. |
| Re-run it; it finishes the remaining steps. |
| pg_rewind could not reuse the data. Over a slow WAN a full copy takes time; let it run in |
Database container fails to bind the WireGuard address after a reboot | WireGuard was not up before Docker. Re-run |
Remote users cannot reach the on-site server through the hub | The hub's |
Host firewall rules (ufw, iptables INPUT) do not block 5432 or 443 | Docker-published ports bypass INPUT. Bind to specific addresses ( |
Primary disk filling with WAL | A standby is disconnected and its slot holds WAL (capped by |
Snapshot webapp never healthy: | Old copy of |
Getting the scripts
Source of truth: scripts/ha/ and compose.ha.yaml in openelis-distro-png, https://github.com/DIGI-UW/openelis-distro-png/pull/10 (branch feat/ha-failover until merged). The tested versions are at commit 9600885:
compose.ha.yaml: publishes PostgreSQL on the WireGuard and LAN addresses only
lib.sh (shared functions, used by every script)
Until the PR is merged and released, copy them into your distro checkout (works from any distro):
cd /opt/openelis # your distro directory
git fetch https://github.com/DIGI-UW/openelis-distro-png.git feat/ha-failover
git checkout FETCH_HEAD -- scripts/ha compose.ha.yaml
sudo chmod 755 scripts/ha/*.shLegacy procedure (old Python installer, 2024)
Kept for reference only. It targets the old setup_OpenELIS.py layout (/var/lib/openelis-global, DB on host port 5432, a backup replication role) and does not work with the Docker Compose distros. Known problems: the "wait for healthy" loop never waits, re-running fails, pg_basebackup -W forces a password prompt, passwords are passed on the command line, the whole live data directory is copied twice, and there is no fencing step.