[Bps-public-commit] postgresql_auto_backup_s3 branch update-for-hostedrt-backups created. 944726e1a7f5a7b38bfe3a9c4aeaaaeb38c81610

BPS Git Server git at git.bestpractical.com
Tue Mar 15 23:47:51 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "postgresql_auto_backup_s3".

The branch, update-for-hostedrt-backups has been created
        at  944726e1a7f5a7b38bfe3a9c4aeaaaeb38c81610 (commit)

- Log -----------------------------------------------------------------
commit 944726e1a7f5a7b38bfe3a9c4aeaaaeb38c81610
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Tue Mar 15 18:39:59 2022 -0500

    Update README.md with usage, config, and setup

diff --git a/README.md b/README.md
index b6c2182..5fc34e0 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,107 @@
-postgresql_auto_backup_s3
-=========================
+# postgresql_auto_backup_s3
 
 Configurable backup scripts to automate postgresql backups and push to s3 via stdin, plus rotation logic.
 
-Contributors welcome!
+## SYNOPSIS
 
-Original code from https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux.
+```
+Usage: pg_backup_rotated.sh [-c <config_file>] [-q] [-h]
 
-Will be adapted to work with s3cmd stdin pushing, therefore needs s3cmd >= s3cmd 1.5.0-alpha1 , also see http://s3tools.org/news
+Options:
+    -c, --config <config_file>    load a specified config file
+    -q, --quiet                   silence output
+    -h, --help                    display this help and exit
+```
+
+## DESCRIPTION
+
+`postgresql_auto_backup_s3` provides the following scripts to create postgresql backups:
+
+### `pg_backup.sh`
+
+This script will create postgresql backups to a local disk directory, without backup rotation.
+
+### `pg_backup_rotated.sh`
+
+This script will create postgresql backups to an s3 bucket, with weekly and monthly backup rotation.
+
+## CONFIGURATION
+
+Configuration is done through through the `pg_backup.config` file, and contains the following variables:
+
+### `BACKUP_USER`
+
+Optional system user to run backups as.  If the user the script is running as doesn't match this the script terminates.  Leave blank to skip check.
+
+### `HOSTNAME`
+
+Optional hostname to adhere to pg_hba policies.  Will default to `localhost` if none specified.
+
+### `USERNAME`
+
+Optional username to connect to database as.  Will default to `postgres` if none specified.
+
+### `BACKUP_DIR`
+
+If running `pg_backup.sh`, `BACKUP_DIR` specifies the directory to write backups to, and will be created if it doesn't exist.  This must be writable by the user the script is running as.
+
+If running `pg_backup_rotated.sh`, `BACKUP_DIR` specifies the s3 bucket to write backups to.
+
+### `BACKUP_DB_LIST`
+
+List of database names to backup, separated by space or comma.
+
+### `DAY_OF_WEEK_TO_KEEP`
+
+Which day to take the weekly backup from (1-7 = Monday-Sunday).
+
+### `DAYS_TO_KEEP`
+
+Number of days to keep daily backups.
+
+### `WEEKS_TO_KEEP`
+
+How many weeks to keep weekly backups.
+
+## DEPENDENCIES
+
+`pg_backup_rotated.sh` requires the external program, `aws-cli` to upload backups to s3.  Please see the [aws-cli installation and setup instructions](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) for more detail.
+
+## EXAMPLES
+
+### create local backups for two databases using a specified config file
+
+```
+$ cp -a pg_backup.config pg_backup.config.local
+$ vi pg_backup.config.local
+BACKUP_DIR=/home/bps/backups/database/postgresql/
+BACKUP_DB_LIST="rt4 rt5"
+
+$ bash pg_backup.sh --config pg_backup.config.local
+creating backup directory - /home/bps/backups/database/postgresql/2022-03-15/
+creating backup - /home/bps/backups/database/postgresql/2022-03-15/rt4.sql.gz
+creating backup - /home/bps/backups/database/postgresql/2022-03-15/rt5.sql.gz
+```
+
+### create backups and upload to s3 for two databases using a specified config file
+
+```
+$ cp -a pg_backup.config pg_backup.config.s3
+$ vi pg_backup.config.s3
+BACKUP_DIR=s3://s3backup-test-backup/pg_backups/
+BACKUP_DB_LIST="rt4 rt5"
+
+$ bash pg_backup_rotated.sh --config pg_backup.config.s3
+creating daily backup - s3://s3backup-test-backup/pg_backups/20220315-daily-rt4.sql.gz
+creating daily backup - s3://s3backup-test-backup/pg_backups/20220315-daily-rt5.sql.gz
+```
+
+## CAVEATS
+
+The backup scripts in `postgresql_auto_backup_s3` have been specifically modified to backup RT databases as outlined in the [RT 5.0.2 database administration documentation](https://docs.bestpractical.com/rt/5.0.2/system_administration/database.html#PostgreSQL).
+
+Backing up non-RT databases will warn about not finding the `sessions` table.
+
+```
+pg_dump: no matching tables were found
+```
commit e729a3720b8ee34ae5c33f08190d1b653a19f431
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Tue Mar 15 17:41:20 2022 -0500

    Remove code section comments

diff --git a/pg_backup.sh b/pg_backup.sh
index c7012c2..329017f 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -1,9 +1,5 @@
 #!/bin/bash
  
-###########################
-####### LOAD CONFIG #######
-###########################
-
 function print_usage() {
         echo "Usage: $0 [-c <config_file>] [-q] [-h]"
 }
@@ -52,21 +48,12 @@ fi
 
 source "${CONFIG_FILE_PATH}"
 
-###########################
-#### PRE-BACKUP CHECKS ####
-###########################
- 
 # Make sure we're running as the required backup user
 if [[ "$BACKUP_USER" != "" && "$(id -un)" != "$BACKUP_USER" ]]; then
         echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
         exit 1;
 fi;
  
- 
-###########################
-### INITIALISE DEFAULTS ###
-###########################
- 
 if [[ -z "$HOSTNAME" ]]; then
         HOSTNAME="localhost"
 fi;
@@ -75,11 +62,6 @@ if [[ -z "$USERNAME" ]]; then
         USERNAME="postgres"
 fi;
  
- 
-###########################
-#### START THE BACKUPS ####
-###########################
- 
 FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y-\%m-\%d`/"
 
 if [[ "$QUIET" -ne "1" ]]; then
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index 83fb975..011114f 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -1,9 +1,5 @@
 #!/bin/bash
  
-###########################
-####### LOAD CONFIG #######
-###########################
-
 function print_usage() {
         echo "Usage: $0 [-c <config_file>] [-q] [-h]"
 }
@@ -52,21 +48,12 @@ fi
  
 source "${CONFIG_FILE_PATH}"
  
-###########################
-#### PRE-BACKUP CHECKS ####
-###########################
- 
 # Make sure we're running as the required backup user
 if [[ "$BACKUP_USER" != "" && "$(id -un)" != "$BACKUP_USER" ]]; then
         echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
         exit 1
 fi
  
- 
-###########################
-### INITIALISE DEFAULTS ###
-###########################
- 
 if [[ -z "$HOSTNAME" ]]; then
         HOSTNAME="localhost"
 fi;
@@ -75,11 +62,6 @@ if [[ -z "$USERNAME" ]]; then
         USERNAME="postgres"
 fi;
  
- 
-###########################
-#### START THE BACKUPS ####
-###########################
- 
 function perform_backups()
 {
         SUFFIX=$1
commit 3b4901ed5e51e57e573763cd3ae2db9c37bb9659
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Tue Mar 15 17:37:33 2022 -0500

    Add help dialogue and long options
    
    This commit adds the -h|--help option for help dialogue, as well as
    long options for -c|--config and -q|--quiet.

diff --git a/pg_backup.sh b/pg_backup.sh
index 480789c..c7012c2 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -4,18 +4,36 @@
 ####### LOAD CONFIG #######
 ###########################
 
+function print_usage() {
+        echo "Usage: $0 [-c <config_file>] [-q] [-h]"
+}
+
+function print_options() {
+        echo
+        echo "Options:"
+        echo "    -c, --config <config_file>    load a specified config file"
+        echo "    -q, --quiet                   silence output"
+        echo "    -h, --help                    display this help and exit"
+}
+
 while [[ "$#" -gt "0" ]]; do
         case $1 in
-                -c)
+                -c|--config)
                         CONFIG_FILE_PATH="$2"
                         shift 2
                         ;;
-                -q)
+                -q|--quiet)
                         QUIET=1
                         shift 1
                         ;;
+                -h|--help)
+                        print_usage
+                        print_options
+                        exit 0
+                        ;;
                 *)
-                        ${ECHO} "Unknown Option \"$1\"" 1>&2
+                        echo "unrecognized option '$1'" 1>&2
+                        print_usage
                         exit 2
                         ;;
         esac
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index 6a2b8bb..83fb975 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -3,24 +3,42 @@
 ###########################
 ####### LOAD CONFIG #######
 ###########################
- 
+
+function print_usage() {
+        echo "Usage: $0 [-c <config_file>] [-q] [-h]"
+}
+
+function print_options() {
+        echo
+        echo "Options:"
+        echo "    -c, --config <config_file>    load a specified config file"
+        echo "    -q, --quiet                   silence output"
+        echo "    -h, --help                    display this help and exit"
+}
+
 while [[ "$#" -gt "0" ]]; do
         case $1 in
-                -c)
+                -c|--config)
                         CONFIG_FILE_PATH="$2"
                         shift 2
                         ;;
-                -q)
+                -q|--quiet)
                         QUIET=1
                         shift 1
                         ;;
+                -h|--help)
+                        print_usage
+                        print_options
+                        exit 0
+                        ;;
                 *)
-                        ${ECHO} "Unknown Option \"$1\"" 1>&2
+                        echo "unrecognized option '$1'" 1>&2
+                        print_usage
                         exit 2
                         ;;
         esac
 done
- 
+
 if [[ -z "$CONFIG_FILE_PATH" ]]; then
         SCRIPT=`realpath $0`
         SCRIPTPATH=`dirname $SCRIPT`
commit 68bceae25051d4e051fd2cc036ad7f56a1c5246f
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Tue Mar 15 16:58:27 2022 -0500

    Update for running through cron
    
    This commit adds the -q option to silence informational runtime
    output, as well as ensures backup failure exits 1, to support
    running through cron.
    
    Additionally, this commit simplifies the non-quiet output and
    cleans up comments.

diff --git a/pg_backup.sh b/pg_backup.sh
index a44e3b7..480789c 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -10,6 +10,10 @@ while [[ "$#" -gt "0" ]]; do
                         CONFIG_FILE_PATH="$2"
                         shift 2
                         ;;
+                -q)
+                        QUIET=1
+                        shift 1
+                        ;;
                 *)
                         ${ECHO} "Unknown Option \"$1\"" 1>&2
                         exit 2
@@ -30,7 +34,6 @@ fi
 
 source "${CONFIG_FILE_PATH}"
 
-
 ###########################
 #### PRE-BACKUP CHECKS ####
 ###########################
@@ -59,34 +62,27 @@ fi;
 #### START THE BACKUPS ####
 ###########################
  
- 
 FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y-\%m-\%d`/"
- 
-echo "Making backup directory in $FINAL_BACKUP_DIR"
- 
+
+if [[ "$QUIET" -ne "1" ]]; then
+        echo "creating backup directory - $FINAL_BACKUP_DIR"
+fi
+
 if ! mkdir -p $FINAL_BACKUP_DIR; then
-        echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2
-        exit 1;
+        echo "failed to create backup directory - $FINAL_BACKUP_DIR" 1>&2
+        exit 1
 fi;
  
- 
-###########################
-###### FULL BACKUPS #######
-###########################
- 
-echo -e "\n\nPerforming full backups"
-echo -e "--------------------------------------------\n"
- 
 for DATABASE in ${BACKUP_DB_LIST//,/ }
 do
-        echo "$DATABASE"
+        if [[ "$QUIET" -ne "1" ]]; then
+                echo "creating backup - $FINAL_BACKUP_DIR$DATABASE.sql.gz"
+        fi
 
         if ! ( pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --table=sessions --schema-only; pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --exclude-table=sessions ) | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then
-                echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
+                echo "[ERROR] failed to create backup - $DATABASE" 1>&2
+                exit 1
         else
                 mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
         fi
- 
 done
- 
-echo -e "\nAll database backups complete!"
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index d66c360..6a2b8bb 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -10,6 +10,10 @@ while [[ "$#" -gt "0" ]]; do
                         CONFIG_FILE_PATH="$2"
                         shift 2
                         ;;
+                -q)
+                        QUIET=1
+                        shift 1
+                        ;;
                 *)
                         ${ECHO} "Unknown Option \"$1\"" 1>&2
                         exit 2
