Browse Source

Add vim folding

Emmanuel Bouthenot 2 years ago
parent
commit
0469a4d5b1
1 changed files with 94 additions and 52 deletions
  1. 94 52
      autopostgresqlbackup

+ 94 - 52
autopostgresqlbackup

@@ -1,5 +1,7 @@
 #!/bin/bash
-#
+
+# {{{ License and Copyright
+
 # PostgreSQL Backup Script
 # https://github.com/k0lter/autopostgresqlbackup
 # Copyright (c) 2005 Aaron Axelsen <axelseaa@amadmax.com>
@@ -25,6 +27,10 @@
 # (Detailed instructions below variables)
 #=====================================================================
 
+# }}}
+
+# {{{ Variables
+
 # Username to access the PostgreSQL server e.g. dbuser
 USERNAME=postgres
 
@@ -130,6 +136,10 @@ ENCRYPTION_SUFFIX=".enc"
 # Command run after backups (uncomment to use)
 #POSTBACKUP="/etc/postgresql-backup-post"
 
+# }}}
+
+# {{{ OS Specific
+
 #=====================================================================
 # Debian specific options ===
 #=====================================================================
@@ -138,6 +148,10 @@ if [ -f /etc/default/autopostgresqlbackup ]; then
     . /etc/default/autopostgresqlbackup
 fi
 
+# }}}
+
+# {{{ Documentation
+
 #=====================================================================
 # Options documentation
 #=====================================================================
@@ -270,16 +284,11 @@ fi
 # you are piping the file.sql to psql and not the other way around.
 #
 # 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
 DATE=`date +%Y-%m-%d_%Hh%Mm`    # Datestamp e.g 2002-09-21
 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.
 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 () {
     rm -f "${2}"
     touch "${2}"
@@ -357,7 +405,10 @@ dbdump () {
     return 0
 }
 
-# Encryption function
+# }}}
+
+# {{{ encryption()
+
 encryption() {
     ENCRYPTED_FILE="${1}${ENCRYPTION}_SUFFIX"
     # Encrypt as needed
@@ -377,6 +428,10 @@ encryption() {
     return 0
 }
 
+# }}}
+
+# {{{ compression()
+
 # Compression (and encrypt) function plus latest copy
 SUFFIX=""
 compression () {
@@ -409,6 +464,10 @@ compression () {
     return 0
 }
 
+# }}}
+
+# {{{ PreBackup
+
 # Run command before we begin
 if [ "${PREBACKUP}" ]
     then
@@ -421,45 +480,9 @@ if [ "${PREBACKUP}" ]
     echo
 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 AutoPostgreSQLBackup VER ${VER}
@@ -584,6 +607,10 @@ echo Size - Location
 echo `du -hs "${BACKUPDIR}"`
 echo
 
+# }}}
+
+# {{{ PostBackup
+
 # Run command when we're done
 if [ "${POSTBACKUP}" ]
     then
@@ -595,10 +622,18 @@ if [ "${POSTBACKUP}" ]
     echo ======================================================================
 fi
 
+# }}}
+
+# {{{ cleanup I/O
+
 #Clean up IO redirection
 exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.
 exec 2>&7 7>&-      # Restore stdout and close file descriptor #7.
 
+# }}}
+
+# {{{ Reporting
+
 if [ "${MAILCONTENT}" = "files" ]; then
     if [ -s "${LOGERR}" ]; then
         # Include error log if is larger than zero.
@@ -649,6 +684,9 @@ else
     fi
 fi
 
+# }}}
+
+# {{{ Cleanup logs and exit()
 if [ -s "${LOGERR}" ]; then
     STATUS=1
 else
@@ -660,3 +698,7 @@ rm -f "${LOGFILE}"
 rm -f "${LOGERR}"
 
 exit ${STATUS}
+
+# }}}
+
+# vim: foldmethod=marker foldlevel=0 foldenable