refactor(RESTORE): create and use restore function

- single function to handle non-SQL restore operations
- replace previous mail and crypt restore with new function
This commit is contained in:
Asif Bacchus 2021-02-09 05:41:56 -07:00
parent cc30581382
commit 363c970f2f

View File

@ -47,6 +47,27 @@ exitError() {
exit "$1" exit "$1"
} }
doRestore() {
sourceFiles=$(find "${backupLocation}" -iname "${1}" -type d)
if [ -n "$sourceFiles" ]; then
if [ "$verbose" -eq 1 ]; then
if (! (cd "$sourceFiles/_data" && tar -cf - .) | (cd "${2}" && tar xvf -) > "$logfile" ); then
return 1
else
return 0
fi
else
if (! (cd "$sourceFiles/_data" && tar -cf - .) | (cd "${3}" && tar xvf -) ); then
return 1
else
return 0
fi
fi
else
return 2
fi
}
scriptHelp() { scriptHelp() {
textNewline textNewline
printf "%sUsage: %s [parameters]%s\n\n" "$bold" "$scriptName" "$norm" printf "%sUsage: %s [parameters]%s\n\n" "$bold" "$scriptName" "$norm"
@ -356,7 +377,7 @@ if [ "$( docker ps --filter "name=${COMPOSE_PROJECT_NAME}" -q | wc -l )" -gt 0 ]
exitError 20 exitError 20
fi fi
### restore email and encryption key ### restore mail and encryption key
if [ "$restoreMail" -eq 1 ]; then if [ "$restoreMail" -eq 1 ]; then
if [ "$verbose" -eq 1 ]; then if [ "$verbose" -eq 1 ]; then
writeLog 'info' "Restoring email" writeLog 'info' "Restoring email"
@ -364,38 +385,61 @@ if [ "$restoreMail" -eq 1 ]; then
writeLog 'task' "Restoring email" writeLog 'task' "Restoring email"
fi fi
# mail restore pre-requisites # restore email messages
mailBackup=$(find "${backupLocation}" -iname "${COMPOSE_PROJECT_LOCATION}_vmail-vol-1" -type d) doRestore "${COMPOSE_PROJECT_NAME}_vmail-vol-1" "$dockerVolumeMail"; ec="$?"
cryptBackup=$(find "${backupLocation}" -iname "${COMPOSE_PROJECT_LOCATION}_crypt-vol-1" -type d) case "$ec" in
if [ -n "$mailBackup" ] && [ -n "$cryptBackup" ]; then 0)
if [ "$verbose" -eq 1 ]; then if [ "$verbose" -eq 1 ]; then
if ! (cd "$mailBackup/_data" && tar cf - .) | (cd "$dockerVolumeMail" && tar xvf -) > "$logfile"; then writeLog 'success' "Email messages restored"
writeLog 'error' '52' "There was an error restoring one or more email messages."
errorCount=$((errorCount+1))
fi
if ! (cd "$cryptBackup/_data" && tar cf - .) | (cd "$dockerVolumeCrypt" && tar xvf -) > "$logfile"; then
writeLog 'error' '53' "There was an error restoring mail encryption keys! Restored mail may *not* be readable!"
errorCount=$((errorCount+1))
fi
else else
if ! (cd "$mailBackup/_data" && tar cf - .) | (cd "$dockerVolumeMail" && tar xvf -); then writeLog 'done'
writeLog 'error' '52' "There was an error restoring one or more email messages."
errorCount=$((errorCount+1))
fi fi
if ! (cd "$cryptBackup/_data" && tar cf - .) | (cd "$dockerVolumeCrypt" && tar xvf -); then ;;
writeLog 'error' '53' "There was an error restoring mail encryption keys! Restored mail may *not* be readable!" 1)
errorCount=$((errorCount+1))
fi
fi
else
if [ "$verbose" -eq 1 ]; then if [ "$verbose" -eq 1 ]; then
writeLog 'error' '51' "Cannot locate email message and/or encryption key backups!" writeLog 'error' '52' "There was an error restoring one or more email messages."
else else
writeLog 'done' 'error' writeLog 'done' 'error'
writeLog 'error' '51' "Cannot locate email message and/or encryption key backups!" writeLog 'error' '52' "There was an error restoring one or more email messages."
fi fi
errorCount=$((errorCount+1)) ;;
2)
if [ "$verbose" -eq 1 ]; then
writeLog 'error' '51' "Cannot locate email message backups!"
else
writeLog 'done' 'error'
writeLog 'error' '51' "Cannot locate email message backups!"
fi fi
;;
esac
# restore encryption key
doRestore "${COMPOSE_PROJECT_NAME}_crypt-vol-1" "$dockerVolumeCrypt"; ec="$?"
case "$ec" in
0)
if [ "$verbose" -eq 1 ]; then
writeLog 'success' "Encryption key restored"
else
writeLog 'done'
fi
;;
1)
if [ "$verbose" -eq 1 ]; then
writeLog 'error' '52' "There was an error restoring the encryption key! Any restored messages are likely *not* readable!"
else
writeLog 'done' 'error'
writeLog 'error' '52' "There was an error restoring the encryption key! Any restored messages are likely *not* readable!"
fi
;;
2)
if [ "$verbose" -eq 1 ]; then
writeLog 'error' '51' "Cannot locate encryption key backup!"
else
writeLog 'done' 'error'
writeLog 'error' '51' "Cannot locate encryption key backup!"
fi
;;
esac
fi fi
#TODO: copy backups to correct docker volumes #TODO: copy backups to correct docker volumes
@ -429,9 +473,8 @@ fi
# 2x: Docker/Docker-Compose errors # 2x: Docker/Docker-Compose errors
# 20: cannot bring docker container(s) down successfully # 20: cannot bring docker container(s) down successfully
# 5x: File restore errors # 5x: File restore errors
# 51: cannot locate email/crypt files in backup directory # 51: cannot locate source files in backup directory
# 52: error restoring one or more mail messages # 52: error restoring one or more files
# 53: error restoring encryption keys, mail likely unreadable
# 97: script completed with 1 or more warnings # 97: script completed with 1 or more warnings
# 98: script completed with 1 or more non-terminating errors # 98: script completed with 1 or more non-terminating errors
# 99: TERM signal trapped # 99: TERM signal trapped