Wednesday, June 1, 2011

How to Backup and Restore OpenLDAP Database

Instead of backup / restore of actual ldap database (hdb, etc) we will export/import ldap directory tree into ldif format that ultimately let us do the same, however without any particular database implementation specifics.

Backup

The backup will be stored in backup.ldif text file.
#!/bin/sh

slapcat -v -l ldap.diff

Restore

The restore will go through replacing current database from a one we have in ldif backup.
#!/bin/sh

# Stop slapd daemon
/etc/init.d/slapd stop

# Remove current database
rm -rf /var/lib/ldap/*

# Import directory tree from backup
slapadd -l backup.ldif

# Fix permissions
chown -R openldap:openldap /var/lib/ldap/*

# Start slapd daemon
/etc/init.d/slapd start

7 comments:

  1. great, very useful.

    ReplyDelete
  2. If it helps anyone, i had to stop slapd first then only i was able to run slapcat.

    ReplyDelete
    Replies
    1. Just in case... I was able backup without a need to stop slapd. Anyway, thank you for sharing your experience.

      Delete
  3. if specific non-standard schemas are used, use

    slapadd -l backup.ldif -f /etc/ldap/slapd.conf

    to make sure the schemas are loaded when importing into the database.

    ReplyDelete
  4. Simply, excellent! Thank you.
    And I neither need to stop slapd to do a backup.

    ReplyDelete
  5. Hey,

    I've tried the command mentioned , but I am getting error
    root@ldap1:~# slapcat -v -l ldap.diff
    53c77bc6 str2entry: invalid value for attributeType objectClass #1 (syntax 1.3.6.1.4.1.1466.115.121.1.38)
    slapcat: bad configuration file!

    Please help

    ReplyDelete
  6. Never Mind worked , But We have to provide the config as well.

    slapcat -v -l ldap.diff -f /etc/ldap/slapd.conf

    Need to provide the configuration as well when running the slapcat.

    And yeah , stopping slapd didnt helped.

    ReplyDelete