Commits

Zhang Huangbin committed d166249

[NEW] Add shell script to backup PostgreSQL databases in cron by default.
Script is /var/vmail/backup/backup_pgsql.sh, backup directory is
/var/vmail/backup/pgsql/.

Comments (0)

Files changed (4)

iRedMail/ChangeLog

 iRedMail-0.8.2:
+    * Add shell script to backup PostgreSQL databases in cron by default.
+      Script is /var/vmail/backup/backup_pgsql.sh, backup directory is
+      /var/vmail/backup/pgsql/.
     * It now works on openSUSE-12.2 (code name Mantis), with all 3 backends
       available. Tested with openSUSE 12.2 RC1.
     * Drop support for Ubuntu-11.10 (oneiric).

iRedMail/conf/postgresql

 export PGSQL_SYS_USER='postgres'
 export PGSQL_SYS_GROUP='postgres'
 
-export PGSQL_ROOT_USER='postgres'
-
 export PGSQL_SYS_USER_HOME='/var/lib/postgresql'
 
 export PGSQL_VERSION='9.1'  # Required on Gentoo
 elif [ X"${DISTRO}" == X'FREEBSD' ]; then
     export PGSQL_SYS_USER='pgsql'
     export PGSQL_SYS_GROUP='pgsql'
-    export PGSQL_ROOT_USER='pgsql'
     export PGSQL_SYS_USER_HOME='/usr/local/pgsql'
 
     export PGSQL_CONF_DIR="${PGSQL_SYS_USER_HOME}/data"
 elif [ X"${DISTRO}" == X'OPENBSD' ]; then
     export PGSQL_SYS_USER='_postgresql'
     export PGSQL_SYS_GROUP='_postgresql'
-    export PGSQL_ROOT_USER='_postgresql'
     export PGSQL_SYS_USER_HOME='/var/postgresql'
 
     export PGSQL_CONF_DIR="${PGSQL_SYS_USER_HOME}/data"
     export PGSQL_DATA_DIR="/var/lib/postgresql/${PGSQL_VERSION}/main"
 fi
 
+export PGSQL_ROOT_USER="${PGSQL_SYS_USER}"
 export PGSQL_RC_SCRIPT="${DIR_RC_SCRIPTS}/${PGSQL_RC_SCRIPT_NAME}"
 
 # ~/.pgpass

iRedMail/functions/cleanup.sh

     fi
 
     # Backup PostgreSQL databases
-    #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
-    #   cat >> ${CRON_SPOOL_DIR}/root <<EOF
-## Backup on 03:30 AM
-#30   3   *   *   *   ${SHELL_BASH} ${BACKUP_SCRIPT_PGSQL}
-#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}
+
+        perl -pi -e 's#^(export BACKUP_ROOTDIR=).*#${1}"$ENV{BACKUP_DIR}"#' ${BACKUP_SCRIPT_PGSQL}
+        perl -pi -e 's#^(export DATABASES=).*#${1}"$ENV{PGSQL_BACKUP_DATABASES}"#' ${BACKUP_SCRIPT_PGSQL}
+
+        # Add cron job
+        cat >> ${CRON_SPOOL_DIR}/root <<EOF
+# Backup on 03:01 AM
+1   3   *   *   *   ${SHELL_BASH} ${BACKUP_SCRIPT_PGSQL}
+EOF
+    fi
 
     echo 'export status_cleanup_backup_scripts="DONE"' >> ${STATUS_FILE}
 }

iRedMail/tools/backup_pgsql.sh

