Preparing for disaster: exporting the directory with slapcat, restoring with slapadd, backing up the mdb database files, replicating for availability, and a complete disaster recovery plan.

Episode 24 deals with what nobody wants to happen but everybody must prepare for: data loss. A directory that holds every password, name, and group membership is a single point of failure if it's lost. You'll learn the slapcat/slapadd export/import cycle, backing up the mdb database files directly, and a disaster recovery plan that includes replication.
Backup for LDAP follows the same principles as for any database, with LDAP-specific details:
cn=config) is as critical as user data; losing it means losing ACLs, overlays, and replication settings.The classic tool is slapcat, which dumps the entire database — including operational attributes like entryUUID, contextCSN, and pwdChangedTime that ldapsearch filters out by default.
slapcat -b dc=example,dc=com -l /backup/ldap-data.ldif
slapcat -b cn=config -l /backup/ldap-config.ldifRun slapcat as the openldap user (or root, but fix ownership afterward) so file permissions stay sane. The result is a full, consistent snapshot — not just the visible data.
Important
Don't import a cn=config dump into a live system with ldapadd or ldapmodify — that's how configs get corrupted. Configuration restore uses slapadd with slapd stopped, or a full reinstall with the config file pointed at the dump. User data, in contrast, can be re-imported live with ldapadd.
slapadd is the offline importer, used with slapd stopped. The user data restore:
sudo systemctl stop slapd
sudo -u openldap slapadd -l /backup/ldap-data.ldif
sudo systemctl start slapdMake sure slapd is stopped and the destination database is empty (or use -c to skip existing entries). After starting, verify the count:
ldapsearch -x -H ldap://ldap.example.com -b dc=example,dc=com dn | wc -l
getent passwd budiThe database itself is a file (or set of files) on disk. Under olcDbDirectory — typically /var/lib/ldap on Debian and Ubuntu — data.mdb is the main store. Two ways to back it up:
data.mdb and lock.mdb. The copy must be consistent — mdb is a copy-on-write B-tree, so an online copy is usually safe, but a stopped server is always safest.slapcat produces a portable, version-independent format that survives an OpenLDAP upgrade; files copied between versions may not.sudo systemctl stop slapd
sudo tar czf /backup/ldap-mdb.tar.gz /var/lib/ldap
sudo systemctl start slapdThe LDIF method is the recommended primary backup because it's human-readable, diff-able, and upgrade-safe; the file copy is a fast emergency snapshot.
Automate with cron. A minimal script:
#!/bin/bash
set -euo pipefail
STAMP=$(date +%Y%m%d-%H%M)
sudo -u openldap slapcat -b dc=example,dc=com -l "/backup/ldap-data-$STAMP.ldif"
sudo -u openldap slapcat -b cn=config -l "/backup/ldap-config-$STAMP.ldif"
find /backup -name '*.ldif' -mtime +30 -delete15 2 * * * /usr/local/sbin/backup-ldap.shThen test the restore at least once a month in a staging instance — this catches the errors that turn a "backup" into a false sense of security.
Replication from episode 13 is availability, not a substitute for backup:
slapcat mis-typed delete, an ACL mistake, or an LDAP injection propagates to every node instantly.slapcat for recovery, and periodic restore testing for confidence.A deleted entry propagates to all replicas within seconds of the refreshAndPersist push. Only the scheduled backup holds the pre-delete state.
A minimal DR plan:
cn=config separately; test a config-only recovery.sudo apt install slapd ldap-utils
sudo systemctl stop slapd
sudo -u openldap slapadd -l /backup/ldap-config.ldif
sudo -u openldap slapadd -l /backup/ldap-data.ldif
sudo systemctl start slapdIn this episode 24 you understood LDAP backup and restore: the principles of consistency and the 3-2-1 rule; slapcat for full LDIF export including operational attributes; slapadd for offline import; backing up the mdb database files; automation with cron; the role of replication as availability rather than backup; and a complete DR plan with RPO and RTO.
Key takeaways:
cn=config too — losing ACLs and overlays is as bad as losing data.In the next episode, episode 25, we optimize what you've built: performance tuning — mdb database tuning, index tuning, and operating system and connection limits for high-load directories.