Add backup, restore scripts
This commit is contained in:
46
ldap_restore
Executable file
46
ldap_restore
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/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."
|
||||
|
||||
# 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 -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 -delete
|
||||
assert "FATAL: could not clean /var/lib/ldap before restoring."
|
||||
|
||||
# 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."
|
||||
Reference in New Issue
Block a user