+#!/usr/bin/env bash
+
+# Author:   Zhang Huangbin (zhb _at_ iredmail.org)
+# Date:     2012-09-06
+# Purpose:  Backup specified PostgreSQL databases with command 'pg_dump'.
+# License:  This shell script is part of iRedMail project, released under GPLv2.
+
+###########################
+# REQUIREMENTS
+###########################
+#
+#   * Required commands:
+#       + pg_dump
+#       + du
+#       + bzip2 or gzip     # 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 in variable $BACKUP_ROOTDIR below.
+#
+#   * Set correct values for below variables:
+#
+#       PGSQL_ADMIN
+#       BACKUP_ROOTDIR
+#       DATABASES
+#       COMPRESS
+#       DELETE_PLAIN_SQL_FILE
+#
+#   * Add crontab job for root user (or whatever user you want):
+#
+#       # crontab -e -u root
+#       1   4   *   *   *   bash /path/to/backup_pgsql.sh
+#
+#   * Make sure 'crond' service is running.
+#
+
+#########################################################
+# Modify below variables to fit your need ----
+#########################################################
+# Administrator user name. Usually, it's postgres.
+# FreeBSD: pgsql
+# OpenBSD: _postgresql
+export PGSQL_ROOT_USER='_postgresql'
+
+# Where to store backup copies.
+export BACKUP_ROOTDIR='/var/vmail/backup'
+
+# Databases we should backup.
+# Multiple databases MUST be seperated by SPACE.
+# Your iRedMail server might have below databases:
+# vmail, roundcubemail, cluebringer, amavisd, iredadmin.
+export DATABASES='vmail roundcubemail policyd amavisd iredadmin'
+
+# Compress plain SQL file: YES, NO.
+export COMPRESS="YES"
+
+# Delete plain SQL files after compressed. Compressed copy will be remained.
+export DELETE_PLAIN_SQL_FILE="YES"
+
+#########################################################
+# You do *NOT* need to modify below lines.
+#########################################################
+export PATH='/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin'
+
+# Commands.
+export CMD_DATE='/bin/date'
+export CMD_DU='du -sh'
+export CMD_COMPRESS='bzip2 -9'
+export CMD_PG_DUMP='pg_dump'
+
+# Date.
+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.
+export BACKUP_DIR="${BACKUP_ROOTDIR}/pgsql/${YEAR}/${MONTH}/${DAY}"
+
+# Log file
+export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"
+
+# Check and create directories.
+[ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} 2>/dev/null
+
+# Get HOME directory of PGSQL_ROOT_USER
+export PGSQL_ROOT_USER_HOME="$(su - ${PGSQL_ROOT_USER} -c 'echo $HOME')"
+
+# Initialize log file.
+echo "* Starting backup: ${TIMESTAMP}." >${LOGFILE}
+echo "* Backup directory: ${BACKUP_DIR}." >>${LOGFILE}
+
+# Backup.
+echo "* Backing up databases ..." >> ${LOGFILE}
+for db in ${DATABASES}; do
+    output_sql="${db}-${TIMESTAMP}.sql"
+
+    # Verify db existence
+    su - "${PGSQL_ROOT_USER}" -c "psql -d ${db} -c '\q' >/dev/null 2>&1"
+
+    # Dump
+    if [ X"$?" == X'0' ]; then
+        su - "${PGSQL_ROOT_USER}" -c "${CMD_PG_DUMP} ${db} > ${PGSQL_ROOT_USER_HOME}/${output_sql}"
+
+        # Move to backup directory.
+        mv ${PGSQL_ROOT_USER_HOME}/${output_sql} ${BACKUP_DIR}
+
+        # Compress
+        if [ X"${COMPRESS}" == X"YES" ]; then
+            cd ${BACKUP_DIR}
+            ${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
+
+        if [ X"$?" == X"0" ]; then
+            echo -e "  + ${db} [DONE]" >> ${LOGFILE}
+        else
+            [ X"${BACKUP_SUCCESS}" == X"YES" ] && export BACKUP_SUCCESS='NO'
+        fi
+    fi
+done
+
+# Append file size of backup files.
+echo -e "* File size:\n----" >>${LOGFILE}
+${CMD_DU} ${BACKUP_DIR}/*${TIMESTAMP}*sql* >>${LOGFILE}
+echo "----" >>${LOGFILE}
+
+echo "* Backup completed (Success? ${BACKUP_SUCCESS})." >>${LOGFILE}
+
+if [ X"${BACKUP_SUCCESS}" == X"YES" ]; then
+    echo -e "\n[OK] Backup successfully completed.\n"
+else
+    echo -e "\n[ERROR] Backup completed with ERRORS.\n" 1>&2
+fi
+
+echo "* Backup log: ${LOGFILE}:"
+cat ${LOGFILE}