Skip to content

Commit a048d20

Browse files
committed
Add support for ignoring SQL errors during postgres database restore.
- resolves #56 - Adds the `-I` option as discussed to control the value of `ON_ERROR_STOP` in the postgres onRestoreDatabase implementation. - An `ignoreErrors` function was added to allow other implementations and scripts to test for the "ignore errors" option.
1 parent 87e1539 commit a048d20

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

docker/backup.config.utils

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,14 @@ function validateOperation(){
488488
return ${_rtnCd}
489489
)
490490
}
491-
# ======================================================================================
491+
492+
function ignoreErrors(){
493+
(
494+
if [ ! -z "${IGNORE_ERRORS}" ]; then
495+
return 0
496+
else
497+
return 1
498+
fi
499+
)
500+
}
501+
# ======================================================================================

docker/backup.postgres.plugin

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ function onRestoreDatabase(){
6161
echo -e "Restoring '${_fileName}' to '${_hostname}${_port:+:${_port}}${_database:+/${_database}}' ...\n" >&2
6262

6363
export PGPASSWORD=${_adminPassword}
64+
_stopOnErrors="-v ON_ERROR_STOP=1"
65+
if ignoreErrors; then
66+
_stopOnErrors="-v ON_ERROR_STOP=0"
67+
fi
6468
_rtnCd=0
6569

6670
# Drop
@@ -86,7 +90,7 @@ function onRestoreDatabase(){
8690

8791
# Restore
8892
if (( ${_rtnCd} == 0 )); then
89-
gunzip -c "${_fileName}" | psql -v ON_ERROR_STOP=1 -x -h "${_hostname}" ${_portArg} -d "${_database}"
93+
gunzip -c "${_fileName}" | psql ${_stopOnErrors} -x -h "${_hostname}" ${_portArg} -d "${_database}"
9094
# Get the status code from psql specifically. ${?} would only provide the status of the last command, psql in this case.
9195
_rtnCd=${PIPESTATUS[1]}
9296
fi

docker/backup.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if [[ ${?} != 0 ]]; then
3131
. ./backup.${CONTAINER_TYPE}.plugin > /dev/null 2>&1
3232
fi
3333

34-
while getopts nclr:v:f:1spha: FLAG; do
34+
while getopts nclr:v:f:1spha:I FLAG; do
3535
case $FLAG in
3636
n)
3737
# Allow null database plugin ...
@@ -72,6 +72,9 @@ while getopts nclr:v:f:1spha: FLAG; do
7272
a)
7373
export _adminPassword=${OPTARG}
7474
;;
75+
I)
76+
export IGNORE_ERRORS=1
77+
;;
7578
h)
7679
usage
7780
;;

docker/backup.usage

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ function usage () {
4646
This can be used with the '-f' option, see below, to prune specific backups or sets of backups.
4747
Use caution when using the '-f' option.
4848
49+
-I ignore errors
50+
This flag can be used with the Restore Options, when restoring a postgres database, to continue the
51+
database restoration process when errors are encountered. By default the postgres restoration script
52+
stops on the first error.
53+
4954
Verify Options:
5055
================
5156
The verify process performs the following basic operations:

0 commit comments

Comments
 (0)