Skip to content
Snippets Groups Projects
Commit b2ad27c6 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch 'add-hooks' into 'master'

Add hooks

See merge request !80
parents b980df48 db3cbe5c
Branches
No related tags found
1 merge request!80Add hooks
Showing with 72 additions and 45 deletions
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
# 2017-01-23 ah,ds v1.1 added j_getLastBackupAge # 2017-01-23 ah,ds v1.1 added j_getLastBackupAge
# 2017-02-16 ah,ds v1.2 added storage helper function # 2017-02-16 ah,ds v1.2 added storage helper function
# 2018-02-13 ah,ds v1.3 detect samba shares based on a flag # 2018-02-13 ah,ds v1.3 detect samba shares based on a flag
# 2022-10-07 ah v1.4 unescape regex with space to prevent "grep: warning: stray \ before white space"
# ================================================================================ # ================================================================================
...@@ -130,7 +131,7 @@ function j_getDirs2Backup(){ ...@@ -130,7 +131,7 @@ function j_getDirs2Backup(){
# param string path # param string path
# ------------------------------------------------------------ # ------------------------------------------------------------
function j_getSetnameOfPath(){ function j_getSetnameOfPath(){
grep "^set.*dir\ =\ $*$" "${DIRFILE}" | cut -f 1 -d "=" | sed "s#\-\-dir\ ##g" grep "^set.*dir = $*$" "${DIRFILE}" | cut -f 1 -d "=" | sed "s#\-\-dir ##g"
} }
# ------------------------------------------------------------ # ------------------------------------------------------------
...@@ -172,13 +173,49 @@ function _j_getvar(){ ...@@ -172,13 +173,49 @@ function _j_getvar(){
>&2 echo "ERROR: cannot read file: ${1}. Abort." >&2 echo "ERROR: cannot read file: ${1}. Abort."
exit 100 exit 100
fi fi
grep "^${2}\ =\ " < "${1}"| cut -f 3- -d " " grep "^${2} = " < "${1}"| cut -f 3- -d " "
} }
# ------------------------------------------------------------ # ------------------------------------------------------------
# read local jobdescription and set as variables # read local jobdescription and set as variables
# ------------------------------------------------------------ # ------------------------------------------------------------
# ------------------------------------------------------------
# execute hook skripts in a given directory in alphabetic order
# param string name of hook directory
# param string optional: integer of existcode or "" for non-on-result hook
# ------------------------------------------------------------
function _j_runHooks(){
local _hookbase="$1"
local _exitcode="$2"
local _hookdir="$( dirname $0 )/hooks/$_hookbase"
if [ -z "$_exitcode" ]; then
_hookdir="$_hookdir/always"
elif [ "$_exitcode" = "0" ]; then
_hookdir="$_hookdir/on-ok"
else
_hookdir="$_hookdir/on-error"
fi
for hookscript in $( ls -1a "$_hookdir" | grep -v "^\." | sort )
do
if [ -x "$_hookdir/$hookscript" ]; then
h3 "HOOK: start $_hookdir/$hookscript ..."
$_hookdir/$hookscript
else
h3 "HOOK: SKIP $_hookdir/$hookscript (not executable) ..."
fi
done
# if an exitcode was given as param then run hooks without exitcode
# (in subdir "always")
if [ -n "$_exitcode" ]; then
_j_runHooks "$_hookbase"
fi
echo
}
# ------------------------------------------------------------ # ------------------------------------------------------------
# parse day of week and day of month and echo 0 or 1 # parse day of week and day of month and echo 0 or 1
# #
...@@ -299,36 +336,6 @@ function _j_setLogfile(){ ...@@ -299,36 +336,6 @@ function _j_setLogfile(){
export JOB_LOGFILE export JOB_LOGFILE
} }
# ------------------------------------------------------------
# date helper for job entries in start-time-inc/ start-time-full
# * get first the first entry or - if many - the latest entry
# * if empty: take value from start-time
# param string one of start-time-inc|start-time-full
# ------------------------------------------------------------
# function _j_fetchLatestStarttime(){
# sLatest=
# sStart=`_j_getvar ${JOBFILE} "$1" | sed "s#[\ \:\-]##g"`
# for sTime in `echo $sStart | sed "s#,# #g"`
# do
# if [ -z $sLatest ]; then
# sLatest=$sTime
# fi
# if [ $sTime -le `date +%H%M` ]; then
# sLatest=$sTime
# fi
# done
# if [ -z $sLatest ]; then
# $sLatest=`_j_getvar ${JOBFILE} "start-time" | sed "s#[\ \:\-]##g"`
# fi
# if [ -z $sLatest ]; then
# color error
# echo ERROR: missing start time info for $1
# color reset
# exit 1
# fi
# echo $sLatest
# }
# ------------------------------------------------------------ # ------------------------------------------------------------
# check if a binary exists - and abort if not # check if a binary exists - and abort if not
# param string name of file # param string name of file
......
...@@ -204,7 +204,7 @@ ...@@ -204,7 +204,7 @@
if [ -d "${BACKUP_TARGETDIR}" ]; then if [ -d "${BACKUP_TARGETDIR}" ]; then
cd "${BACKUP_TARGETDIR}" cd "${BACKUP_TARGETDIR}"
if [ -z $1 ]; then if [ -z $1 ]; then
find -type f | sed "s#__[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9].*##g" | grep -v ".meta" | sort -ud| sed "s#^\./##g" find -type f | sed "s#__[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9].*##g" | grep -v "\.meta" | sort -ud| sed "s#^\./##g"
else else
ls -ltr "$*__"* | sed "s#^\./##g" ls -ltr "$*__"* | sed "s#^\./##g"
fi fi
...@@ -354,7 +354,9 @@ ...@@ -354,7 +354,9 @@
# ----- start service specific script # ----- start service specific script
h2 "START SCRIPT FOR ${SERVICENAME} - $BACKUP_SCRIPT" h2 "START SCRIPT FOR ${SERVICENAME} - $BACKUP_SCRIPT"
_j_runHooks "12-before-db-service"
. $BACKUP_SCRIPT $mode . $BACKUP_SCRIPT $mode
_j_runHooks "18-after-db-service" "$rc"
# ----- post jobs: cleanup # ----- post jobs: cleanup
cleanup_backup_target cleanup_backup_target
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
# 2017-03-27 ..... v1.0 restore # 2017-03-27 ..... v1.0 restore
# 2022-01-20 v1.1 fixes with shellcheck # 2022-01-20 v1.1 fixes with shellcheck
# 2022-03-17 v1.2 WIP: add lines with prefix __DB__ # 2022-03-17 v1.2 WIP: add lines with prefix __DB__
# 2022-10-07 ah v1.3 unescape regex with space to prevent "grep: warning: stray \ before white space"
# ================================================================================ # ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then if [ -z "$BACKUP_TARGETDIR" ]; then
...@@ -108,7 +109,7 @@ function doBackup(){ ...@@ -108,7 +109,7 @@ function doBackup(){
loadInstance "$COUCHDB_INSTANCE" loadInstance "$COUCHDB_INSTANCE"
echo "--- instance: $COUCHDB_INSTANCE" echo "--- instance: $COUCHDB_INSTANCE"
if curl --head -X GET "$COUCHDB_URL" 2>/dev/null | grep "^HTTP.*\ 200\ "; then if curl --head -X GET "$COUCHDB_URL" 2>/dev/null | grep "^HTTP.* 200 "; then
_doBackupOfSingleInstance _doBackupOfSingleInstance
...@@ -186,7 +187,7 @@ function restoreByFile(){ ...@@ -186,7 +187,7 @@ function restoreByFile(){
loadInstance "$COUCHDB_INSTANCE" loadInstance "$COUCHDB_INSTANCE"
echo "connect $couchdbhost on port $couchdbport with user $couchdbuser" echo "connect $couchdbhost on port $couchdbport with user $couchdbuser"
if ! curl --head -X GET "$COUCHDB_URL" 2>/dev/null | grep "^HTTP.*\ 200\ " >/dev/null; then if ! curl --head -X GET "$COUCHDB_URL" 2>/dev/null | grep "^HTTP.* 200 " >/dev/null; then
color error color error
echo ERROR: couch DB instance is not available echo ERROR: couch DB instance is not available
curl -X GET "$COUCHDB_URL" curl -X GET "$COUCHDB_URL"
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
# 2022-04-07 v1.5 check archive file, not only seq file # 2022-04-07 v1.5 check archive file, not only seq file
# 2022-04-14 v1.6 backup security infos (no restore yet) # 2022-04-14 v1.6 backup security infos (no restore yet)
# 2022-04-21 v1.7 restore security infos # 2022-04-21 v1.7 restore security infos
# 2022-10-07 ah v1.8 unescape regex with space to prevent "grep: warning: stray \ before white space"
# ================================================================================ # ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then if [ -z "$BACKUP_TARGETDIR" ]; then
...@@ -123,7 +124,7 @@ function doBackup(){ ...@@ -123,7 +124,7 @@ function doBackup(){
loadInstance "$COUCHDB_INSTANCE" loadInstance "$COUCHDB_INSTANCE"
echo "--- instance: $COUCHDB_INSTANCE" echo "--- instance: $COUCHDB_INSTANCE"
if curl --head -X GET "$COUCH_URL" 2>/dev/null | grep "^HTTP.*\ 200\ "; then if curl --head -X GET "$COUCH_URL" 2>/dev/null | grep "^HTTP.* 200 "; then
echo OK, connected. echo OK, connected.
sleep 2 sleep 2
_doBackupOfSingleInstance _doBackupOfSingleInstance
...@@ -303,7 +304,7 @@ function restoreByFile(){ ...@@ -303,7 +304,7 @@ function restoreByFile(){
if [ $bFastMode -eq 0 ]; then if [ $bFastMode -eq 0 ]; then
echo connect $couchdbhost on port $couchdbport with user $couchdbuser echo connect $couchdbhost on port $couchdbport with user $couchdbuser
curl --head -X GET $COUCH_URL 2>/dev/null | grep "^HTTP.*\ 200\ " >/dev/null curl --head -X GET $COUCH_URL 2>/dev/null | grep "^HTTP.* 200 " >/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
color error color error
echo ERROR: couch DB instance is not available echo ERROR: couch DB instance is not available
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
# 2021-12-13 ah v1.1 detect config path # 2021-12-13 ah v1.1 detect config path
# 2021-12-14 ah v1.2 detect sbin path to execute slapcat without path # 2021-12-14 ah v1.2 detect sbin path to execute slapcat without path
# 2022-03-17 v1.3 WIP: add lines with prefix __DB__ # 2022-03-17 v1.3 WIP: add lines with prefix __DB__
# 2022-10-07 ah v1.4 unescape regex with space to prevent "grep: warning: stray \ before white space"
# ================================================================================ # ================================================================================
...@@ -76,7 +77,7 @@ function doLdapBackup(){ ...@@ -76,7 +77,7 @@ function doLdapBackup(){
for cfgname in $(ldapsearch -Y EXTERNAL -H ldapi:/// -s base -b '' -LLL configContext | grep "configContext" | cut -f 2 -d ":") for cfgname in $(ldapsearch -Y EXTERNAL -H ldapi:/// -s base -b '' -LLL configContext | grep "configContext" | cut -f 2 -d ":")
do do
echo DN $cfgname echo DN $cfgname
cfg2=$(echo $cfgname | sed "s#[\ =,]#_#g") cfg2=$(echo $cfgname | sed "s#[ =,]#_#g")
outfile=$(hostname)_ldap_olc_config__$(get_outfile ${cfg2}).ldif outfile=$(hostname)_ldap_olc_config__$(get_outfile ${cfg2}).ldif
dump_ldap "$cfgname" "$BACKUP_TARGETDIR/$outfile" dump_ldap "$cfgname" "$BACKUP_TARGETDIR/$outfile"
...@@ -88,7 +89,7 @@ function doLdapBackup(){ ...@@ -88,7 +89,7 @@ function doLdapBackup(){
for cfgname in $(ldapsearch -Y EXTERNAL -H ldapi:/// -s base -b "" -LLL "namingContexts" | grep "namingContexts" | cut -f 2 -d ":") for cfgname in $(ldapsearch -Y EXTERNAL -H ldapi:/// -s base -b "" -LLL "namingContexts" | grep "namingContexts" | cut -f 2 -d ":")
do do
echo DN $cfgname echo DN $cfgname
cfg2=`echo $cfgname | sed "s#[\ =,]#_#g"` cfg2=`echo $cfgname | sed "s#[ =,]#_#g"`
outfile=$(hostname)_ldap_data__$(get_outfile ${cfg2}).ldif outfile=$(hostname)_ldap_data__$(get_outfile ${cfg2}).ldif
dump_ldap "$cfgname" "$BACKUP_TARGETDIR/$outfile" dump_ldap "$cfgname" "$BACKUP_TARGETDIR/$outfile"
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
# -------------------------------------------------------------------------------- # --------------------------------------------------------------------------------
# ah - Axel Hahn <axel.hahn@iml.unibe.ch> # ah - Axel Hahn <axel.hahn@iml.unibe.ch>
# 2021-05-19 ah v0.0 INIT ... WIP # 2021-05-19 ah v0.0 INIT ... WIP
# 2022-10-07 ah v1.1 unescape regex with space to prevent "grep: warning: stray \ before white space"
# ================================================================================ # ================================================================================
...@@ -211,7 +212,7 @@ ...@@ -211,7 +212,7 @@
local _date= local _date=
echo "--- Existing snapshots:" echo "--- Existing snapshots:"
t_restoreDoShowVolumes \ t_restoreDoShowVolumes \
| grep -E "(Full|Incremental).*[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\ " \ | grep -E "(Full|Incremental).*[0-9][0-9]:[0-9][0-9]:[0-9][0-9] " \
| sort -u > $tmpoutVolumes | sort -u > $tmpoutVolumes
if [ `cat $tmpoutVolumes | wc -l` -eq 0 ]; then if [ `cat $tmpoutVolumes | wc -l` -eq 0 ]; then
color error color error
......
...@@ -213,6 +213,8 @@ ...@@ -213,6 +213,8 @@
color reset color reset
t_rcCheckPrune $_myrc t_rcCheckPrune $_myrc
_j_runHooks "26-after-prune" "$_myrc"
echo echo
} }
# verify backup data # verify backup data
...@@ -231,6 +233,7 @@ ...@@ -231,6 +233,7 @@
color reset color reset
t_rcCheckVerify $_myrc t_rcCheckVerify $_myrc
_j_runHooks "28-after-verify" "$_myrc"
echo echo
} }
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
# 2022-02-10 ah v2.2 update logging (removing tee) # 2022-02-10 ah v2.2 update logging (removing tee)
# 2022-10-01 ah v2.3 customize prune and verify action # 2022-10-01 ah v2.3 customize prune and verify action
# 2022-10-04 ah v2.4 prune and verify are non directory based # 2022-10-04 ah v2.4 prune and verify are non directory based
# 2022-10-07 ah v2.5 unescape regex with space to prevent "grep: warning: stray \ before white space"
# ================================================================================ # ================================================================================
...@@ -65,8 +66,7 @@ ...@@ -65,8 +66,7 @@
# check # check
if [ -z "$STORAGE_BIN" ]; then if [ -z "$STORAGE_BIN" ]; then
# STORAGE_BIN=restic STORAGE_BIN=restic
STORAGE_BIN=duplicity
fi fi
CFGPREFIX=${STORAGE_BIN}_ CFGPREFIX=${STORAGE_BIN}_
...@@ -219,13 +219,14 @@ function setAction(){ ...@@ -219,13 +219,14 @@ function setAction(){
fi fi
. `dirname $0`/plugins/transfer/$STORAGE_BIN.sh || exit 1 . `dirname $0`/plugins/transfer/$STORAGE_BIN.sh || exit 1
test -z "$STORAGE_REGISTER" || . `dirname $0`/plugins/register/$STORAGE_REGISTER.sh || exit 1
# -------------------------------------------------------------------------------- # --------------------------------------------------------------------------------
# ----- Check requirements # ----- Check requirements
t_checkRequirements || exit 1 t_checkRequirements || exit 1
test -z "$STORAGE_REGISTER" || . `dirname $0`/plugins/register/$STORAGE_REGISTER.sh || exit 1
echo Check locking of a running transfer echo Check locking of a running transfer
if [ -f "${lockfile}" ]; then if [ -f "${lockfile}" ]; then
color error color error
...@@ -268,6 +269,8 @@ function setAction(){ ...@@ -268,6 +269,8 @@ function setAction(){
exit 2 exit 2
fi fi
_j_runHooks "20-before-transfer"
# -------------------------------------------------------------------------------- # --------------------------------------------------------------------------------
# ----- BACKUP VARS # ----- BACKUP VARS
...@@ -357,11 +360,11 @@ function setAction(){ ...@@ -357,11 +360,11 @@ function setAction(){
if [ ! -z $backupid ]; then if [ ! -z $backupid ]; then
for sItem in `_j_getvar ${DIRFILE} "${backupid}\-\-include" | sed "s#\ #${sSpaceReplace}#g"` for sItem in `_j_getvar ${DIRFILE} "${backupid}\-\-include" | sed "s# #${sSpaceReplace}#g"`
do do
ARGS_BACKUP="${ARGS_BACKUP} $( t_getParamInlude $sItem)" ARGS_BACKUP="${ARGS_BACKUP} $( t_getParamInlude $sItem)"
done done
for sItem in `_j_getvar ${DIRFILE} "${backupid}\-\-exclude" | sed "s#\ #${sSpaceReplace}#g"` for sItem in `_j_getvar ${DIRFILE} "${backupid}\-\-exclude" | sed "s# #${sSpaceReplace}#g"`
do do
ARGS_BACKUP="${ARGS_BACKUP} $( t_getParamExlude $sItem)" ARGS_BACKUP="${ARGS_BACKUP} $( t_getParamExlude $sItem)"
done done
...@@ -379,6 +382,9 @@ function setAction(){ ...@@ -379,6 +382,9 @@ function setAction(){
if [ $doBackup -eq 0 ]; then if [ $doBackup -eq 0 ]; then
echo "SKIP backup" echo "SKIP backup"
else else
_j_runHooks "22-before-folder-transfer"
sCmd="$( t_backupDirGetCmdBackup )" sCmd="$( t_backupDirGetCmdBackup )"
echo "what: ${BACKUP_DIR}" echo "what: ${BACKUP_DIR}"
echo "target: ${sTarget}" | sed 's#:[^:]*@#:**********@#' echo "target: ${sTarget}" | sed 's#:[^:]*@#:**********@#'
...@@ -391,6 +397,7 @@ function setAction(){ ...@@ -391,6 +397,7 @@ function setAction(){
echo echo
t_rcCheckBackup $myrc "${BACKUP_DIR}" t_rcCheckBackup $myrc "${BACKUP_DIR}"
_j_runHooks "24-after-folder-transfer" "$myrc"
fi fi
echo echo
...@@ -410,6 +417,8 @@ function setAction(){ ...@@ -410,6 +417,8 @@ function setAction(){
fi fi
echo echo
done done
else
echo "SKIP backup of dirs"
fi fi
# --- prune # --- prune
...@@ -469,6 +478,8 @@ function setAction(){ ...@@ -469,6 +478,8 @@ function setAction(){
echo Backup FAILED :-/ echo Backup FAILED :-/
fi fi
color reset color reset
_j_runHooks "30-post-backup" "$rc"
echo echo
typeset -i TIMER_TRANSFER=`date +%s`-$TIMER_TRANSFER_START typeset -i TIMER_TRANSFER=`date +%s`-$TIMER_TRANSFER_START
echo `date` $ACTION DONE in $TIMER_TRANSFER sec echo `date` $ACTION DONE in $TIMER_TRANSFER sec
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment