enh: introduce exit_fail and exit_success for shell scripts

This commit is contained in:
Stéphane Lesimple 2022-01-18 12:59:01 +00:00 committed by Stéphane Lesimple
parent d7a898a5fa
commit 744bd5fa0c
2 changed files with 29 additions and 16 deletions

View file

@ -9,11 +9,6 @@ basedir=$(readlink -f "$(dirname "$0")"/../..)
trap "_err 'Unexpected termination!'" EXIT trap "_err 'Unexpected termination!'" EXIT
exit_fail() {
trap - EXIT
exit 1
}
# setting default values # setting default values
LOGFILE="" LOGFILE=""
LOG_FACILITY="local6" LOG_FACILITY="local6"
@ -35,8 +30,7 @@ if [ -d "$BASTION_ETC_DIR/osh-backup-acl-keys.conf.d" ]; then
fi fi
if [ -z "$config_list" ]; then if [ -z "$config_list" ]; then
_err "No configuration loaded, aborting" exit_fail "No configuration loaded, aborting"
exit_fail
fi fi
# load the config files only if they're owned by root:root and mode is o-rwx # load the config files only if they're owned by root:root and mode is o-rwx
@ -45,8 +39,7 @@ for file in $config_list; do
# shellcheck source=etc/bastion/osh-backup-acl-keys.conf.dist # shellcheck source=etc/bastion/osh-backup-acl-keys.conf.dist
. "$file" . "$file"
else else
_err "Configuration file not secure ($file), aborting." exit_fail "Configuration file not secure ($file), aborting."
exit_fail
fi fi
done done
@ -56,13 +49,11 @@ if [ -n "$LOGFILE" ] ; then
fi fi
if [ -z "$DESTDIR" ] ; then if [ -z "$DESTDIR" ] ; then
_err "$0: Missing DESTDIR in configuration, aborting." exit_fail "$0: Missing DESTDIR in configuration, aborting."
exit_fail
fi fi
if ! echo "$DAYSTOKEEP" | grep -Eq '^[0-9]+$' ; then if ! echo "$DAYSTOKEEP" | grep -Eq '^[0-9]+$' ; then
_err "$0: Invalid specified DAYSTOKEEP value ($DAYSTOKEEP), aborting." exit_fail "$0: Invalid specified DAYSTOKEEP value ($DAYSTOKEEP), aborting."
exit_fail
fi fi
_log "Starting backup..." _log "Starting backup..."
@ -123,8 +114,7 @@ do
fi fi
done done
if [ "$try" = "$maxtries" ]; then if [ "$try" = "$maxtries" ]; then
_err "Failed creating tar archive after $maxtries tries!" exit_fail "Failed creating tar archive after $maxtries tries!"
exit_fail
fi fi
encryption_worked=0 encryption_worked=0
@ -165,7 +155,11 @@ if [ -n "$GPGKEYS" ] ; then
if [ "$ret" = 0 ]; then if [ "$ret" = 0 ]; then
encryption_worked=1 encryption_worked=1
shred -u "$file" 2>/dev/null || rm -f "$file" if command -v shred >/dev/null; then
shred -u "$file"
else
rm -f "$file"
fi
else else
_err "Encryption failed" _err "Encryption failed"
fi fi
@ -192,6 +186,7 @@ fi
_log "Cleaning up old backups..." _log "Cleaning up old backups..."
find "$DESTDIR/" -mindepth 1 -maxdepth 1 -type f -name 'backup-????-??-??.tar.gz' -mtime +"$DAYSTOKEEP" -delete find "$DESTDIR/" -mindepth 1 -maxdepth 1 -type f -name 'backup-????-??-??.tar.gz' -mtime +"$DAYSTOKEEP" -delete
find "$DESTDIR/" -mindepth 1 -maxdepth 1 -type f -name 'backup-????-??-??.tar.gz.gpg' -mtime +"$DAYSTOKEEP" -delete find "$DESTDIR/" -mindepth 1 -maxdepth 1 -type f -name 'backup-????-??-??.tar.gz.gpg' -mtime +"$DAYSTOKEEP" -delete
_log "Done" _log "Done"
trap - EXIT trap - EXIT
exit 0 exit 0

View file

@ -376,3 +376,21 @@ _err()
{ {
__log err "ERROR: $*" >&2 __log err "ERROR: $*" >&2
} }
exit_fail()
{
if [ -n "${1:-}" ]; then
_err "$1"
fi
trap - EXIT
exit 1
}
exit_success()
{
if [ -n "${1:-}" ]; then
_log "$1"
fi
trap - EXIT
exit 0
}