Skip to content

Commit

Permalink
Merge pull request #57 from WadeBarnes/feature/ignore_errors
Browse files Browse the repository at this point in the history
Add support for ignoring SQL errors during postgres database restore.
  • Loading branch information
WadeBarnes authored Oct 3, 2023
2 parents 87e1539 + a048d20 commit 7e46504
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
12 changes: 11 additions & 1 deletion docker/backup.config.utils
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,14 @@ function validateOperation(){
return ${_rtnCd}
)
}
# ======================================================================================

function ignoreErrors(){
(
if [ ! -z "${IGNORE_ERRORS}" ]; then
return 0
else
return 1
fi
)
}
# ======================================================================================
6 changes: 5 additions & 1 deletion docker/backup.postgres.plugin
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ function onRestoreDatabase(){
echo -e "Restoring '${_fileName}' to '${_hostname}${_port:+:${_port}}${_database:+/${_database}}' ...\n" >&2

export PGPASSWORD=${_adminPassword}
_stopOnErrors="-v ON_ERROR_STOP=1"
if ignoreErrors; then
_stopOnErrors="-v ON_ERROR_STOP=0"
fi
_rtnCd=0

# Drop
Expand All @@ -86,7 +90,7 @@ function onRestoreDatabase(){

# Restore
if (( ${_rtnCd} == 0 )); then
gunzip -c "${_fileName}" | psql -v ON_ERROR_STOP=1 -x -h "${_hostname}" ${_portArg} -d "${_database}"
gunzip -c "${_fileName}" | psql ${_stopOnErrors} -x -h "${_hostname}" ${_portArg} -d "${_database}"
# Get the status code from psql specifically. ${?} would only provide the status of the last command, psql in this case.
_rtnCd=${PIPESTATUS[1]}
fi
Expand Down
5 changes: 4 additions & 1 deletion docker/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if [[ ${?} != 0 ]]; then
. ./backup.${CONTAINER_TYPE}.plugin > /dev/null 2>&1
fi

while getopts nclr:v:f:1spha: FLAG; do
while getopts nclr:v:f:1spha:I FLAG; do
case $FLAG in
n)
# Allow null database plugin ...
Expand Down Expand Up @@ -72,6 +72,9 @@ while getopts nclr:v:f:1spha: FLAG; do
a)
export _adminPassword=${OPTARG}
;;
I)
export IGNORE_ERRORS=1
;;
h)
usage
;;
Expand Down
5 changes: 5 additions & 0 deletions docker/backup.usage
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ function usage () {
This can be used with the '-f' option, see below, to prune specific backups or sets of backups.
Use caution when using the '-f' option.
-I ignore errors
This flag can be used with the Restore Options, when restoring a postgres database, to continue the
database restoration process when errors are encountered. By default the postgres restoration script
stops on the first error.
Verify Options:
================
The verify process performs the following basic operations:
Expand Down

0 comments on commit 7e46504

Please sign in to comment.