Commits

Zhang Huangbin committed 78b65df

Add shell scripts to backup OpenLDAP and/or MySQL in cron by default.

  • Participants
  • Parent commits f732e1b

Comments (0)

Files changed (5)

File iRedMail/ChangeLog

 iRedMail-0.8.0-beta3:
+    * Add shell scripts to backup OpenLDAP and/or MySQL in cron by default.
     * Use Dovecot-2.0 instead of 1.2.x on openSUSE-12.1.
     * Drop support for below distributions and releases:
         + Debian 5 (Lenny)

File iRedMail/dialog/config_via_dialog.sh

     * It may take large disk space.
 " 20 76 "${VMAIL_USER_HOME_DIR}" 2>/tmp/vmail_user_home_dir
 
-VMAIL_USER_HOME_DIR="$(cat /tmp/vmail_user_home_dir)"
-export VMAIL_USER_HOME_DIR="${VMAIL_USER_HOME_DIR}"
+export VMAIL_USER_HOME_DIR="$(cat /tmp/vmail_user_home_dir)"
+rm -f /tmp/vmail_user_home_dir
+
 export STORAGE_BASE_DIR="${VMAIL_USER_HOME_DIR}"
 export SIEVE_DIR="${VMAIL_USER_HOME_DIR}/sieve"
 echo "export VMAIL_USER_HOME_DIR='${VMAIL_USER_HOME_DIR}'" >> ${CONFIG_FILE}
 echo "export STORAGE_BASE_DIR='${VMAIL_USER_HOME_DIR}'" >> ${CONFIG_FILE}
 echo "export SIEVE_DIR='${SIEVE_DIR}'" >>${CONFIG_FILE}
-rm -f /tmp/vmail_user_home_dir
+
+export BACKUP_DIR="${VMAIL_USER_HOME_DIR}/backup"
+export BACKUP_SCRIPT_OPENLDAP="${BACKUP_DIR}/backup_openldap.sh"
+export BACKUP_SCRIPT_MYSQL="${BACKUP_DIR}/backup_mysql.sh"
+export BACKUP_SCRIPT_PGSQL="${BACKUP_DIR}/backup_pgsql.sh"
+echo "export BACKUP_DIR='${BACKUP_DIR}'" >>${CONFIG_FILE}
+echo "export BACKUP_SCRIPT_OPENLDAP='${BACKUP_SCRIPT_OPENLDAP}'" >>${CONFIG_FILE}
+echo "export BACKUP_SCRIPT_MYSQL='${BACKUP_SCRIPT_MYSQL}'" >>${CONFIG_FILE}
+echo "export BACKUP_SCRIPT_PGSQL='${BACKUP_SCRIPT_PGSQL}'" >>${CONFIG_FILE}
 
 # --------------------------------------------------
 # --------------------- Backend --------------------

