@@ -16,6 +16,21 @@ if ! [ -x "$(command -v gpg)" ]; then
1616 exit 1
1717fi
1818
19+ # This function will expect the `PG_EXCLUDE_TABLE_DATA_TABLES` argument to carry a
20+ # comma-separated list of tables. These are then split up into arguments for use
21+ # with `pg_dump`.
22+
23+ function pg_exclude_table_data_args()
24+ {
25+ local table_patterns
26+
27+ IFS=' ,' read -r -a table_patterns <<< " ${PG_EXCLUDE_TABLE_DATA_TABLES}"
28+
29+ for table in " ${table_patterns[@]} " ; do
30+ echo -n " --exclude-table-data ${table} "
31+ done
32+ }
33+
1934ACTION=" $1 "
2035PG_DBNAME=" $2 "
2136
4964
5065# Database config
5166if [ -z " $PG_HOSTNAME " ]; then
52- echo " Please set TWOBUCKET !"
67+ echo " Please set PG_HOSTNAME !"
5368 exit 1
5469fi
5570if [ -z " $PG_PORT " ]; then
@@ -85,16 +100,16 @@ case $ACTION in
85100 echo " Backup ${PG_DBNAME} to ${REMOTE} /$SNAPSHOT_NAME ..."
86101 cd /tmp
87102 export XZ_DEFAULTS=" -2"
88- pg_dump -C -h $ PG_HOSTNAME -p $ PG_PORT -d $ PG_DBNAME -U $ PG_USERNAME | xz > /tmp/$SNAPSHOT_NAME
103+ pg_dump -C -h " ${ PG_HOSTNAME} " -p " ${ PG_PORT} " -d " ${ PG_DBNAME} " -U " ${ PG_USERNAME} " $( pg_exclude_table_data_args ) | xz > " /tmp/${ SNAPSHOT_NAME} "
89104 if [[ $? -ne 0 ]]; then
90105 rm -f ~ /.pgpass
91- echo " Error: Problem encounted performing snapshot!"
106+ echo " Error: Problem encountered performing snapshot!"
92107 exit 1
93108 fi
94109 rm -f ~ /.pgpass
95110 cat $TWOSECRET_PATH | gpg --batch --yes --passphrase-fd 0 --symmetric --cipher-algo TWOFISH /tmp/$SNAPSHOT_NAME
96111 if [[ $? -ne 0 ]]; then
97- echo " Error: Problem encounted performing encryption! (gpg)"
112+ echo " Error: Problem encountered performing encryption! (gpg)"
98113 rm /tmp/$SNAPSHOT_NAME
99114 exit 1
100115 fi
@@ -118,21 +133,21 @@ case $ACTION in
118133 rclone copy $REMOTE /$LATEST /tmp/
119134 if [[ $? -ne 0 ]]; then
120135 rm -f ~ /.pgpass
121- echo " Error: Problem encounted getting snapshot from s3! (mc)"
136+ echo " Error: Problem encountered getting snapshot from s3! (mc)"
122137 rm /tmp/$LATEST
123138 exit 1
124139 fi
125140 echo $TWOSECRET | gpg --batch --yes --passphrase-fd 0 -o /tmp/$PG_DBNAME -restore.sql.xz -d /tmp/$LATEST
126141 if [[ $? -ne 0 ]]; then
127- echo " Error: Problem encounted decrypting snapshot! (gpg)"
142+ echo " Error: Problem encountered decrypting snapshot! (gpg)"
128143 rm -f ~ /.pgpass
129144 rm /tmp/$LATEST
130145 exit 1
131146 fi
132147 rm /tmp/$LATEST
133148 xz -cd /tmp/$PG_DBNAME -restore.sql.xz | psql -h $PG_HOSTNAME -U $PG_USERNAME
134149 if [[ $? -ne 0 ]]; then
135- echo " Error: Problem encounted extracting snapshot! (sql)"
150+ echo " Error: Problem encountered extracting snapshot! (sql)"
136151 rm -f ~ /.pgpass
137152 rm /tmp/$PG_DBNAME -restore.sql.xz
138153 exit 1
0 commit comments