# bash -xv backup_openldap.sh
#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb@iredmail.org)
# Date: Mar 15, 2012
# Purpose: Dump whole LDAP tree with command 'slapcat'.
# License: This shell script is part of iRedMail project, released under
# GPL v2.
###########################
# REQUIREMENTS
###########################
#
# * Required commands:
# + slapcat
# + du
# + bzip2 # If bzip2 is not available, change 'CMD_COMPRESS' to use 'gzip'.
#
###########################
# USAGE
###########################
#
# * It stores all backup copies in directory '/var/vmail/backup' by default,
# You can change it with variable $BACKUP_ROOTDIR below.
#
# * Set correct values for below variables:
#
# BACKUP_ROOTDIR
#
# * Add crontab job for root user (or whatever user you want):
#
# # crontab -e -u root
# 1 4 * * * bash /path/to/backup_openldap.sh
#
# * Make sure 'crond' service is running, and will start automatically when
# system startup:
#
# # ---- On RHEL/CentOS ----
# # chkconfig --level 345 crond on
# # /etc/init.d/crond status
#
# # ---- On Debian/Ubuntu ----
# # update-rc.d cron defaults
# # /etc/init.d/cron status
#
###############################
# How to restore backup file:
###############################
# Please refer to wiki tutorial for detail steps:
# http://www.iredmail.org/docs/backup.restore.html
#
#########################################################
# Modify below variables to fit your need ----
#########################################################
# Where to store backup copies.
export BACKUP_ROOTDIR="/var/vmail/backup"
+ export BACKUP_ROOTDIR=/var/vmail/backup
+ BACKUP_ROOTDIR=/var/vmail/backup
# Keep backup for how many days. Default is 90 days.
export KEEP_DAYS='90'
+ export KEEP_DAYS=90
+ KEEP_DAYS=90
#########################################################
# You do *NOT* need to modify below lines.
#########################################################
export PATH="$PATH:/usr/sbin:/usr/local/sbin/"
+ export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/cc/.local/bin:/home/cc/bin:/usr/sbin:/usr/local/sbin/
+ PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/cc/.local/bin:/home/cc/bin:/usr/sbin:/usr/local/sbin/
# Commands.
export CMD_DATE='/bin/date'
+ export CMD_DATE=/bin/date
+ CMD_DATE=/bin/date
export CMD_DU='du -sh'
+ export 'CMD_DU=du -sh'
+ CMD_DU='du -sh'
export CMD_COMPRESS='bzip2 -9'
+ export 'CMD_COMPRESS=bzip2 -9'
+ CMD_COMPRESS='bzip2 -9'
export COMPRESS_SUFFIX='bz2'
+ export COMPRESS_SUFFIX=bz2
+ COMPRESS_SUFFIX=bz2
export CMD_MYSQL='mysql'
+ export CMD_MYSQL=mysql
+ CMD_MYSQL=mysql
# MySQL user and password, used to log backup status to sql table `iredadmin.log`.
# You can find password of SQL user 'iredadmin' in iRedAdmin config file 'settings.py'.
#
# If MYSQL_PASSWD is empty, read password from /root/.my.cnf instead.
export MYSQL_USER="iredadmin"
+ export MYSQL_USER=iredadmin
+ MYSQL_USER=iredadmin
export MYSQL_PASSWD="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ export MYSQL_PASSWD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+ MYSQL_PASSWD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export MYSQL_DOT_MY_CNF='/root/.my.cnf'
+ export MYSQL_DOT_MY_CNF=/root/.my.cnf
+ MYSQL_DOT_MY_CNF=/root/.my.cnf
if [ -f /etc/ldap/slapd.conf ]; then
export CMD_SLAPCAT='slapcat -f /etc/ldap/slapd.conf'
elif [ -f /etc/openldap/slapd.conf ]; then
export CMD_SLAPCAT='slapcat -f /etc/openldap/slapd.conf'
elif [ -f /usr/local/etc/openldap/slapd.conf ]; then
export CMD_SLAPCAT='slapcat -f /usr/local/etc/openldap/slapd.conf'
else
export CMD_SLAPCAT='slapcat'
fi
+ '[' -f /etc/ldap/slapd.conf ']'
+ '[' -f /etc/openldap/slapd.conf ']'
+ export 'CMD_SLAPCAT=slapcat -f /etc/openldap/slapd.conf'
+ CMD_SLAPCAT='slapcat -f /etc/openldap/slapd.conf'
# Date.
export YEAR="$(${CMD_DATE} +%Y)"
++ /bin/date +%Y
+ export YEAR=2017
+ YEAR=2017
export MONTH="$(${CMD_DATE} +%m)"
++ /bin/date +%m
+ export MONTH=03
+ MONTH=03
export DAY="$(${CMD_DATE} +%d)"
++ /bin/date +%d
+ export DAY=20
+ DAY=20
export TIME="$(${CMD_DATE} +%H-%M-%S)"
++ /bin/date +%H-%M-%S
+ export TIME=09-45-42
+ TIME=09-45-42
export TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}"
+ export TIMESTAMP=2017-03-20-09-45-42
+ TIMESTAMP=2017-03-20-09-45-42
# Pre-defined backup status
export BACKUP_SUCCESS='NO'
+ export BACKUP_SUCCESS=NO
+ BACKUP_SUCCESS=NO
#########
# Define, check, create directories.
#
# Backup directory.
export BACKUP_DIR="${BACKUP_ROOTDIR}/ldap/${YEAR}/${MONTH}"
+ export BACKUP_DIR=/var/vmail/backup/ldap/2017/03
+ BACKUP_DIR=/var/vmail/backup/ldap/2017/03
export BACKUP_FILE="${BACKUP_DIR}/${TIMESTAMP}.ldif"
+ export BACKUP_FILE=/var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif
+ BACKUP_FILE=/var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif
# Find the old backup which should be removed.
export REMOVE_OLD_BACKUP='NO'
+ export REMOVE_OLD_BACKUP=NO
+ REMOVE_OLD_BACKUP=NO
if which python &>/dev/null; then
export REMOVE_OLD_BACKUP='YES'
py_cmd="import time; import datetime; t=time.localtime(); print datetime.date(t.tm_year, t.tm_mon, t.tm_mday) - datetime.timedelta(days=${KEEP_DAYS})"
shift_date=$(python -c "${py_cmd}")
shift_year="$(echo ${shift_date} | awk -F'-' '{print $1}')"
shift_month="$(echo ${shift_date} | awk -F'-' '{print $2}')"
shift_day="$(echo ${shift_date} | awk -F'-' '{print $3}')"
export REMOVED_BACKUP_DIR="${BACKUP_ROOTDIR}/ldap/${shift_year}/${shift_month}"
export REMOVED_BACKUPS="${BACKUP_ROOTDIR}/ldap/${shift_year}/${shift_month}/${shift_date}*"
fi
+ which python
+ export REMOVE_OLD_BACKUP=YES
+ REMOVE_OLD_BACKUP=YES
+ py_cmd='import time; import datetime; t=time.localtime(); print datetime.date(t.tm_year, t.tm_mon, t.tm_mday) - datetime.timedelta(days=90)'
++ python -c 'import time; import datetime; t=time.localtime(); print datetime.date(t.tm_year, t.tm_mon, t.tm_mday) - datetime.timedelta(days=90)'
+ shift_date=2016-12-20
++ echo 2016-12-20
++ awk -F- '{print $1}'
+ shift_year=2016
++ echo 2016-12-20
++ awk -F- '{print $2}'
+ shift_month=12
++ echo 2016-12-20
++ awk -F- '{print $3}'
+ shift_day=20
+ export REMOVED_BACKUP_DIR=/var/vmail/backup/ldap/2016/12
+ REMOVED_BACKUP_DIR=/var/vmail/backup/ldap/2016/12
+ export 'REMOVED_BACKUPS=/var/vmail/backup/ldap/2016/12/2016-12-20*'
+ REMOVED_BACKUPS='/var/vmail/backup/ldap/2016/12/2016-12-20*'
# Log file
export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"
+ export LOGFILE=/var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.log
+ LOGFILE=/var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.log
# Check and create directories.
[ -d ${BACKUP_DIR} ] || mkdir -p ${BACKUP_DIR}
+ '[' -d /var/vmail/backup/ldap/2017/03 ']'
chown root ${BACKUP_DIR}
+ chown root /var/vmail/backup/ldap/2017/03
chmod 0700 ${BACKUP_DIR}
+ chmod 0700 /var/vmail/backup/ldap/2017/03
# Initialize log file.
echo "* Starting backup at ${TIMESTAMP}" >> ${LOGFILE}
+ echo '* Starting backup at 2017-03-20-09-45-42'
echo "* Backup directory: ${BACKUP_DIR}." >> ${LOGFILE}
+ echo '* Backup directory: /var/vmail/backup/ldap/2017/03.'
# Backup
echo "* Dumping LDAP data into file: ${BACKUP_FILE}..." >> ${LOGFILE}
+ echo '* Dumping LDAP data into file: /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif...'
${CMD_SLAPCAT} > ${BACKUP_FILE}
+ slapcat -f /etc/openldap/slapd.conf
58cf3446 mdb_monitor_db_open: monitoring disabled; configure monitor database to enable
if [ X"$?" == X"0" ]; then
export BACKUP_SUCCESS='YES'
# Get original backup file size
original_size="$(${CMD_DU} ${BACKUP_FILE} | awk '{print $1}')"
# Compress backup file.
echo "* Compressing LDIF file with command: '${CMD_COMPRESS}' ..." >> ${LOGFILE}
${CMD_COMPRESS} ${BACKUP_FILE} >> ${LOGFILE} 2>&1
echo "* [DONE]" >>${LOGFILE}
# Get compressed file size
compressed_file_name="${BACKUP_FILE}.${COMPRESS_SUFFIX}"
compressed_size="$(${CMD_DU} ${compressed_file_name} | awk '{print $1}')"
echo -n "* Removing plain LDIF file: ${BACKUP_FILE}..." >>${LOGFILE}
rm -f ${BACKUP_FILE} >> ${LOGFILE} 2>&1
[ X"$?" == X"0" ] && echo -e "\t[DONE]" >>${LOGFILE}
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Backup LDAP data, size: ${original_size}, compressed: ${compressed_size}', 'cron_backup_ldap', '127.0.0.1', UTC_TIMESTAMP());"
else
# Log failure
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Backup LDAP data failed, check log file ${LOGFILE} for more details.', 'cron_backup_ldap', '127.0.0.1', UTC_TIMESTAMP());"
fi
+ '[' X0 == X0 ']'
+ export BACKUP_SUCCESS=YES
+ BACKUP_SUCCESS=YES
++ du -sh /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif
++ awk '{print $1}'
+ original_size=16K
+ echo '* Compressing LDIF file with command: '\''bzip2 -9'\'' ...'
+ bzip2 -9 /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif
+ echo '* [DONE]'
+ compressed_file_name=/var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif.bz2
++ du -sh /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif.bz2
++ awk '{print $1}'
+ compressed_size=4.0K
+ echo -n '* Removing plain LDIF file: /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif...'
+ rm -f /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif
+ '[' X0 == X0 ']'
+ echo -e '\t[DONE]'
+ sql_log_msg='INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('\''backup'\'', '\''info'\'', '\''Backup LDAP data, size: 16K, compressed: 4.0K'\'', '\''cron_backup_ldap'\'', '\''127.0.0.1'\'', UTC_TIMESTAMP());'
# Log to SQL table `iredadmin.log`, so that global domain admins can
# check backup status (System -> Admin Log)
if [[ -n ${MYSQL_USER} ]]; then
if [[ -n ${MYSQL_PASSWD} ]]; then
export CMD_MYSQL_ROOT="${CMD_MYSQL} -u'${MYSQL_USER}' -p'${MYSQL_PASSWD}'"
else
export CMD_MYSQL_ROOT="${CMD_MYSQL} --defaults-file=${MYSQL_DOT_MY_CNF} -u'${MYSQL_USER}'"
fi
fi
+ [[ -n iredadmin ]]
+ [[ -n xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ]]
+ export 'CMD_MYSQL_ROOT=mysql -u'\''iredadmin'\'' -p'\''xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'\'''
+ CMD_MYSQL_ROOT='mysql -u'\''iredadmin'\'' -p'\''xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'\'''
${CMD_MYSQL_ROOT} iredadmin -e "${sql_log_msg}" >>${LOGFILE} 2>&1
+ mysql '-u'\''iredadmin'\''' '-p'\''xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'\''' iredadmin -e 'INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('\''backup'\'', '\''info'\'', '\''Backup LDAP data, size: 16K, compressed: 4.0K'\'', '\''cron_backup_ldap'\'', '\''127.0.0.1'\'', UTC_TIMESTAMP());'
# Append file size of backup files to log file.
echo "* File size:" >>${LOGFILE}
+ echo '* File size:'
echo "=================" >>${LOGFILE}
+ echo =================
${CMD_DU} ${BACKUP_FILE}* >>${LOGFILE}
+ du -sh /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif.bz2
echo "=================" >>${LOGFILE}
+ echo =================
# Print some message. It will cause cron generates an email to root user.
if [ X"${BACKUP_SUCCESS}" == X'YES' ]; then
echo "* [ OK ] Backup completes successfully." >> ${LOGFILE}
else
echo "* <<< ERROR >>> Backup not successfully complete." >> ${LOGFILE}
fi
+ '[' XYES == XYES ']'
+ echo '* [ OK ] Backup completes successfully.'
if [ X"${REMOVE_OLD_BACKUP}" == X'YES' -a -d ${REMOVED_BACKUP_DIR} ]; then
echo -e "* Delete old backup under ${REMOVED_BACKUP_DIR}." >> ${LOGFILE}
echo -e "* Suppose to delete: ${REMOVED_BACKUPS}" >> ${LOGFILE}
rm -rf ${REMOVED_BACKUPS} >> ${LOGFILE} 2>&1
if [ -n ${MYSQL_USER} ] && [ -n ${MYSQL_PASSWD} ]; then
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Remove old backup: ${REMOVED_BACKUPS}.', 'cron_backup_sql', '127.0.0.1', UTC_TIMESTAMP());"
${CMD_MYSQL_ROOT} iredadmin -e "${sql_log_msg}"
fi
fi
+ '[' XYES == XYES -a -d /var/vmail/backup/ldap/2016/12 ']'
cat ${LOGFILE}
+ cat /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.log
* Starting backup at 2017-03-20-09-45-42
* Backup directory: /var/vmail/backup/ldap/2017/03.
* Dumping LDAP data into file: /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif...
* Compressing LDIF file with command: 'bzip2 -9' ...
* [DONE]
* Removing plain LDIF file: /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif... [DONE]
ERROR 1045 (28000): Access denied for user 'iredadmin'@'localhost' (using password: YES)
* File size:
=================
4.0K /var/vmail/backup/ldap/2017/03/2017-03-20-09-45-42.ldif.bz2
=================
* [ OK ] Backup completes successfully.