File iRedMail/functions/cleanup.sh

 
 cleanup_backup_scripts()
 {
-    ECHO_DEBUG "Updating backup script: ${TOOLS_DIR}/backup_mysql.sh."
-    perl -pi -e 's#^(MYSQL_USER=).*#${1}"ENV{MYSQL_ROOT_USER}"#' ${TOOLS_DIR}/backup_mysql.sh
-    perl -pi -e 's#^(MYSQL_PASSWD=).*#${1}"ENV{MYSQL_ROOT_PASSWD}"#' ${TOOLS_DIR}/backup_mysql.sh
-    perl -pi -e 's#^(DATABASES=).*#${1}"ENV{MYSQL_BACKUP_DATABASES}"#' ${TOOLS_DIR}/backup_mysql.sh
+    [ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} &>/dev/null
+
+    if [ X"${BACKEND}" == X'OPENLDAP' ]; then
+        ECHO_DEBUG "Setup backup script: ${BACKUP_SCRIPT_OPENLDAP}"
+
+        backup_file ${BACKUP_SCRIPT_OPENLDAP}
+        cp ${TOOLS_DIR}/backup_openldap.sh ${BACKUP_SCRIPT_OPENLDAP}
+        chown ${SYS_ROOT_USER}:${SYS_ROOT_GROUP} ${BACKUP_SCRIPT_OPENLDAP}
+        chmod 0700 ${BACKUP_SCRIPT_OPENLDAP}
+
+        perl -pi -e 's#^(BACKUP_ROOTDIR=).*#${1}"$ENV{BACKUP_DIR}"#' ${BACKUP_SCRIPT_OPENLDAP}
+
+        # Add cron job
+        cat >> ${CRON_SPOOL_DIR}/root <<EOF
+# Backup on 03:00 AM
+0   3   *   *   *   bash ${BACKUP_SCRIPT_OPENLDAP}
+EOF
+
+        cat >> ${TIP_FILE} <<EOF
+Backup OpenLDAP data:
+    * Script: ${BACKUP_SCRIPT_OPENLDAP}
+    * See also:
+        # crontab -l -u ${SYS_ROOT_USER}
+
+EOF
+    fi
+
+    if [ X"${BACKEND}" == X'OPENLDAP' -o X"${BACKEND}" == X'MYSQL' ]; then
+        ECHO_DEBUG "Setup backup script: ${BACKUP_SCRIPT_MYSQL}"
+
+        backup_file ${BACKUP_SCRIPT_MYSQL}
+        cp ${TOOLS_DIR}/backup_mysql.sh ${BACKUP_SCRIPT_MYSQL}
+        chown ${SYS_ROOT_USER}:${SYS_ROOT_GROUP} ${BACKUP_SCRIPT_MYSQL}
+        chmod 0700 ${BACKUP_SCRIPT_MYSQL}
+
+        perl -pi -e 's#^(BACKUP_ROOTDIR=).*#${1}"$ENV{BACKUP_DIR}"#' ${BACKUP_SCRIPT_MYSQL}
+        perl -pi -e 's#^(MYSQL_USER=).*#${1}"$ENV{MYSQL_ROOT_USER}"#' ${BACKUP_SCRIPT_MYSQL}
+        perl -pi -e 's#^(MYSQL_PASSWD=).*#${1}"$ENV{MYSQL_ROOT_PASSWD}"#' ${BACKUP_SCRIPT_MYSQL}
+        perl -pi -e 's#^(DATABASES=).*#${1}"$ENV{MYSQL_BACKUP_DATABASES}"#' ${BACKUP_SCRIPT_MYSQL}
+
+        # Add cron job
+        cat >> ${CRON_SPOOL_DIR}/root <<EOF
+# Backup on 03:30 AM
+30   3   *   *   *   bash ${BACKUP_SCRIPT_MYSQL}
+EOF
+
+        cat >> ${TIP_FILE} <<EOF
+Backup MySQL database:
+    * Script: ${BACKUP_SCRIPT_MYSQL}
+    * See also:
+        # crontab -l -u ${SYS_ROOT_USER}
+EOF
+    fi
+
+    #if [ X"${BACKEND}" == X'PGSQL' ]; then
+    #   ECHO_DEBUG "Setup backup script: ${BACKUP_SCRIPT_PGSQL}"
+    #
+    #   backup_file ${BACKUP_SCRIPT_PGSQL}
+    #   cp ${TOOLS_DIR}/backup_pgsql.sh ${BACKUP_SCRIPT_PGSQL}
+    #   chown ${SYS_ROOT_USER}:${SYS_ROOT_GROUP} ${BACKUP_SCRIPT_PGSQL}
+    #   chmod 0700 ${BACKUP_SCRIPT_PGSQL}
+    #
+    #   TODO
+    #   Add cron job
+    #fi
 
     echo 'export status_cleanup_backup_scripts="DONE"' >> ${STATUS_FILE}
 }

File iRedMail/tools/backup_mysql.sh

 # USAGE
 ###########################
 #
-#   * It stores all backup copies in directory '/backup' by default, you can
-#     change it in variable $BACKUP_ROOTDIR below.
+#   * It stores all backup copies in directory '/var/vmail/backup' by default,
+#     You can change it in variable $BACKUP_ROOTDIR below.
 #
 #   * Set correct values for below variables:
 #
 #       # /etc/init.d/cron status
 #
 
-###########################
-# DIRECTORY STRUCTURE
-###########################
-#
-#   $BACKUP_ROOTDIR             # Default is /backup
-#       |- mysql/               # Used to store all backed up databases.
-#           |- YEAR.MONTH/
-#               |- YEAR.MONTH.DAY/
-#                   |- DB.YEAR.MONTH.DAY.MIN.HOUR.SECOND.sql
-#                               # Backup copy, plain SQL file.
-#                               # Note: it will be removed immediately after
-#                               # it was compressed with success and 
-#                               # DELETE_PLAIN_SQL_FILE='YES'
-#
-#                   |- DB.YEAR.MONTH.DAY.HOUR.MINUTE.SECOND.sql.bz2
-#                               # Backup copy, compressed SQL file.
-#
-#       |- logs/
-#           |- YEAR.MONTH/
-#               |- mysql-YEAR.MONTH.DAY.MIN.HOUR.SECOND.log     # Log file
-#
-
 #########################################################
 # Modify below variables to fit your need ----
 #########################################################
 # Where to store backup copies.
