2019-05-25 17:36:24 -03:00

41 lines
1.3 KiB
Django/Jinja

#!/bin/bash
# Ansible-generated LDAP backup script
#
# usage: ldap_backup.sh DN DIRECTORY KEEP
# where:
# DN is the base DN to backup
# DIRECTORY is where to save the backup
# KEEP number of backups to keep
# utility functions
msg(){ ${VERBOSE:-true} && echo ${@} ; }
assert(){ [[ $? -eq 0 ]] || { [[ -n ${1} ]] && msg ${@} ; exit 1 ; } }
# Base DN to backup
DN=${1}
# Directory where backups are saved
BACKUP_DIR="${2:-{{ openldap_backup_dir }}}"
# Number of backups to keep
KEEP=${3:-{{openldap_backup_keep}}}
# validate arguments
[[ -n "${DN}" ]] ; assert "ERROR: El primer argumento debe ser un DN."
[[ -n "${BACKUP_DIR}" ]] ; assert "ERROR: el segundo argumento (dir de backup) no puede estar vacío."
[[ "${KEEP}" -eq "${KEEP}" ]] 2> /dev/null ; assert "ERROR: el tercer argumento debe ser numérico."
# create backup dir
mkdir -p "${BACKUP_DIR}"
# check for commands
command -v /usr/sbin/slapcat > /dev/null
assert "ERROR: no se encuentra 'slapcat', por favor instale ldap-utils."
# perform backup
/usr/sbin/slapcat -H "ldap:///${DN}" | \
gzip -9 > "${BACKUP_DIR}/${DN}_$(date '+%F%H%M').ldif.gz"
assert "ERROR al hacer backup de ${DN}"
# remove old files
find "${BACKUP_DIR}" -maxdepth 1 -type f -name "cn=config_*.ldif.gz" -print0 \
| sort -z | tail -zn +${KEEP} | xargs -0 rm -f