Simplify ldap restore hook

This commit is contained in:
Alexandre Aubin 2021-04-02 00:18:17 +02:00
parent 8c351ad176
commit 956e860ff7

View file

@ -1,21 +1,14 @@
#!/bin/bash
backup_dir="${1}/conf/ldap" backup_dir="${1}/conf/ldap"
if [[ $EUID -ne 0 ]]; then systemctl stop slapd
# We need to execute this script as root, since the ldap # Create a directory for backup
# service will be shut down during the operation (and sudo TMPDIR="/tmp/$(date +%s)"
# won't be available) mkdir -p "$TMPDIR"
/bin/bash $(readlink -f $0) $1
else die() {
service slapd stop || true
# Create a directory for backup
TMPDIR="/tmp/$(date +%s)"
mkdir -p "$TMPDIR"
die() {
state=$1 state=$1
error=$2 error=$2
@ -28,34 +21,34 @@ else
mv "${TMPDIR}/ldap" /var/lib/ldap) mv "${TMPDIR}/ldap" /var/lib/ldap)
chown -R openldap: /etc/ldap/slapd.d /var/lib/ldap chown -R openldap: /etc/ldap/slapd.d /var/lib/ldap
service slapd start systemctl start slapd
rm -rf "$TMPDIR" rm -rf "$TMPDIR"
# Print an error message and exit # Print an error message and exit
printf "%s" "$error" 1>&2 printf "%s" "$error" 1>&2
exit 1 exit 1
} }
# Restore the configuration # Restore the configuration
mv /etc/ldap/slapd.d "$TMPDIR" mv /etc/ldap/slapd.d "$TMPDIR"
mkdir -p /etc/ldap/slapd.d mkdir -p /etc/ldap/slapd.d
cp -a "${backup_dir}/ldap.conf" /etc/ldap/ldap.conf cp -a "${backup_dir}/ldap.conf" /etc/ldap/ldap.conf
cp -a "${backup_dir}/slapd.ldif" /etc/ldap/slapd.ldif cp -a "${backup_dir}/slapd.ldif" /etc/ldap/slapd.ldif
# Legacy thing but we need it to force the regen-conf in case of it exist # Legacy thing but we need it to force the regen-conf in case of it exist
cp -a "${backup_dir}/slapd.conf" /etc/ldap/slapd.conf [ ! -e "${backup_dir}/slapd.conf" ] \
slapadd -F /etc/ldap/slapd.d -b cn=config \ || cp -a "${backup_dir}/slapd.conf" /etc/ldap/slapd.conf
slapadd -F /etc/ldap/slapd.d -b cn=config \
-l "${backup_dir}/cn=config.master.ldif" \ -l "${backup_dir}/cn=config.master.ldif" \
|| die 1 "Unable to restore LDAP configuration" || die 1 "Unable to restore LDAP configuration"
chown -R openldap: /etc/ldap/slapd.d chown -R openldap: /etc/ldap/slapd.d
# Restore the database # Restore the database
mv /var/lib/ldap "$TMPDIR" mv /var/lib/ldap "$TMPDIR"
mkdir -p /var/lib/ldap mkdir -p /var/lib/ldap
slapadd -F /etc/ldap/slapd.d -b dc=yunohost,dc=org \ slapadd -F /etc/ldap/slapd.d -b dc=yunohost,dc=org \
-l "${backup_dir}/dc=yunohost-dc=org.ldif" \ -l "${backup_dir}/dc=yunohost-dc=org.ldif" \
|| die 2 "Unable to restore LDAP database" || die 2 "Unable to restore LDAP database"
chown -R openldap: /var/lib/ldap chown -R openldap: /var/lib/ldap
service slapd start service slapd start
rm -rf "$TMPDIR" rm -rf "$TMPDIR"
fi