-BACKUP_ROOTDIR='/backup'
+BACKUP_ROOTDIR='/var/vmail/backup'
 
 # MySQL user and password.
 MYSQL_USER='root'
 MYSQL_PASSWD='passwd'
 
-# Which database(s) we should backup. Multiple databases MUST be seperated by
-# a SPACE.
+# Databases we should backup.
+# Multiple databases MUST be seperated by SPACE.
 # Your iRedMail server might have below databases:
 # mysql, roundcubemail, policyd (or postfixpolicyd), amavisd, iredadmin
-DATABASES='mysql roundcubemail postfixpolicyd amavisd iredadmin'
+DATABASES='mysql roundcubemail policyd amavisd iredadmin'
 
 # Database character set for ALL databases.
 # Note: Currently, it doesn't support to specify character set for each databases.
 CMD_MYSQL='mysql'
 
 # Date.
-MONTH="$(${CMD_DATE} +%Y.%m)"
-DAY="$(${CMD_DATE} +%d)"
-DATE="$(${CMD_DATE} +%Y.%m.%d.%H.%M.%S)"
+export YEAR="$(${CMD_DATE} +%Y)"
+export MONTH="$(${CMD_DATE} +%m)"
+export DAY="$(${CMD_DATE} +%d)"
+export TIME="$(${CMD_DATE} +%H.%M.%S)"
+export TIMESTAMP="${YEAR}.${MONTH}.${DAY}.${TIME}"
 
+# Pre-defined backup status
 export BACKUP_SUCCESS='YES'
 
 # Define, check, create directories.
-BACKUP_DIR="${BACKUP_ROOTDIR}/mysql/${MONTH}/${DAY}"
+export BACKUP_DIR="${BACKUP_ROOTDIR}/mysql/${YEAR}/${MONTH}/${DAY}"
 
-# Logfile directory. Default is /backup/logs/YYYY.MM/.
-LOG_DIR="${BACKUP_ROOTDIR}/logs/${MONTH}"
-LOGFILE="${LOG_DIR}/mysql-${DATE}.log"
+# Log file
+export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"
 
 # Check required variables.
 if [ X"${MYSQL_USER}" == X"" -o X"${MYSQL_PASSWD}" == X"" -o X"${DATABASES}" == X"" ]; then
 fi
 
 # Verify MySQL connection.
-${CMD_MYSQL} -u"${MYSQL_USER}" -p"${MYSQL_PASSWD}" -e "show databases" >/dev/null 2>&1
+${CMD_MYSQL} -u"${MYSQL_USER}" -p"${MYSQL_PASSWD}" -e "show databases" &>/dev/null
 if [ X"$?" != X"0" ]; then
     echo "[ERROR] MySQL username or password is incorrect in file ${0}." 1>&2
     echo "Please fix them first." 1>&2
 fi
 
 # Check and create directories.
-if [ ! -d ${BACKUP_DIR} ]; then
-    echo "* Create data directory: ${BACKUP_DIR} ..."
-    mkdir -p ${BACKUP_DIR} 2>/dev/null
-fi
-
-if [ ! -d ${LOG_DIR} ]; then
-    echo "* Create log directory: ${LOG_DIR} ..."
-    mkdir -p ${LOG_DIR} 2>/dev/null
-fi
+[ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} 2>/dev/null
 
 # Initialize log file.
-echo "* Starting backup: ${DATE}." >${LOGFILE}
+echo "* Starting backup: ${TIMESTAMP}." >${LOGFILE}
 echo "* Backup directory: ${BACKUP_DIR}." >>${LOGFILE}
