Protecting Keycloak from disaster with database and configuration backups, putting together a disaster recovery plan with RTO and RPO targets, and running safe version upgrades and realm migrations.

In episode 28 you made the cluster fast and measurable. Episode 29 answers the question people like least but matters most: what if everything gets destroyed? Backups protect data; disaster recovery restores service; upgrades keep security and features current. These three capabilities are the insurance a production deployment must not be without.
Keycloak stores almost everything important in the database. But it's not only the database that must be backed up:
| Component | How to back up | Notes |
|---|---|---|
| Database | pg_dump for PostgreSQL | User, session, client, realm data |
| Realm configuration | kc.sh export | Resources, policies, permissions |
| Custom themes | version control + artifact | Themes aren't in the database |
| Custom extensions | container image + source | Provider JARs must be stored too |
| Secrets and config | environment / vault | Never in a repository |
Database backup is done with the database's native utility:
pg_dump -h db.example.com -U keycloak -Fc keycloak > keycloak-$(date +%F).dumppg_dump -Fc produces a binary archive that's easy to restore and compress. Store backups in a separate location — ideally object storage in a different region — and automate with cron or a systemd timer. A backup that only humans remember eventually never happens.
The principle is simple: if data is lost and can't be rebuilt, it must be backed up. Themes and extensions can indeed be rebuilt from source, but the database and realm configuration cannot. A good backup is also one that has been restored — schedule periodic restore tests into an empty database, then run a login health check so you know the backup files aren't corrupt.
Realm configuration export completes the database backup:
kc.sh export --dir /backup/keycloak-config --realm bankkc.sh export describes the realm as files — very useful for moving configuration between environments (client policies from episode 26, authorization policies from episode 25) without carrying user data.
A backup has no value without a tested recovery procedure. Design a disaster recovery plan with two key numbers:
| Target | Meaning | Example |
|---|---|---|
| RTO (Recovery Time Objective) | maximum time for the service to recover | 4 hours |
| RPO (Recovery Point Objective) | how much data may be lost | 15 minutes |
RPO determines backup frequency: an RPO of 15 minutes means backup or replication must run every 15 minutes. RTO determines recovery automation: the tighter the RTO, the more processes must be automatic. For multi-region, consider database replication between regions and DNS failover; without that, the realistic strategy is backup and restore to a standby region.
Multi-region adds cost and replication complexity. For most teams, a backup-and-restore strategy to a standby region is sufficient — make sure its RTO meets the SLA promise before chasing a full multi-region topology. RTO and RPO also aren't just numbers on a document: both must be tested, starting from realistic figures and tightening as automation improves.
Upgrading Keycloak is part of the lifecycle, not a rare event. The safe steps:
An example rolling upgrade flow:
# 1. Take the first node out of the load balancer rotation
# 2. Run the new version's image on that node
# 3. Wait for /health/ready to indicate it's healthy
# 4. Put it back, then repeat for the next nodeThe rolling upgrade preserves the SLA: as long as one node is healthy, users don't feel a thing. Especially for database migration, run one node first and watch the logs before other nodes follow suit with the new version.
Pick an upgrade window during quiet hours and test in the staging tenant first. Small and regular upgrades are safer than rare big jumps — the closer your version is to the latest supported one, the smaller the migration leaps you'll have to face.
Carrying configuration between realms or environments is daily work:
kc.sh export and kc.sh import move the entire realm definition: clients, users, groups, roles, policies, and permissions. Import into a new environment:kc.sh import --dir /backup/keycloak-configDon't forget to back up the deployment configuration itself: environment values, compose files, and customization files. Without them, restoring a database to a new machine remains hard because the outer layer surrounding Keycloak is incomplete.
Warning
Never import production configuration into a running environment without reviewing its contents. Export/import is a transfer tool, not a merger tool — overwritten configuration can wipe out already-tuned settings.
Episode 29 protected your deployment: database backups with pg_dump, configuration backups with kc.sh export, disaster recovery with clear RTO and RPO targets, safe version upgrades with backups and rolling upgrades, and tested realm migrations between environments.
Key takeaways:
In episode 30 — the final episode — you'll tie everything together: troubleshooting & best practices for facing real problems and a complete checklist before launching Keycloak into production.