54 lines
2.0 KiB
Bash
Executable File
54 lines
2.0 KiB
Bash
Executable File
#!/bin/bash
|
|
assert(){ [[ $? -eq 0 ]] || { [[ -n ${1} ]] && echo ${@} ; exit 1 ; } }
|
|
|
|
[[ -f /var/backups/ldap/0.ldif ]]
|
|
assert "Unable to restore backup. Missing /var/backups/ldap/0.ldif backup of cn=config."
|
|
|
|
[[ -f /var/backups/ldap/1.ldif ]]
|
|
assert "Unable to restore backup. Missing /var/backups/ldap/1.ldif backup of first database."
|
|
|
|
[[ -n "${LDAP_DOMAIN}" ]]
|
|
assert "FATAL: Please set LDAP_DOMAIN and retry."
|
|
DN0="dc=${LDAP_DOMAIN//./,dc=}"
|
|
LDAP_DOMAIN_DN=${LDAP_DOMAIN_DN:=${DN0}}
|
|
|
|
# Backup and clean existing config directory
|
|
tar czf /var/backups/ldap/etc_ldap_slapd_d-$(date '+%Y-%m-%d').tar.gz /etc/ldap/slapd.d
|
|
assert "FATAL: could not backup /etc/ldap/slapd.d before restoring."
|
|
find /etc/ldap/slapd.d -mindepth 1 -delete
|
|
assert "FATAL: could not clean /etc/ldap/slapd.d before restoring."
|
|
|
|
# Backup and clean existing data directory
|
|
tar czf /var/backups/ldap/var_lib_ldap-$(date '+%Y-%m-%d').tar.gz /var/lib/ldap
|
|
assert "FATAL: could not backup /var/lib/ldap before restoring."
|
|
find /var/lib/ldap -mindepth 1 -delete
|
|
assert "FATAL: could not clean /var/lib/ldap before restoring."
|
|
mkdir "/var/lib/ldap/${LDAP_DOMAIN_DN}"
|
|
assert "FATAL: could not create /var/lib/ldap/${LDAP_DOMAIN_DN}."
|
|
|
|
# Restore cn=config
|
|
echo "Restoring cn=config..."
|
|
slapadd -n 0 -F /etc/ldap/slapd.d -l /var/backups/ldap/0.ldif
|
|
assert "FATAL: error restoring cn=config using slapadd."
|
|
chown -R openldap:openldap /etc/ldap/slapd.d
|
|
assert "FATAL: could not fix /etc/ldap/slapd.d permissions."
|
|
|
|
for LDIF in /var/backups/ldap/*.ldif
|
|
do
|
|
# Check if it's cn=config backup
|
|
if [[ "${LDIF}" == "/var/backups/ldap/0.ldif" ]]
|
|
then continue
|
|
else
|
|
INDEX=$(basename "${LDIF}" | sed 's/.ldif$//g')
|
|
[[ "${INDEX}" =~ ^[0-9]+$ ]]
|
|
assert "FATAL: could not determine the DB index for ${LDIF}."
|
|
|
|
echo "Restoring database ${INDEX}..."
|
|
slapadd -n ${INDEX} -F /etc/ldap/slapd.d -l "${LDIF}"
|
|
assert "FATAL: error restoring database ${INDEX} using slapadd."
|
|
fi
|
|
done
|
|
|
|
chown -R openldap:openldap /var/lib/ldap
|
|
assert "FATAL: could not fix /var/lib/ldap permissions."
|