-echo "* Log file: ${LOGFILE}." >>${LOGFILE}
 
 backup_db()
 {
     # USAGE:
     #  # backup dbname
     db="${1}"
-    output_sql="${BACKUP_DIR}/${DATE}-${db}.sql"
+    output_sql="${BACKUP_DIR}/${db}-${TIMESTAMP}.sql"
 
-    ${CMD_MYSQLDUMP} \
-        -u"${MYSQL_USER}" \
-        -p"${MYSQL_PASSWD}" \
-        --default-character-set=${DB_CHARACTER_SET} \
-        ${db} > ${output_sql}
+    ${CMD_MYSQL} -u"${MYSQL_USER}" -p"${MYSQL_PASSWD}" -e "use ${db}" &>/dev/null
+
+    if [ X"$?" == X'0' ]; then
+        # Dump
+        ${CMD_MYSQLDUMP} \
+            -u"${MYSQL_USER}" \
+            -p"${MYSQL_PASSWD}" \
+            --default-character-set=${DB_CHARACTER_SET} \
+            ${db} > ${output_sql}
+
+        # Compress
+        if [ X"${COMPRESS}" == X"YES" ]; then
+            ${CMD_COMPRESS} ${output_sql} &>>${LOGFILE}
+
+            if [ X"$?" == X'0' -a X"${DELETE_PLAIN_SQL_FILE}" == X'YES' ]; then
+                rm -f ${output_sql} &>> ${LOGFILE}
+            fi
+        fi
+    fi
+
 }
 
 # Backup.
 echo "* Backing up databases ..." >> ${LOGFILE}
 for db in ${DATABASES}; do
-    backup_db ${db} >>${LOGFILE} 2>&1
+    backup_db ${db} &>>${LOGFILE}
 
     if [ X"$?" == X"0" ]; then
-        echo "  - [DONE] ${db}" >> ${LOGFILE}
+        echo "  - ${db} [DONE]" >> ${LOGFILE}
     else
         [ X"${BACKUP_SUCCESS}" == X"YES" ] && export BACKUP_SUCCESS='NO'
     fi
 done
 
