|
@@ -1,5 +1,7 @@
|
|
#!/bin/bash
|
|
#!/bin/bash
|
|
-#
|
|
|
|
|
|
+
|
|
|
|
+# {{{ License and Copyright
|
|
|
|
+
|
|
# PostgreSQL Backup Script
|
|
# PostgreSQL Backup Script
|
|
# https://github.com/k0lter/autopostgresqlbackup
|
|
# https://github.com/k0lter/autopostgresqlbackup
|
|
# Copyright (c) 2005 Aaron Axelsen <axelseaa@amadmax.com>
|
|
# Copyright (c) 2005 Aaron Axelsen <axelseaa@amadmax.com>
|
|
@@ -25,6 +27,10 @@
|
|
# (Detailed instructions below variables)
|
|
# (Detailed instructions below variables)
|
|
#=====================================================================
|
|
#=====================================================================
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ Variables
|
|
|
|
+
|
|
# Username to access the PostgreSQL server e.g. dbuser
|
|
# Username to access the PostgreSQL server e.g. dbuser
|
|
USERNAME=postgres
|
|
USERNAME=postgres
|
|
|
|
|
|
@@ -130,6 +136,10 @@ ENCRYPTION_SUFFIX=".enc"
|
|
# Command run after backups (uncomment to use)
|
|
# Command run after backups (uncomment to use)
|
|
#POSTBACKUP="/etc/postgresql-backup-post"
|
|
#POSTBACKUP="/etc/postgresql-backup-post"
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ OS Specific
|
|
|
|
+
|
|
#=====================================================================
|
|
#=====================================================================
|
|
# Debian specific options ===
|
|
# Debian specific options ===
|
|
#=====================================================================
|
|
#=====================================================================
|
|
@@ -138,6 +148,10 @@ if [ -f /etc/default/autopostgresqlbackup ]; then
|
|
. /etc/default/autopostgresqlbackup
|
|
. /etc/default/autopostgresqlbackup
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ Documentation
|
|
|
|
+
|
|
#=====================================================================
|
|
#=====================================================================
|
|
# Options documentation
|
|
# Options documentation
|
|
#=====================================================================
|
|
#=====================================================================
|
|
@@ -270,16 +284,11 @@ fi
|
|
# you are piping the file.sql to psql and not the other way around.
|
|
# you are piping the file.sql to psql and not the other way around.
|
|
#
|
|
#
|
|
# Lets hope you never have to use this.. :)
|
|
# Lets hope you never have to use this.. :)
|
|
-#
|
|
|
|
-#=====================================================================
|
|
|
|
-#=====================================================================
|
|
|
|
-#=====================================================================
|
|
|
|
-#
|
|
|
|
-# Should not need to be modified from here down!!
|
|
|
|
-#
|
|
|
|
-#=====================================================================
|
|
|
|
-#=====================================================================
|
|
|
|
-#=====================================================================
|
|
|
|
|
|
+
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ Defaults
|
|
|
|
+
|
|
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/postgres/bin:/usr/local/pgsql/bin
|
|
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/postgres/bin:/usr/local/pgsql/bin
|
|
DATE=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2002-09-21
|
|
DATE=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2002-09-21
|
|
DOW=`date +%A` # Day of the week e.g. Monday
|
|
DOW=`date +%A` # Day of the week e.g. Monday
|
|
@@ -331,10 +340,49 @@ exec 7>&2 # Link file descriptor #7 with stderr.
|
|
# Saves stderr.
|
|
# Saves stderr.
|
|
exec 2> ${LOGERR} # stderr replaced with file ${LOGERR}.
|
|
exec 2> ${LOGERR} # stderr replaced with file ${LOGERR}.
|
|
|
|
|
|
|
|
+if [ "${SEPDIR}" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump
|
|
|
|
+ if [ "${CREATE}_DATABASE" = "no" ]; then
|
|
|
|
+ OPT="${OPT}"
|
|
|
|
+ else
|
|
|
|
+ OPT="${OPT} --create"
|
|
|
|
+ fi
|
|
|
|
+else
|
|
|
|
+ OPT="${OPT}"
|
|
|
|
+fi
|
|
|
|
+
|
|
|
|
+# Hostname for LOG information
|
|
|
|
+if [ "${DBHOST}" = "localhost" ]; then
|
|
|
|
+ HOST=`hostname`
|
|
|
|
+ PGHOST=""
|
|
|
|
+else
|
|
|
|
+ HOST=${DBHOST}
|
|
|
|
+ PGHOST="-h ${DBHOST}"
|
|
|
|
+fi
|
|
|
|
+
|
|
|
|
+# If backing up all DBs on the server
|
|
|
|
+if [ "${DBNAMES}" = "all" ]; then
|
|
|
|
+ if [ -n "${SU}_USERNAME" ]; then
|
|
|
|
+ DBNAMES="$(su - ${SU}_USERNAME -l -c "LANG=C psql -U ${USERNAME} ${PGHOST} -l -A -F: | sed -ne '/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }'")"
|
|
|
|
+ else
|
|
|
|
+ DBNAMES="`LANG=C psql -U ${USERNAME} ${PGHOST} -l -A -F: | sed -ne "/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }"`"
|
|
|
|
+ fi
|
|
|
|
|
|
-# Functions
|
|
|
|
|
|
+ # If DBs are excluded
|
|
|
|
+ for exclude in ${DBEXCLUDE} ; do
|
|
|
|
+ DBNAMES=`echo ${DBNAMES} | sed "s/\b${exclude}\b//g"`
|
|
|
|
+ done
|
|
|
|
+ DBNAMES="$(echo ${DBNAMES}| tr '\n' ' ')"
|
|
|
|
+ MDBNAMES=${DBNAMES}
|
|
|
|
+fi
|
|
|
|
+
|
|
|
|
+# Include global objects (users, tablespaces)
|
|
|
|
+DBNAMES="${GLOBALS}_OBJECTS ${DBNAMES}"
|
|
|
|
+MDBNAMES="${GLOBALS}_OBJECTS ${MDBNAMES}"
|
|
|
|
+
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ dbdump()
|
|
|
|
|
|
-# Database dump function
|
|
|
|
dbdump () {
|
|
dbdump () {
|
|
rm -f "${2}"
|
|
rm -f "${2}"
|
|
touch "${2}"
|
|
touch "${2}"
|
|
@@ -357,7 +405,10 @@ dbdump () {
|
|
return 0
|
|
return 0
|
|
}
|
|
}
|
|
|
|
|
|
-# Encryption function
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ encryption()
|
|
|
|
+
|
|
encryption() {
|
|
encryption() {
|
|
ENCRYPTED_FILE="${1}${ENCRYPTION}_SUFFIX"
|
|
ENCRYPTED_FILE="${1}${ENCRYPTION}_SUFFIX"
|
|
# Encrypt as needed
|
|
# Encrypt as needed
|
|
@@ -377,6 +428,10 @@ encryption() {
|
|
return 0
|
|
return 0
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ compression()
|
|
|
|
+
|
|
# Compression (and encrypt) function plus latest copy
|
|
# Compression (and encrypt) function plus latest copy
|
|
SUFFIX=""
|
|
SUFFIX=""
|
|
compression () {
|
|
compression () {
|
|
@@ -409,6 +464,10 @@ compression () {
|
|
return 0
|
|
return 0
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ PreBackup
|
|
|
|
+
|
|
# Run command before we begin
|
|
# Run command before we begin
|
|
if [ "${PREBACKUP}" ]
|
|
if [ "${PREBACKUP}" ]
|
|
then
|
|
then
|
|
@@ -421,45 +480,9 @@ if [ "${PREBACKUP}" ]
|
|
echo
|
|
echo
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
|
|
-if [ "${SEPDIR}" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump
|
|
|
|
- if [ "${CREATE}_DATABASE" = "no" ]; then
|
|
|
|
- OPT="${OPT}"
|
|
|
|
- else
|
|
|
|
- OPT="${OPT} --create"
|
|
|
|
- fi
|
|
|
|
-else
|
|
|
|
- OPT="${OPT}"
|
|
|
|
-fi
|
|
|
|
-
|
|
|
|
-# Hostname for LOG information
|
|
|
|
-if [ "${DBHOST}" = "localhost" ]; then
|
|
|
|
- HOST=`hostname`
|
|
|
|
- PGHOST=""
|
|
|
|
-else
|
|
|
|
- HOST=${DBHOST}
|
|
|
|
- PGHOST="-h ${DBHOST}"
|
|
|
|
-fi
|
|
|
|
-
|
|
|
|
-# If backing up all DBs on the server
|
|
|
|
-if [ "${DBNAMES}" = "all" ]; then
|
|
|
|
- if [ -n "${SU}_USERNAME" ]; then
|
|
|
|
- DBNAMES="$(su - ${SU}_USERNAME -l -c "LANG=C psql -U ${USERNAME} ${PGHOST} -l -A -F: | sed -ne '/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }'")"
|
|
|
|
- else
|
|
|
|
- DBNAMES="`LANG=C psql -U ${USERNAME} ${PGHOST} -l -A -F: | sed -ne "/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }"`"
|
|
|
|
- fi
|
|
|
|
-
|
|
|
|
- # If DBs are excluded
|
|
|
|
- for exclude in ${DBEXCLUDE} ; do
|
|
|
|
- DBNAMES=`echo ${DBNAMES} | sed "s/\b${exclude}\b//g"`
|
|
|
|
- done
|
|
|
|
- DBNAMES="$(echo ${DBNAMES}| tr '\n' ' ')"
|
|
|
|
- MDBNAMES=${DBNAMES}
|
|
|
|
-fi
|
|
|
|
-
|
|
|
|
-# Include global objects (users, tablespaces)
|
|
|
|
-DBNAMES="${GLOBALS}_OBJECTS ${DBNAMES}"
|
|
|
|
-MDBNAMES="${GLOBALS}_OBJECTS ${MDBNAMES}"
|
|
|
|
|
|
+# {{{ main()
|
|
|
|
|
|
echo ======================================================================
|
|
echo ======================================================================
|
|
echo AutoPostgreSQLBackup VER ${VER}
|
|
echo AutoPostgreSQLBackup VER ${VER}
|
|
@@ -584,6 +607,10 @@ echo Size - Location
|
|
echo `du -hs "${BACKUPDIR}"`
|
|
echo `du -hs "${BACKUPDIR}"`
|
|
echo
|
|
echo
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ PostBackup
|
|
|
|
+
|
|
# Run command when we're done
|
|
# Run command when we're done
|
|
if [ "${POSTBACKUP}" ]
|
|
if [ "${POSTBACKUP}" ]
|
|
then
|
|
then
|
|
@@ -595,10 +622,18 @@ if [ "${POSTBACKUP}" ]
|
|
echo ======================================================================
|
|
echo ======================================================================
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ cleanup I/O
|
|
|
|
+
|
|
#Clean up IO redirection
|
|
#Clean up IO redirection
|
|
exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
|
|
exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
|
|
exec 2>&7 7>&- # Restore stdout and close file descriptor #7.
|
|
exec 2>&7 7>&- # Restore stdout and close file descriptor #7.
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ Reporting
|
|
|
|
+
|
|
if [ "${MAILCONTENT}" = "files" ]; then
|
|
if [ "${MAILCONTENT}" = "files" ]; then
|
|
if [ -s "${LOGERR}" ]; then
|
|
if [ -s "${LOGERR}" ]; then
|
|
# Include error log if is larger than zero.
|
|
# Include error log if is larger than zero.
|
|
@@ -649,6 +684,9 @@ else
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# {{{ Cleanup logs and exit()
|
|
if [ -s "${LOGERR}" ]; then
|
|
if [ -s "${LOGERR}" ]; then
|
|
STATUS=1
|
|
STATUS=1
|
|
else
|
|
else
|
|
@@ -660,3 +698,7 @@ rm -f "${LOGFILE}"
|
|
rm -f "${LOGERR}"
|
|
rm -f "${LOGERR}"
|
|
|
|
|
|
exit ${STATUS}
|
|
exit ${STATUS}
|
|
|
|
+
|
|
|
|
+# }}}
|
|
|
|
+
|
|
|
|
+# vim: foldmethod=marker foldlevel=0 foldenable
|