@@ -61,28 +65,21 @@ fi;
 function perform_backups()
 {
         SUFFIX=$1
-        FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y\%m\%d`$SUFFIX-"
- 
-        ###########################
-        ###### FULL BACKUPS #######
-        ###########################
- 
-        echo -e "\n\nPerforming full backups"
-        echo -e "--------------------------------------------\n"
+        FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y\%m\%d`-$SUFFIX-"
  
         for DATABASE in ${BACKUP_DB_LIST//,/ }
         do
-                echo "$DATABASE"
+                if [[ "$QUIET" -ne "1" ]]; then
+                        echo "creating $SUFFIX backup - $FINAL_BACKUP_DIR$DATABASE.sql.gz"
+                fi
 
                 if ! ( pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --table=sessions --schema-only; pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --exclude-table=sessions ) | gzip --stdout | aws s3 cp - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then
-                        echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
+                        echo "[ERROR] failed to create $SUFFIX backup - $DATABASE" 1>&2
+                        exit 1
                 else
-                        aws s3 mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
+                        aws s3 mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz > /dev/null
                 fi
- 
         done
- 
-        echo -e "\nAll database backups complete!"
 }
  
 # MONTHLY BACKUPS
@@ -93,10 +90,14 @@ if [[ "$DAY_OF_MONTH" -eq "1" ]];
 then
         # Delete all expired monthly directories
         for backup in $(aws s3 ls $BACKUP_DIR | awk '{print $4}' | grep '\-monthly'); do
-                aws s3 rm $BACKUP_DIR"$backup"
+                if [[ "$QUIET" -ne "1" ]]; then
+                        echo "deleting monthly backup - $BACKUP_DIR$backup"
+                fi
+
+                aws s3 rm $BACKUP_DIR"$backup" > /dev/null
         done
  
-        perform_backups "-monthly"
+        perform_backups "monthly"
  
         exit 0;
 fi
@@ -113,11 +114,15 @@ then
                 weekly_backup_date=${backup:0:8}
 
                 if [[ "$weekly_backup_date" -le "$weekly_deldate" ]]; then
-                        aws s3 rm $BACKUP_DIR"$backup"
+                        if [[ "$QUIET" -ne "1" ]]; then
+                                echo "deleting weekly backup - $BACKUP_DIR$backup"
+                        fi
+
+                        aws s3 rm $BACKUP_DIR"$backup" > /dev/null
                 fi
         done
 
-        perform_backups "-weekly"
+        perform_backups "weekly"
  
         exit 0;
 fi
@@ -130,8 +135,12 @@ for backup in $(aws s3 ls $BACKUP_DIR | awk '{print $4}' | grep '\-daily'); do
         daily_backup_date=${backup:0:8}
 
         if [[ "$daily_backup_date" -le "$daily_deldate" ]]; then
-                aws s3 rm $BACKUP_DIR"$backup"
+                if [[ "$QUIET" -ne "1" ]]; then
+                        echo "deleting daily backup - $BACKUP_DIR$backup"
+                fi
+
+                aws s3 rm $BACKUP_DIR"$backup" > /dev/null
         fi
 done
 
-perform_backups "-daily"
+perform_backups "daily"
commit 8125c0747e22287f342c34eb7d67def273d3b80f
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Tue Mar 15 14:03:03 2022 -0500

    Add gitignore entry for alternative configs
    
    To keep configuratations out of git history, this commit adds a
    gitignore entry for pg_backup.config.*

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..452d0b6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+pg_backup.config.*
commit 6a7521f293e260aed77bc969d389d1a20b79c302
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Mon Mar 14 19:19:08 2022 -0500

    Remove commented out backup dir code

diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index acbb613..d66c360 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -63,14 +63,6 @@ function perform_backups()
         SUFFIX=$1
         FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y\%m\%d`$SUFFIX-"
  
-        #echo "Making backup directory in $FINAL_BACKUP_DIR"
- 
-        #if ! mkdir -p $FINAL_BACKUP_DIR; then
-        #       echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2
-        #       exit 1;
-        #fi;
- 
- 
         ###########################
         ###### FULL BACKUPS #######
         ###########################
commit d29a3684c06bb3ca9cc7736bbf56aea1e42819ac
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Mon Mar 14 19:07:43 2022 -0500

    Update s3cmd commands to aws-cli
    
    aws-cli is the recommended tool from AWS and supports input from
    STDIN for s3 cp commands.
    
    This commit also updates the backup rotation logic which was not
    completed in the original version.

diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index f0f2421..acbb613 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -82,10 +82,10 @@ function perform_backups()
         do
                 echo "$DATABASE"
 
-                if ! ( pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --table=sessions --schema-only; pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --exclude-table=sessions ) | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress --no-encrypt; then
+                if ! ( pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --table=sessions --schema-only; pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --exclude-table=sessions ) | gzip --stdout | aws s3 cp - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then
                         echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
                 else
-                        s3cmd mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
+                        aws s3 mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
                 fi
  
         done
@@ -100,7 +100,9 @@ DAY_OF_MONTH=`date +%d`
 if [[ "$DAY_OF_MONTH" -eq "1" ]];
 then
         # Delete all expired monthly directories
-        find $BACKUP_DIR -maxdepth 1 -name "*-monthly" -exec rm -rf '{}' ';'
+        for backup in $(aws s3 ls $BACKUP_DIR | awk '{print $4}' | grep '\-monthly'); do
+                aws s3 rm $BACKUP_DIR"$backup"
+        done
  
         perform_backups "-monthly"
  
@@ -110,13 +112,19 @@ fi
 # WEEKLY BACKUPS
  
 DAY_OF_WEEK=`date +%u` #1-7 (Monday-Sunday)
-EXPIRED_DAYS=`expr $((($WEEKS_TO_KEEP * 7) + 1))`
  
 if [[ "$DAY_OF_WEEK" -eq "$DAY_OF_WEEK_TO_KEEP" ]];
 then
-        # Delete all expired weekly directories
-        find $BACKUP_DIR -maxdepth 1 -mtime +$EXPIRED_DAYS -name "*-weekly" -exec rm -rf '{}' ';'
- 
+        weekly_deldate=`date +"%Y%m%d" --date="$WEEKS_TO_KEEP days ago"`
+
+        for backup in $(aws s3 ls $BACKUP_DIR | awk '{print $4}' | grep '\-weekly'); do
+                weekly_backup_date=${backup:0:8}
+
+                if [[ "$weekly_backup_date" -le "$weekly_deldate" ]]; then
+                        aws s3 rm $BACKUP_DIR"$backup"
+                fi
+        done
+
         perform_backups "-weekly"
  
         exit 0;
@@ -124,16 +132,14 @@ fi
  
 # DAILY BACKUPS
  
-# Delete daily backups $DAYS_TO_KEEP days old or more
-#find $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*-daily" -exec rm -rf '{}' ';'
+daily_deldate=`date +"%Y%m%d" --date="$DAYS_TO_KEEP days ago"`
 
-#set $deldate to date minus $DAYS_TO_KEEP
-deldate=`date +"%Y%m%d" --date="$DAYS_TO_KEEP days ago"`
+for backup in $(aws s3 ls $BACKUP_DIR | awk '{print $4}' | grep '\-daily'); do
+        daily_backup_date=${backup:0:8}
 
-#remove leading bucket name from s3 object displayed by "s3cmd ls $bucketname"
-#compare first 8 chars of s3 object name (minus bucket name) with $deldate, 
-#if first 8 chars of s3 object name::int <= $deldate then delete those s3 objects
-s3cmd ls $BACKUP_DIR | awk -v bucket="$BACKUP_DIR" -v deldate="$deldate" '{gsub(bucket,"",$4);if (substr($4,0,9)<=deldate) system("s3cmd info " bucket$4)}' 
-#replace s3cmd info by s3cmd del
+        if [[ "$daily_backup_date" -le "$daily_deldate" ]]; then
+                aws s3 rm $BACKUP_DIR"$backup"
+        fi
+done
 
 perform_backups "-daily"
commit f9a2f7dfc2a264b6ba12541adeb39e56cb808c21
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Fri Mar 11 15:27:27 2022 -0600

    Fix -c option to load config
    
    The -c option to load a non-default config file didn't work for
    pg_backup.sh.  This commit copies the logic from
    pg_backup_rotated.sh over to pg_backup.sh, which does work.

diff --git a/pg_backup.sh b/pg_backup.sh
index 93acb14..a44e3b7 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -3,17 +3,12 @@
 ###########################
 ####### LOAD CONFIG #######
 ###########################
- 
+
 while [[ "$#" -gt "0" ]]; do
         case $1 in
                 -c)
-                        if [[ -r "$2" ]]; then
-                                source "$2"
-                                shift 2
-                        else
-                                ${ECHO} "Unreadable config file \"$2\"" 1>&2
-                                exit 1
-                        fi
+                        CONFIG_FILE_PATH="$2"
+                        shift 2
                         ;;
                 *)
                         ${ECHO} "Unknown Option \"$1\"" 1>&2
@@ -21,13 +16,21 @@ while [[ "$#" -gt "0" ]]; do
                         ;;
         esac
 done
- 
-if [[ "$#" -eq "0" ]]; then
+
+if [[ -z "$CONFIG_FILE_PATH" ]]; then
         SCRIPT=`realpath $0`
         SCRIPTPATH=`dirname $SCRIPT`
-        source $SCRIPTPATH/pg_backup.config
-fi;
- 
+        CONFIG_FILE_PATH="${SCRIPTPATH}/pg_backup.config"
+fi
+
+if [[ ! -r "${CONFIG_FILE_PATH}" ]]; then
+        echo "Could not load config file from ${CONFIG_FILE_PATH}" 1>&2
+        exit 1
+fi
+
+source "${CONFIG_FILE_PATH}"
+
+
 ###########################
 #### PRE-BACKUP CHECKS ####
 ###########################
commit 5686cdb025b0f0f6de99ae67336312defcfcca35
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Fri Mar 11 15:19:24 2022 -0600

    Convert tabs to spaces (retab)

diff --git a/pg_backup.sh b/pg_backup.sh
index 4e7817b..93acb14 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -34,8 +34,8 @@ fi;
  
 # Make sure we're running as the required backup user
 if [[ "$BACKUP_USER" != "" && "$(id -un)" != "$BACKUP_USER" ]]; then
-	echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
-	exit 1;
+        echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
+        exit 1;
 fi;
  
  
@@ -44,11 +44,11 @@ fi;
 ###########################
  
 if [[ -z "$HOSTNAME" ]]; then
-	HOSTNAME="localhost"
+        HOSTNAME="localhost"
 fi;
  
 if [[ -z "$USERNAME" ]]; then
-	USERNAME="postgres"
+        USERNAME="postgres"
 fi;
  
  
@@ -62,8 +62,8 @@ FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y-\%m-\%d`/"
 echo "Making backup directory in $FINAL_BACKUP_DIR"
  
 if ! mkdir -p $FINAL_BACKUP_DIR; then
-	echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2
-	exit 1;
+        echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2
+        exit 1;
 fi;
  
  
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index 675ca7b..f0f2421 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -36,8 +36,8 @@ source "${CONFIG_FILE_PATH}"
  
 # Make sure we're running as the required backup user
 if [[ "$BACKUP_USER" != "" && "$(id -un)" != "$BACKUP_USER" ]]; then
-	echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
-	exit 1
+        echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
+        exit 1
 fi
  
  
@@ -46,11 +46,11 @@ fi
 ###########################
  
 if [[ -z "$HOSTNAME" ]]; then
-	HOSTNAME="localhost"
+        HOSTNAME="localhost"
 fi;
  
 if [[ -z "$USERNAME" ]]; then
-	USERNAME="postgres"
+        USERNAME="postgres"
 fi;
  
  
@@ -60,37 +60,37 @@ fi;
  
 function perform_backups()
 {
-	SUFFIX=$1
-	FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y\%m\%d`$SUFFIX-"
+        SUFFIX=$1
+        FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y\%m\%d`$SUFFIX-"
  
-	#echo "Making backup directory in $FINAL_BACKUP_DIR"
+        #echo "Making backup directory in $FINAL_BACKUP_DIR"
  
-	#if ! mkdir -p $FINAL_BACKUP_DIR; then
-	#	echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2
-	#	exit 1;
-	#fi;
+        #if ! mkdir -p $FINAL_BACKUP_DIR; then
+        #       echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2
+        #       exit 1;
+        #fi;
  
  
-	###########################
-	###### FULL BACKUPS #######
-	###########################
+        ###########################
+        ###### FULL BACKUPS #######
+        ###########################
  
-	echo -e "\n\nPerforming full backups"
-	echo -e "--------------------------------------------\n"
+        echo -e "\n\nPerforming full backups"
+        echo -e "--------------------------------------------\n"
  
-	for DATABASE in ${BACKUP_DB_LIST//,/ }
-	do
-	        echo "$DATABASE"
+        for DATABASE in ${BACKUP_DB_LIST//,/ }
+        do
+                echo "$DATABASE"
 
-		if ! ( pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --table=sessions --schema-only; pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --exclude-table=sessions ) | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress --no-encrypt; then
-	                echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
-	        else
-	                s3cmd mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
-	        fi
+                if ! ( pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --table=sessions --schema-only; pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --exclude-table=sessions ) | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress --no-encrypt; then
+                        echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
+                else
+                        s3cmd mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
+                fi
  
-	done
+        done
  
-	echo -e "\nAll database backups complete!"
+        echo -e "\nAll database backups complete!"
 }
  
 # MONTHLY BACKUPS
@@ -99,12 +99,12 @@ DAY_OF_MONTH=`date +%d`
  
 if [[ "$DAY_OF_MONTH" -eq "1" ]];
 then
-	# Delete all expired monthly directories
-	find $BACKUP_DIR -maxdepth 1 -name "*-monthly" -exec rm -rf '{}' ';'
+        # Delete all expired monthly directories
+        find $BACKUP_DIR -maxdepth 1 -name "*-monthly" -exec rm -rf '{}' ';'
  
-	perform_backups "-monthly"
+        perform_backups "-monthly"
  
-	exit 0;
+        exit 0;
 fi
  
 # WEEKLY BACKUPS
@@ -114,12 +114,12 @@ EXPIRED_DAYS=`expr $((($WEEKS_TO_KEEP * 7) + 1))`
  
 if [[ "$DAY_OF_WEEK" -eq "$DAY_OF_WEEK_TO_KEEP" ]];
 then
-	# Delete all expired weekly directories
-	find $BACKUP_DIR -maxdepth 1 -mtime +$EXPIRED_DAYS -name "*-weekly" -exec rm -rf '{}' ';'
+        # Delete all expired weekly directories
+        find $BACKUP_DIR -maxdepth 1 -mtime +$EXPIRED_DAYS -name "*-weekly" -exec rm -rf '{}' ';'
  
-	perform_backups "-weekly"
+        perform_backups "-weekly"
  
-	exit 0;
+        exit 0;
 fi
  
 # DAILY BACKUPS
commit 6e5258167dbde23458e09010f1be4c1526f93d02
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Fri Mar 11 15:03:01 2022 -0600

    Update pg_dump commands for RT specific backups
    
    The pg_dump commands now specifically exclude the sessions table
    data as outlined in our documentation.

diff --git a/pg_backup.sh b/pg_backup.sh
index 9bfd8fe..4e7817b 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -78,7 +78,7 @@ for DATABASE in ${BACKUP_DB_LIST//,/ }
 do
         echo "$DATABASE"
 
-        if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then
+        if ! ( pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --table=sessions --schema-only; pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --exclude-table=sessions ) | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then
                 echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
         else
                 mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index cf470d1..675ca7b 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -82,7 +82,7 @@ function perform_backups()
 	do
 	        echo "$DATABASE"
 
-		if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress --no-encrypt; then
+		if ! ( pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --table=sessions --schema-only; pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" --exclude-table=sessions ) | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress --no-encrypt; then
 	                echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
 	        else
 	                s3cmd mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
commit 6dd34bd76e8d646dbca92104941274b47f9ab018
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Fri Mar 11 14:39:52 2022 -0600

    Remove custom and plain backup configuration
    
    This commit removes the option and command to create custom
    backups.  The scripts now only create plain backups and don't have
    a configurable option to enable or disable the backup creation.

diff --git a/pg_backup.config b/pg_backup.config
index c84a5c5..8374983 100644
--- a/pg_backup.config
+++ b/pg_backup.config
@@ -21,12 +21,6 @@ BACKUP_DIR=s3://pg_backups/
 # List of database names to backup, separated by space or comma.
 BACKUP_DB_LIST=""
  
-# Will produce a custom-format backup if set to "yes"
-ENABLE_CUSTOM_BACKUPS=yes
- 
-# Will produce a gzipped plain-format backup if set to "yes"
-ENABLE_PLAIN_BACKUPS=yes
- 
  
 #### SETTINGS FOR ROTATED BACKUPS ####
  
diff --git a/pg_backup.sh b/pg_backup.sh
index ea2cecb..9bfd8fe 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -76,27 +76,13 @@ echo -e "--------------------------------------------\n"
  
 for DATABASE in ${BACKUP_DB_LIST//,/ }
 do
-	if [[ "$ENABLE_PLAIN_BACKUPS" == "yes" ]]
-	then
-		echo "Plain backup of $DATABASE"
- 
-		if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then
-			echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE" 1>&2
-		else
-			mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
-		fi
-	fi
- 
-	if [[ $ENABLE_CUSTOM_BACKUPS == "yes" ]]
-	then
-		echo "Custom backup of $DATABASE"
- 
-		if ! pg_dump -Fc -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" -f $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress; then
-			echo "[!!ERROR!!] Failed to produce custom backup database $DATABASE" 1>&2
-		else
-			mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom
-		fi
-	fi
+        echo "$DATABASE"
+
+        if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then
+                echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
+        else
+                mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
+        fi
  
 done
  
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index b6b6105..cf470d1 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -80,26 +80,13 @@ function perform_backups()
  
 	for DATABASE in ${BACKUP_DB_LIST//,/ }
 	do
-		if [[ "$ENABLE_PLAIN_BACKUPS" == "yes" ]]
-		then
-			echo "Plain backup of $DATABASE"
- 			
-			if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress --no-encrypt; then
-	                	echo "[!!ERROR!!] Failed to produce plain backup database schema of $DATABASE" 1>&2
-	        	else
-	                	s3cmd mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
-	        	fi
-		fi
- 
-		if [[ "$ENABLE_CUSTOM_BACKUPS" == "yes" ]]
-		then
-			echo "Custom backup of $DATABASE"
- 			if ! pg_dump -Fc -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress --no-encrypt; then
-	                	echo "[!!ERROR!!] Failed to produce custom backup database schema of $DATABASE" 1>&2
-	        	else
-	                	s3cmd mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom
-	        	fi
-		fi
+	        echo "$DATABASE"
+
+		if ! pg_dump -Fp -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress --no-encrypt; then
+	                echo "[!!ERROR!!] Failed to produce backup of database $DATABASE" 1>&2
+	        else
+	                s3cmd mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz
+	        fi
  
 	done
  
commit c43d8d091fefcaeee278358379c55605dae5e799
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Fri Mar 11 14:16:07 2022 -0600

    Remove schema only backups and db exclusion match
    
    This commit removes the schema only backup functionality, as well as
    simplifying functionality to only backup the explicitly defined
    databases.

diff --git a/pg_backup.config b/pg_backup.config
index 5e97702..c84a5c5 100644
--- a/pg_backup.config
+++ b/pg_backup.config
@@ -18,10 +18,8 @@ USERNAME=
 #change to s3 bucket
 BACKUP_DIR=s3://pg_backups/
  
-# List of strings to match against in database name, separated by space or comma, for which we only
-# wish to keep a backup of the schema, not the data. Any database names which contain any of these
-# values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01")
-SCHEMA_ONLY_LIST=""
+# List of database names to backup, separated by space or comma.
+BACKUP_DB_LIST=""
  
 # Will produce a custom-format backup if set to "yes"
 ENABLE_CUSTOM_BACKUPS=yes
diff --git a/pg_backup.sh b/pg_backup.sh
index 320398a..ea2cecb 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -67,51 +67,14 @@ if ! mkdir -p $FINAL_BACKUP_DIR; then
 fi;
  
  
-###########################
-### SCHEMA-ONLY BACKUPS ###
-###########################
- 
-for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ }
-do
-	SCHEMA_ONLY_CLAUSE="$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'"
-done
- 
-SCHEMA_ONLY_QUERY="select datname from pg_database where false $SCHEMA_ONLY_CLAUSE order by datname;"
- 
-echo -e "\n\nPerforming schema-only backups"
-echo -e "--------------------------------------------\n"
- 
-SCHEMA_ONLY_DB_LIST=`psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$SCHEMA_ONLY_QUERY" postgres`
- 
-echo -e "The following databases were matched for schema-only backup:\n${SCHEMA_ONLY_DB_LIST}\n"
- 
-for DATABASE in $SCHEMA_ONLY_DB_LIST
-do
-	echo "Schema-only backup of $DATABASE"
- 
-	if ! pg_dump -Fp -s -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress; then
-		echo "[!!ERROR!!] Failed to backup database schema of $DATABASE" 1>&2
-	else
-		mv $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz
-	fi
-done
- 
- 
 ###########################
 ###### FULL BACKUPS #######
 ###########################
  
-for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ }
-do
-	EXCLUDE_SCHEMA_ONLY_CLAUSE="$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ '$SCHEMA_ONLY_DB'"
-done
- 
-FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;"
- 
 echo -e "\n\nPerforming full backups"
 echo -e "--------------------------------------------\n"
  
-for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres`
+for DATABASE in ${BACKUP_DB_LIST//,/ }
 do
 	if [[ "$ENABLE_PLAIN_BACKUPS" == "yes" ]]
 	then
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index 8d71796..b6b6105 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -71,51 +71,14 @@ function perform_backups()
 	#fi;
  
  
-	###########################
-	### SCHEMA-ONLY BACKUPS ###
-	###########################
- 
-	for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ }
-	do
-	        SCHEMA_ONLY_CLAUSE="$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'"
-	done
- 
-	SCHEMA_ONLY_QUERY="select datname from pg_database where false $SCHEMA_ONLY_CLAUSE order by datname;"
- 
-	echo -e "\n\nPerforming schema-only backups"
-	echo -e "--------------------------------------------\n"
- 
-	SCHEMA_ONLY_DB_LIST=`psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$SCHEMA_ONLY_QUERY" postgres`
- 
-	echo -e "The following databases were matched for schema-only backup:\n${SCHEMA_ONLY_DB_LIST}\n"
- 
-	for DATABASE in $SCHEMA_ONLY_DB_LIST
-	do
-	        echo "Schema-only backup of $DATABASE"
- 		
-	        if ! pg_dump -Fp -s -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress --no-encrypt; then
-	                echo "[!!ERROR!!] Failed to backup database schema of $DATABASE" 1>&2
-	        else
-	                s3cmd mv $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz
-	        fi
-	done
- 
- 
 	###########################
 	###### FULL BACKUPS #######
 	###########################
  
-	for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ }
-	do
-		EXCLUDE_SCHEMA_ONLY_CLAUSE="$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ '$SCHEMA_ONLY_DB'"
-	done
- 
-	FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;"
- 
 	echo -e "\n\nPerforming full backups"
 	echo -e "--------------------------------------------\n"
  
-	for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres`
+	for DATABASE in ${BACKUP_DB_LIST//,/ }
 	do
 		if [[ "$ENABLE_PLAIN_BACKUPS" == "yes" ]]
 		then
commit fc022712767caa3929b2dff7ba67c0571e695b65
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Fri Mar 11 12:00:01 2022 -0600

    Fix config file path generation

diff --git a/pg_backup.sh b/pg_backup.sh
index a35a3a1..320398a 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -23,7 +23,8 @@ while [[ "$#" -gt "0" ]]; do
 done
  
 if [[ "$#" -eq "0" ]]; then
-        SCRIPTPATH=$(cd ${0%/*} && pwd -P)
+        SCRIPT=`realpath $0`
+        SCRIPTPATH=`dirname $SCRIPT`
         source $SCRIPTPATH/pg_backup.config
 fi;
  
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index 7380a5e..8d71796 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -18,7 +18,8 @@ while [[ "$#" -gt "0" ]]; do
 done
  
 if [[ -z "$CONFIG_FILE_PATH" ]]; then
-        SCRIPTPATH=$(cd ${0%/*} && pwd -P)
+        SCRIPT=`realpath $0`
+        SCRIPTPATH=`dirname $SCRIPT`
         CONFIG_FILE_PATH="${SCRIPTPATH}/pg_backup.config"
 fi
  
commit 1b6052e3854191a48b6c27b730fd717e44e0c2e3
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Fri Mar 11 11:36:50 2022 -0600

    Update comparison operators
    
    The scripts within this project mix POSIX compliant syntax with
    modern bash syntax.  Rather than mixing usage, since the scripts
    are defined specifically to run /bin/bash, this commit updates the
    test and comparison operators to modern bash syntax.
    
    - update "-a" booleen to &&
    
    "-a" is deprecated and its use is discouraged.
    
    - update single bracket "[]" test to double bracket "[[]]"
    
    mixing single and double brackets within a codebase isn't
    consistent or considered best practice.
    
    - update "=" to "=="
    
    mixing single and double equal is inconsistent.
    
    - update variable quoting in comparison
    
    for correct equality comparison and interpolation best practice.

diff --git a/pg_backup.sh b/pg_backup.sh
index 4092cd0..a35a3a1 100644
--- a/pg_backup.sh
+++ b/pg_backup.sh
@@ -4,10 +4,10 @@
 ####### LOAD CONFIG #######
 ###########################
  
-while [ $# -gt 0 ]; do
+while [[ "$#" -gt "0" ]]; do
         case $1 in
                 -c)
-                        if [ -r "$2" ]; then
+                        if [[ -r "$2" ]]; then
                                 source "$2"
                                 shift 2
                         else
@@ -22,7 +22,7 @@ while [ $# -gt 0 ]; do
         esac
 done
  
-if [ $# = 0 ]; then
+if [[ "$#" -eq "0" ]]; then
         SCRIPTPATH=$(cd ${0%/*} && pwd -P)
         source $SCRIPTPATH/pg_backup.config
 fi;
@@ -32,7 +32,7 @@ fi;
 ###########################
  
 # Make sure we're running as the required backup user
-if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then
+if [[ "$BACKUP_USER" != "" && "$(id -un)" != "$BACKUP_USER" ]]; then
 	echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
 	exit 1;
 fi;
@@ -42,11 +42,11 @@ fi;
 ### INITIALISE DEFAULTS ###
 ###########################
  
-if [ ! $HOSTNAME ]; then
+if [[ -z "$HOSTNAME" ]]; then
 	HOSTNAME="localhost"
 fi;
  
-if [ ! $USERNAME ]; then
+if [[ -z "$USERNAME" ]]; then
 	USERNAME="postgres"
 fi;
  
@@ -112,7 +112,7 @@ echo -e "--------------------------------------------\n"
  
 for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres`
 do
-	if [ $ENABLE_PLAIN_BACKUPS = "yes" ]
+	if [[ "$ENABLE_PLAIN_BACKUPS" == "yes" ]]
 	then
 		echo "Plain backup of $DATABASE"
  
@@ -123,7 +123,7 @@ do
 		fi
 	fi
  
-	if [ $ENABLE_CUSTOM_BACKUPS = "yes" ]
+	if [[ $ENABLE_CUSTOM_BACKUPS == "yes" ]]
 	then
 		echo "Custom backup of $DATABASE"
  
diff --git a/pg_backup_rotated.sh b/pg_backup_rotated.sh
index 10e36d6..7380a5e 100644
--- a/pg_backup_rotated.sh
+++ b/pg_backup_rotated.sh
@@ -4,7 +4,7 @@
 ####### LOAD CONFIG #######
 ###########################
  
-while [ $# -gt 0 ]; do
+while [[ "$#" -gt "0" ]]; do
         case $1 in
                 -c)
                         CONFIG_FILE_PATH="$2"
@@ -17,12 +17,12 @@ while [ $# -gt 0 ]; do
         esac
 done
  
-if [ -z $CONFIG_FILE_PATH ] ; then
+if [[ -z "$CONFIG_FILE_PATH" ]]; then
         SCRIPTPATH=$(cd ${0%/*} && pwd -P)
         CONFIG_FILE_PATH="${SCRIPTPATH}/pg_backup.config"
 fi
  
-if [ ! -r ${CONFIG_FILE_PATH} ] ; then
+if [[ ! -r "${CONFIG_FILE_PATH}" ]]; then
         echo "Could not load config file from ${CONFIG_FILE_PATH}" 1>&2
         exit 1
 fi
@@ -34,7 +34,7 @@ source "${CONFIG_FILE_PATH}"
 ###########################
  
 # Make sure we're running as the required backup user
-if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ] ; then
+if [[ "$BACKUP_USER" != "" && "$(id -un)" != "$BACKUP_USER" ]]; then
 	echo "This script must be run as $BACKUP_USER. Exiting." 1>&2
 	exit 1
 fi
@@ -44,11 +44,11 @@ fi
 ### INITIALISE DEFAULTS ###
 ###########################
  
-if [ ! $HOSTNAME ]; then
+if [[ -z "$HOSTNAME" ]]; then
 	HOSTNAME="localhost"
 fi;
  
-if [ ! $USERNAME ]; then
+if [[ -z "$USERNAME" ]]; then
 	USERNAME="postgres"
 fi;
  
@@ -116,7 +116,7 @@ function perform_backups()
  
 	for DATABASE in `psql -h "$HOSTNAME" -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres`
 	do
-		if [ $ENABLE_PLAIN_BACKUPS = "yes" ]
+		if [[ "$ENABLE_PLAIN_BACKUPS" == "yes" ]]
 		then
 			echo "Plain backup of $DATABASE"
  			
@@ -127,7 +127,7 @@ function perform_backups()
 	        	fi
 		fi
  
-		if [ $ENABLE_CUSTOM_BACKUPS = "yes" ]
+		if [[ "$ENABLE_CUSTOM_BACKUPS" == "yes" ]]
 		then
 			echo "Custom backup of $DATABASE"
  			if ! pg_dump -Fc -h "$HOSTNAME" -U "$USERNAME" "$DATABASE" | gzip --stdout | s3cmd --reduced-redundancy put - $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress --no-encrypt; then
@@ -146,7 +146,7 @@ function perform_backups()
  
 DAY_OF_MONTH=`date +%d`
  
-if [ $DAY_OF_MONTH -eq 1 ];
+if [[ "$DAY_OF_MONTH" -eq "1" ]];
 then
 	# Delete all expired monthly directories
 	find $BACKUP_DIR -maxdepth 1 -name "*-monthly" -exec rm -rf '{}' ';'
@@ -161,7 +161,7 @@ fi
 DAY_OF_WEEK=`date +%u` #1-7 (Monday-Sunday)
 EXPIRED_DAYS=`expr $((($WEEKS_TO_KEEP * 7) + 1))`
  
-if [ $DAY_OF_WEEK = $DAY_OF_WEEK_TO_KEEP ];
+if [[ "$DAY_OF_WEEK" -eq "$DAY_OF_WEEK_TO_KEEP" ]];
 then
 	# Delete all expired weekly directories
 	find $BACKUP_DIR -maxdepth 1 -mtime +$EXPIRED_DAYS -name "*-weekly" -exec rm -rf '{}' ';'
-----------------------------------------------------------------------


hooks/post-receive
-- 
postgresql_auto_backup_s3


More information about the Bps-public-commit mailing list