#!/bin/bash ################################################################################# # # $Id$ # ################################################################################# CONFIG="$0.cfg" CUR_DIR="`pwd`" OS="`uname`" DARWIN="Darwin" LINUX="Linux" FREEBSD="FreeBSD" PG_DUMPALL="pg_dumpall" PG_VACUUM="vacuumdb" BACKUP="YES" VACUUM="NO" VAC_PARAM="" SILENCE="NO" ################################################################################# HELP () { echo "================================================================================" echo "Usage:" echo "$0 [ BACKUP_PARAM ] [ VACUUM_PARAM ] [ OUTPUT_PARAM ] [ CONFIG_FILE ]" echo "--------------------------------------------------------------------------------" echo " BACKUP_PARAM = --backup | --nobackup" echo " --backup ...... : regular backup for each DB (default)" echo " --nobackup .... : no backup" echo "--------------------------------------------------------------------------------" echo " VACUUM_PARAM = --novacuum | --vacuum | --vacuumfull" echo " --novacuum .... : no vacuum (default)" echo " --vacuum ...... : performs 'vacuum --analyze' for each DB" echo " --vacuumfull .. : performs 'vacuum --analyze --full' for each DB" echo "--------------------------------------------------------------------------------" echo " OUTPUT_PARAM = --verbose | --quick | --silence" echo " --verbose full script output (default)" echo " --quick quick script output" echo " --silence reduces the script output" echo "--------------------------------------------------------------------------------" echo " CONFIG_FILE = configuration file" echo " .cfg in same directory (default)" echo "================================================================================" exit 1 } ERROR () { echo " Error: $1" exit 1 } PRINT () { if [ "x${SILENCE}" = "xNO" ]; then if [ "x$2" != "x" ]; then if [ "x$2" = "x-n" ]; then echo -n "$1" else echo "$1 $2" fi else echo "$1" fi fi; } QUICK () { if [ "x${SILENCE}" = "xQUICK" ]; then if [ "x$2" != "x" ]; then if [ "x$2" = "x-n" ]; then echo -n "$1" else echo "$1 $2" fi else echo "$1" fi fi; } WARNING () { if [ "x${SILENCE}" != "xYES" ]; then if [ "x$2" != "x" ]; then if [ "x$2" = "x-n" ]; then echo -n " - warning: $1" else echo " - warning: $1 $2" fi else echo " - warning: $1" fi fi } ################################################################################# [ "x$1" = "x--help" ] && HELP [ "x$1" != "x" ] && [[ "x$1" != x--* ]] && CONFIG="$1" [ "x$2" != "x" ] && [[ "x$2" != x--* ]] && CONFIG="$2" [ "x$3" != "x" ] && [[ "x$3" != x--* ]] && CONFIG="$3" [ "x$4" != "x" ] && [[ "x$4" != x--* ]] && CONFIG="$4" if [ ! -f ${CONFIG} ]; then WARNING "config file ${CONFIG} not found" CONFIG="/dev/null" fi [ "x$1" != "x" ] && [ "x$1" = "x--silence" ] && SILENCE="YES" [ "x$2" != "x" ] && [ "x$2" = "x--silence" ] && SILENCE="YES" [ "x$3" != "x" ] && [ "x$3" = "x--silence" ] && SILENCE="YES" [ "x$4" != "x" ] && [ "x$4" = "x--silence" ] && SILENCE="YES" [ "x$1" != "x" ] && [ "x$1" = "x--quick" ] && SILENCE="QUICK" [ "x$2" != "x" ] && [ "x$2" = "x--quick" ] && SILENCE="QUICK" [ "x$3" != "x" ] && [ "x$3" = "x--quick" ] && SILENCE="QUICK" [ "x$4" != "x" ] && [ "x$4" = "x--quick" ] && SILENCE="QUICK" [ "x$1" != "x" ] && [ "x$1" = "x--verbose" ] && SILENCE="NO" [ "x$2" != "x" ] && [ "x$2" = "x--verbose" ] && SILENCE="NO" [ "x$3" != "x" ] && [ "x$3" = "x--verbose" ] && SILENCE="NO" [ "x$4" != "x" ] && [ "x$4" = "x--verbose" ] && SILENCE="NO" [ "x$1" != "x" ] && [ "x$1" = "x--backup" ] && BACKUP="YES" [ "x$2" != "x" ] && [ "x$2" = "x--backup" ] && BACKUP="YES" [ "x$3" != "x" ] && [ "x$3" = "x--backup" ] && BACKUP="YES" [ "x$4" != "x" ] && [ "x$4" = "x--backup" ] && BACKUP="YES" [ "x$1" != "x" ] && [ "x$1" = "x--nobackup" ] && BACKUP="NO" [ "x$2" != "x" ] && [ "x$2" = "x--nobackup" ] && BACKUP="NO" [ "x$3" != "x" ] && [ "x$3" = "x--nobackup" ] && BACKUP="NO" [ "x$4" != "x" ] && [ "x$4" = "x--nobackup" ] && BACKUP="NO" [ "x$1" != "x" ] && [ "x$1" = "x--novacuum" ] && VACUUM="NO" && VAC_PARAM="" [ "x$2" != "x" ] && [ "x$2" = "x--novacuum" ] && VACUUM="NO" && VAC_PARAM="" [ "x$3" != "x" ] && [ "x$3" = "x--novacuum" ] && VACUUM="NO" && VAC_PARAM="" [ "x$4" != "x" ] && [ "x$4" = "x--novacuum" ] && VACUUM="NO" && VAC_PARAM="" [ "x$1" != "x" ] && [ "x$1" = "x--vacuum" ] && VACUUM="YES" && VAC_PARAM="--analyze" [ "x$2" != "x" ] && [ "x$2" = "x--vacuum" ] && VACUUM="YES" && VAC_PARAM="--analyze" [ "x$3" != "x" ] && [ "x$3" = "x--vacuum" ] && VACUUM="YES" && VAC_PARAM="--analyze" [ "x$4" != "x" ] && [ "x$4" = "x--vacuum" ] && VACUUM="YES" && VAC_PARAM="--analyze" [ "x$1" != "x" ] && [ "x$1" = "x--vacuumfull" ] && VACUUM="YES" && VAC_PARAM="--analyze --full" [ "x$2" != "x" ] && [ "x$2" = "x--vacuumfull" ] && VACUUM="YES" && VAC_PARAM="--analyze --full" [ "x$3" != "x" ] && [ "x$3" = "x--vacuumfull" ] && VACUUM="YES" && VAC_PARAM="--analyze --full" [ "x$4" != "x" ] && [ "x$4" = "x--vacuumfull" ] && VACUUM="YES" && VAC_PARAM="--analyze --full" PRINT " = `date "+%d-%b-%y %H:%M"` - PRINT" QUICK " = `date "+%d-%b-%y %H:%M"` - QUICK" PGSYSUSER="`cat ${CONFIG}|sed 's/^ *//g'|sed 's/ *$//g'|grep -v '^#'|grep '^PG_SYS_USER'|head -1|awk '{print $2;}'`"; [ "x${PGSYSUSER}" = "x" ] && PRINT " - cannot find value for the PG_SYS_USER parameter. will try to determine from OS type" && unset PGSYSUSER PGPASSWORD="`cat ${CONFIG}|sed 's/^ *//g'|sed 's/ *$//g'|grep -v '^#'|grep '^PG_SYS_PASSWORD'|head -1|awk '{print $2;}'`"; [ "x${PGPASSWORD}" = "x" ] && PRINT " - cannot find value for the PG_SYS_PASSWORD parameter. assuming trusted access" && unset PGPASSWORD PGHOST="`cat ${CONFIG}|sed 's/^ *//g'|sed 's/ *$//g'|grep -v '^#'|grep '^PG_HOST'|head -1|awk '{print $2;}'`"; [ "x${PGHOST}" = "x" ] && PRINT " - cannot find value for the PG_HOST parameter. assuming native socket access" PGPORT="`cat ${CONFIG}|sed 's/^ *//g'|sed 's/ *$//g'|grep -v '^#'|grep '^PG_PORT'|head -1|awk '{print $2;}'`"; [ "x${PGPORT}" = "x" ] && PRINT " - cannot find value for the PG_PORT parameter. assuming default port or native socket" PG_DUMPFILE="`cat ${CONFIG}|sed 's/^ *//g'|sed 's/ *$//g'|grep -v '^#'|grep '^PG_DUMPFILE'|head -1|awk '{print $2;}'`"; [ "x${PG_DUMPFILE}" = "x" ] && PRINT " - cannot find value for the PG_DUMPFILE parameter. assuming no backup for now" [ "x${OS}" = "x${DARWIN}" ] && PGSYSUSER="postgres" [ "x${OS}" = "x${LINUX}" ] && PGSYSUSER="postgres" [ "x${OS}" = "x${FREEBSD}" ] && PGSYSUSER="pgsql" [ "x${PGSYSUSER}" = "x" ] && ERROR "non Linux or FreeBSD OS" PG_VERSION_FILE="`cat /etc/passwd|grep ^${PGSYSUSER}:|awk 'BEGIN{FS=\":\"}{print $6"/data/PG_VERSION";}'`" [ "x${PG_VERSION_FILE}" = "x" ] && [ "x${OS}" = "x${DARWIN}" ] && PG_VERSION_FILE="`nidump passwd /|grep ^${PGSYSUSER}:|awk -F: '{print $9"/data/PG_VERSION";}'`" [ "x${PG_VERSION_FILE}" = "x" ] && ERROR "PG_VERSION_FILE parameters cannot be found" PRINT " + PG_VERSION_FILE is ${PG_VERSION_FILE}" [ ! -f ${PG_VERSION_FILE} ] && ERROR "${PG_VERSION_FILE} file not found" PG_VERSION="`cat ${PG_VERSION_FILE}`" [ "x${PG_VERSION}" = "x" ] && ERROR "${PG_VERSION_FILE} info missing" PRINT " + PG_VERSION is ${PG_VERSION}" PGUSER=${PGSYSUSER} POSTGRESQL_OLD="NO" ################### if [ "x${PG_VERSION}" = "x7.1" ]; then POSTGRESQL_OLD="YES" [ "x${VAC_PARAM}" = "x--full" ] && VAC_PARAM="" fi PRINT " ==================================================" PRINT " SILENCE = [${SILENCE}]" PRINT " CONFIG FILE [${CONFIG}] PARAMETERS:" PRINT " --------------------------------------------------" PRINT " PGUSER = [${PGUSER}]" PRINT " PGHOST = [${PGHOST}]" PRINT " PGPORT = [${PGPORT}]" PRINT " PG_DUMPFILE = [${PG_DUMPFILE}]" PRINT " BACKUP = [${BACKUP}]" PRINT " VACUUM = [${VACUUM}]" [ "x${VACUUM}" = "xYES" ] && [ "x${VAC_PARAM}" != "x" ] && PRINT " VAC_PARAM = [${VAC_PARAM}]" PRINT " ==================================================" if [ "x${VACUUM}" = "xYES" ]; then if which ${PG_VACUUM} >/dev/null; then PRINT " + ${PG_VACUUM} utility found in the system paths" else ERROR "${PG_VACUUM} utility not found in the system paths" fi fi if which ${PG_DUMPALL} >/dev/null; then PRINT " + ${PG_DUMPALL} utility found in the system paths" else ERROR "${PG_DUMPALL} utility not found in the system paths" fi export PGUSER PGPASSWORD PGHOST PGPORT if [ "x${VACUUM}" = "xYES" ]; then PRINT " * PostgreSQL DB vacuum ..." QUICK ' + ' -n if [ "x${POSTGRESQL_OLD}" = "xYES" ]; then ( echo "${PGPASSWORD}" ) | ${PG_VACUUM} -h ${PGHOST} -p ${PGPORT} -U ${PGUSER} -a ${VAC_PARAM} || \ ERROR "PostgreSQL vacuum error." else ${PG_VACUUM} -a ${VAC_PARAM} || \ ERROR "PostgreSQL vacuum error." fi PRINT " + PostgreSQL DB vacuum ... OK" fi if [ "x${BACKUP}" = "xYES" ]; then PRINT " * PostgreSQL DB backup ..." [ "x${PG_DUMPFILE}" = "x" ] && ERROR "cannot find value for the PG_DUMPFILE parameter" if [ "x${POSTGRESQL_OLD}" = "xYES" ]; then ( echo "${PGUSER}"; echo "${PGPASSWORD}" ) | ${PG_DUMPALL} -f ${PG_DUMPFILE} -h ${PGHOST} -p ${PGPORT} -u || \ ERROR "PostgreSQL dump error." else ${PG_DUMPALL} > ${PG_DUMPFILE} || \ ERROR "PostgreSQL dump error." fi chown 0:0 ${PG_DUMPFILE} || exit 1 chmod 600 ${PG_DUMPFILE} || exit 1 PRINT " + PostgreSQL DB backup ... OK" QUICK " + PostgreSQL DB BACKUP" fi PRINT " # OK" QUICK " # OK" exit 0