[Bps-public-commit] postgresql_auto_backup_s3 branch update-for-hostedrt-backups updated. e729a3720b8ee34ae5c33f08190d1b653a19f431
BPS Git Server
git at git.bestpractical.com
Tue Mar 15 22:42:16 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 updated
via e729a3720b8ee34ae5c33f08190d1b653a19f431 (commit)
via 3b4901ed5e51e57e573763cd3ae2db9c37bb9659 (commit)
via 68bceae25051d4e051fd2cc036ad7f56a1c5246f (commit)
from 8125c0747e22287f342c34eb7d67def273d3b80f (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
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"
-----------------------------------------------------------------------
Summary of changes:
pg_backup.sh | 74 ++++++++++++++++++++----------------------
pg_backup_rotated.sh | 91 +++++++++++++++++++++++++++++-----------------------
2 files changed, 85 insertions(+), 80 deletions(-)
hooks/post-receive
--
postgresql_auto_backup_s3
More information about the Bps-public-commit
mailing list