-# Compress plain SQL file.
-if [ X"${COMPRESS}" == X"YES" ]; then
-    echo "* Compressing plain SQL files ..." >>${LOGFILE}
-    for sql_file in $(ls ${BACKUP_DIR}/*${DATE}*); do
-        ${CMD_COMPRESS} ${sql_file} >>${LOGFILE} 2>&1
+# Append file size of backup files.
+echo -e "* File size:\n----" >>${LOGFILE}
+${CMD_DU} ${BACKUP_DIR}/*${TIMESTAMP}*sql* >>${LOGFILE}
+echo "----" >>${LOGFILE}
 
-        if [ X"$?" == X"0" ]; then
-            echo "  - [DONE] $(basename ${sql_file})" >>${LOGFILE}
+echo "* Backup completed (Success? ${BACKUP_SUCCESS})." >>${LOGFILE}
 
-            # Delete plain SQL file after compressed.
-            if [ X"${DELETE_PLAIN_SQL_FILE}" == X"YES" -a -f ${sql_file} ]; then
-                echo -n "* Removing plain SQL file: ${sql_file}..." >>${LOGFILE}
-                rm -f ${BACKUP_DIR}/*${DATE}*sql >>${LOGFILE} 2>&1
-            fi
-        fi
-    done
+if [ X"${BACKUP_SUCCESS}" == X"YES" ]; then
+    echo "==> Backup completed successfully."
+else
+    echo -e "==> Backup completed with !!!ERRORS!!!.\n" 1>&2
 fi
 
-# Append file size of backup files.
-echo "* File size:" >>${LOGFILE}
-echo "=================" >>${LOGFILE}
-${CMD_DU} ${BACKUP_DIR}/*${DATE}* >>${LOGFILE}
-echo "=================" >>${LOGFILE}
-
-echo "* Backup complete (Successfully: ${BACKUP_SUCCESS})." >>${LOGFILE}
-
-if [ X"${BACKUP_SUCCESS}" == X"YES" ]; then
-    cat <<EOF
-* Backup completed successfully.
-EOF
-else
-    echo -e "\n* Backup completed with !!!ERRORS!!!.\n" 1>&2
-fi
-
-cat << EOF
-    + Data: ${BACKUP_FILE}*
-    + Log: ${LOGFILE}
-EOF
-
+echo "==> Detailed log (${LOGFILE}):"
+echo "========================="
+cat ${LOGFILE}

File iRedMail/tools/backup_openldap.sh

 #!/usr/bin/env bash
 
 # Author:   Zhang Huangbin (zhb@iredmail.org)
-# Date:     05/02/2010
+# 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.
 # USAGE
 ###########################
 #
-#   * It stores all backup copies in directory '/backup' by default, you can
-#     change it in variable $BACKUP_ROOTDIR below.
+#   * 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:
 #
 # http://www.iredmail.org/wiki/index.php?title=IRedMail/FAQ/Backup#How_to_restore_from_LDIF_file
 #
 
-###########################
-# DIRECTORY STRUCTURE
-###########################
-#
-#   $BACKUP_ROOTDIR             # Default is /backup
-#       |- ldap/                # Used to store all backed up copies.
-#           |- YEAR.MONTH/
-#               |- DAY/
-#                   |- YEAR.MONTH.DAY.MIN.HOUR.SECOND.ldif
-#                               # Backup copy, plain LDIF file.
-#                               # Note: it will be removed immediately after
-#                               # it was compressed with success and 
-#                               # DELETE_PLAIN_SQL_FILE='YES'
-#
-#                   |- YEAR.MONTH.DAY.HOUR.MINUTE.SECOND.ldif.bz2
-#                               # Backup copy, compressed LDIF file.
-#
-#       |- logs/
-#           |- YEAR.MONTH/
-#               |- ldap-YEAR.MONTH.DAY.MIN.HOUR.SECOND.log     # Log file
-#
-
 #########################################################
 # Modify below variables to fit your need ----
 #########################################################
 
 # Where to store backup copies.
-BACKUP_ROOTDIR='/backup'
+BACKUP_ROOTDIR='/var/vmail/backup'
 
 # Compress plain SQL file: YES, NO.
 COMPRESS="YES"
 fi
 
 # Date.
-export MONTH="$(${CMD_DATE} +%Y.%m)"
+export YEAR="$(${CMD_DATE} +%Y)"
+export MONTH="$(${CMD_DATE} +%m)"
 export DAY="$(${CMD_DATE} +%d)"
-export DATE="$(${CMD_DATE} +%Y.%m.%d.%H.%M.%S)"
+export TIME="$(${CMD_DATE} +%H.%M.%S)"
+export TIMESTAMP="${YEAR}.${MONTH}.${DAY}.${TIME}"
 
+# Pre-defined backup status
 export BACKUP_SUCCESS='NO'
 
 #########
 # Define, check, create directories.
 #
 # Backup directory.
-export BACKUP_DIR="${BACKUP_ROOTDIR}/ldap/${MONTH}/${DAY}"
-export BACKUP_FILE="${BACKUP_DIR}/${DATE}.ldif"
+export BACKUP_DIR="${BACKUP_ROOTDIR}/ldap/${YEAR}/${MONTH}"
+export BACKUP_FILE="${BACKUP_DIR}/${TIMESTAMP}.ldif"
 
-# Logfile directory. Default is /backup/logs/YYYY.MM/.
-export LOG_DIR="${BACKUP_ROOTDIR}/logs/${MONTH}"
+# Log file
+export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"
 
 # Check and create directories.
 if [ ! -d ${BACKUP_DIR} ]; then
     mkdir -p ${BACKUP_DIR}
 fi
 
-if [ ! -d ${LOG_DIR} ]; then
-    echo "* Create log directory: ${LOG_DIR}."
-    mkdir -p ${LOG_DIR} 2>/dev/null
-fi
-
-# Log file. Default is /backup/logs/YYYY.MM/mysql-YYYY.MM.DD.log.
-LOGFILE="${LOG_DIR}/ldap-${DATE}.log"
-
 ############
 # Initialize log file.
 #
-echo "* Starting backup: ${DATE}." >${LOGFILE}
+echo "* Starting backup at ${TIMESTAMP}" >${LOGFILE}
 echo "* Backup directory: ${BACKUP_DIR}." >>${LOGFILE}
-echo "* Log file: ${LOGFILE}." >>${LOGFILE}
 
 ##############
 # Backing up
 fi
 
 
-# Append file size of backup files.
+# Append file size of backup files to log file.
 echo "* File size:" >>${LOGFILE}
 echo "=================" >>${LOGFILE}
 ${CMD_DU} ${BACKUP_FILE}* >>${LOGFILE}
 echo "=================" >>${LOGFILE}
 
-echo "* Backup completed (Successfully: ${BACKUP_SUCCESS})." >>${LOGFILE}
+echo "* Backup completed (Success? ${BACKUP_SUCCESS})." >>${LOGFILE}
 
+# Print some message. It will cause cron generates an email to root user.
 if [ X"${BACKUP_SUCCESS}" == X"YES" ]; then
-    cat <<EOF
-* Backup completed successfully.
-EOF
+    echo "==> Backup completed successfully."
 else
-    echo -e "\n* Backup completed with !!!ERRORS!!!.\n" 1>&2
+    echo -e "==> Backup completed with !!!ERRORS!!!.\n" 1>&2
 fi
 
-cat << EOF
-    + Data: ${BACKUP_FILE}*
-    + Log: ${LOGFILE}
-EOF
-
+echo "==> Detailed log (${LOGFILE}):"
+echo "========================="
+cat ${LOGFILE}