Reorganize logging

This commit is contained in:
Jérémy Lecour 2022-01-26 11:42:52 +01:00 committed by Jérémy Lecour
parent 06f6c54d8c
commit 82e111adc8

View file

@ -38,23 +38,26 @@ LOCAL_BACKUP_DIR="/home/backup"
# You can set "linux" or "bsd" manually or let it choose automatically # You can set "linux" or "bsd" manually or let it choose automatically
SYSTEM=$(uname | tr '[:upper:]' '[:lower:]') SYSTEM=$(uname | tr '[:upper:]' '[:lower:]')
# Store pid and logs in a file named after this program's name # Store pid in a file named after this program's name
PROGNAME=$(basename "$0") PROGNAME=$(basename "$0")
PIDFILE="/var/run/${PROGNAME}.pid" PIDFILE="/var/run/${PROGNAME}.pid"
LOGFILE="/var/log/evobackup/${PROGNAME}.log"
# Customize the log path if you have multiple scripts and with separate logs
LOGFILE="/var/log/evobackup.log"
# Enable/Disable tasks # Enable/Disable tasks
LOCAL_TASKS=${LOCAL_TASKS:-1} LOCAL_TASKS=${LOCAL_TASKS:-1}
SYNC_TASKS=${SYNC_TASKS:-1} SYNC_TASKS=${SYNC_TASKS:-1}
HOSTNAME=$(hostname)
##### SETUP AND FUNCTIONS ############################################# ##### SETUP AND FUNCTIONS #############################################
BEGINNING=$(/bin/date +"%d-%m-%Y ; %H:%M") START_EPOCH=$(/bin/date +%s)
DATE_FORMAT="%Y-%m-%d %H:%M:%S"
# shellcheck disable=SC2174 # shellcheck disable=SC2174
mkdir -p -m 700 ${LOCAL_BACKUP_DIR} mkdir -p -m 700 ${LOCAL_BACKUP_DIR}
# shellcheck disable=SC2174
mkdir -p -m 700 "$(dirname "${LOGFILE}")"
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
@ -121,6 +124,15 @@ pick_server() {
echo "${SERVERS}" | cut -d' ' -f${field} echo "${SERVERS}" | cut -d' ' -f${field}
} }
log() {
msg="${1:-$(cat /dev/stdin)}"
pid=$$
printf "[%s] %s[%s]: %s\\n" \
"$(/bin/date +${DATE_FORMAT})" "${PROGNAME}" "${pid}" "${msg}" \
>> "${LOGFILE}"
}
log "START GLOBAL - LOCAL_TASKS=${LOCAL_TASKS} SYNC_TASKS=${SYNC_TASKS}"
## Verify other evobackup process and kill if needed ## Verify other evobackup process and kill if needed
if [ -e "${PIDFILE}" ]; then if [ -e "${PIDFILE}" ]; then
@ -145,6 +157,8 @@ trap "rm -f ${PIDFILE}" EXIT
##### LOCAL BACKUP #################################################### ##### LOCAL BACKUP ####################################################
if [ "${LOCAL_TASKS}" = "1" ]; then if [ "${LOCAL_TASKS}" = "1" ]; then
log "START LOCAL_TASKS"
# You can comment or uncomment sections below to customize the backup # You can comment or uncomment sections below to customize the backup
## OpenLDAP : example with slapcat ## OpenLDAP : example with slapcat
@ -358,43 +372,44 @@ if [ "${LOCAL_TASKS}" = "1" ]; then
#getfacl -R /usr > ${LOCAL_BACKUP_DIR}/rights-usr.txt #getfacl -R /usr > ${LOCAL_BACKUP_DIR}/rights-usr.txt
#getfacl -R /home > ${LOCAL_BACKUP_DIR}/rights-home.txt #getfacl -R /home > ${LOCAL_BACKUP_DIR}/rights-home.txt
log "STOP LOCAL_TASKS"
fi fi
##### REMOTE BACKUP ################################################### ##### REMOTE BACKUP ###################################################
n=0
server=""
if [ "${SERVERS_FALLBACK}" = "1" ]; then
# We try to find a suitable server
while :; do
server=$(pick_server "${n}")
test $? = 0 || exit 2
if test_server "${server}"; then
break
else
server=""
n=$(( n + 1 ))
fi
done
else
# we force the server
server=$(pick_server "${n}")
fi
SSH_SERVER=$(echo "${server}" | cut -d':' -f1)
SSH_PORT=$(echo "${server}" | cut -d':' -f2)
HOSTNAME=$(hostname)
if [ "${SYSTEM}" = "linux" ]; then
rep="/bin /boot /lib /opt /sbin /usr"
else
rep="/bsd /bin /sbin /usr"
fi
if [ "${SYNC_TASKS}" = "1" ]; then if [ "${SYNC_TASKS}" = "1" ]; then
n=0
server=""
if [ "${SERVERS_FALLBACK}" = "1" ]; then
# We try to find a suitable server
while :; do
server=$(pick_server "${n}")
test $? = 0 || exit 2
if test_server "${server}"; then
break
else
server=""
n=$(( n + 1 ))
fi
done
else
# we force the server
server=$(pick_server "${n}")
fi
SSH_SERVER=$(echo "${server}" | cut -d':' -f1)
SSH_PORT=$(echo "${server}" | cut -d':' -f2)
if [ "${SYSTEM}" = "linux" ]; then
rep="/bin /boot /lib /opt /sbin /usr"
else
rep="/bsd /bin /sbin /usr"
fi
log "START SYNC_TASKS - server=${server}"
# /!\ DO NOT USE COMMENTS in the rsync command /!\ # /!\ DO NOT USE COMMENTS in the rsync command /!\
# It breaks the command and destroys data, simply remove (or add) lines. # It breaks the command and destroys data, simply remove (or add) lines.
@ -456,20 +471,19 @@ if [ "${SYNC_TASKS}" = "1" ]; then
-e "${RSH_COMMAND}" \ -e "${RSH_COMMAND}" \
"root@${SSH_SERVER}:/var/backup/" \ "root@${SSH_SERVER}:/var/backup/" \
| tail -30 >> $LOGFILE | tail -30 >> $LOGFILE
log "STOP SYNC_TASKS - server=${server}"
fi fi
##### REPORTING ####################################################### ##### REPORTING #######################################################
END=$(/bin/date +"%d-%m-%Y ; %H:%M") STOP_EPOCH=$(/bin/date +%s)
printf "EvoBackup - %s - START %s ON %s (LOCAL_TASKS=%s SYNC_TASKS=%s)\\n" \ start=$(/bin/date --date="@${START_EPOCH}" +"${DATE_FORMAT}")
"${HOSTNAME}" "${BEGINNING}" "${SSH_SERVER}" "${LOCAL_TASKS}" "${SYNC_TASKS}" \ stop=$(/bin/date --date="@${STOP_EPOCH}" +"${DATE_FORMAT}")
>> "${LOGFILE}" duration=$(( STOP_EPOCH - START_EPOCH ))
printf "EvoBackup - %s - STOP %s ON %s (LOCAL_TASKS=%s SYNC_TASKS=%s)\\n" \ log "STOP GLOBAL - start='${start}' stop='${stop}' duration=${duration}s"
"${HOSTNAME}" "${END}" "${SSH_SERVER}" "${LOCAL_TASKS}" "${SYNC_TASKS}" \
>> "${LOGFILE}"
tail -10 "${LOGFILE}" | \ tail -20 "${LOGFILE}" \
mail -s "[info] EvoBackup - Client ${HOSTNAME}" \ | mail -s "[info] EvoBackup - Client ${HOSTNAME}" ${MAIL}
${MAIL}