add logging to syslog

dev
Bryan 2020-11-01 13:55:52 -06:00
parent 2fbe90c0cb
commit 115758537d
1 changed files with 23 additions and 8 deletions

View File

@ -1,8 +1,16 @@
#!/bin/bash
VERSION="1.5.2"
SCRIPT_NAME=$(basename $0)
log () {
echo "$@" | logger -p user.notice -t ${SCRIPT_NAME}
}
err () {
echo "$@" | logger -p user.err -t ${SCRIPT_NAME}
}
print_usage () {
cat << EOF
Usage: borgwrapper [OPTIONS] MODE
@ -27,7 +35,7 @@ error_handler () {
local SCRIPT_NAME="$0"
local LINE="$1"
local EXIT_CODE="$2"
>&2 echo "${SCRIPT_NAME}: Error in line ${LINE} (exit code ${EXIT_CODE})"
err "${SCRIPT_NAME}: Error in line ${LINE} (exit code ${EXIT_CODE})"
exit ${EXIT_CODE}
}
@ -45,28 +53,30 @@ save_package_list () {
dump_sql () {
if ! which mysqldump; then
log "No mysqldump"
return
fi
if [ ! -d "${MYSQL_DUMP_DIRECTORY}" ]
then mkdir -p "${MYSQL_DUMP_DIRECTORY}"
fi
log "Dumping mysql databases"
for DB in $(mysql -u 'root' -e 'show databases' -s --skip-column-names); do
mysqldump --single-transaction -u 'root' "${DB}" > "${MYSQL_DUMP_DIRECTORY}${DB}.sql.bak" || echo "mysqldump error"
mysqldump --single-transaction -u 'root' "${DB}" > "${MYSQL_DUMP_DIRECTORY}${DB}.sql.bak" || err "Error dumping mysql"
done
}
dump_mongo () {
if ! which mongodump; then
return
log "No mongodump"
return
fi
if [ ! -d "${MONGODB_DUMP_DIRECTORY}" ]
then mkdir -p "${MONGODB_DUMP_DIRECTORY}/mongodump"
fi
mongodump --out "${MONGODB_DUMP_DIRECTORY}/mongodump"
log "Dumping mongo database"
mongodump --out "${MONGODB_DUMP_DIRECTORY}/mongodump" || err "Error dumping mongo database"
}
@ -77,6 +87,7 @@ borg_init () {
--encryption repokey-blake2
)
fi
log "Initializing ${BORG_REPO}"
${BORG} init "${BORG_INIT_ARGS[@]}" "${BORG_REPO}"
}
@ -102,11 +113,14 @@ borg_backup () {
BORG_CREATE_ARGS+=( --stats )
fi
log "Creating borg repo using ${BORG} create ${BORG_CREATE_ARGS[@]} ${BORG_REPO}::${SYSTEM_NAME}-$(date -u +'%Y-%m-%dT%H-%M-%SZ') ${PATHS[@]} ${EXCLUDE_CMD[@]}"
${BORG} create \
"${BORG_CREATE_ARGS[@]}" \
"${BORG_REPO}"::"${SYSTEM_NAME}-$(date -u +'%Y-%m-%dT%H-%M-%SZ')" \
"${PATHS[@]}" \
"${EXCLUDE_CMD[@]}"
"${EXCLUDE_CMD[@]}" \
|| log "Error backing up. Has repo been initialized?"
}
borg_prune () {
@ -122,7 +136,7 @@ borg_prune () {
else
BORG_PRUNE_ARGS+=( --stats )
fi
log "Running borg prune"
${BORG} prune \
"${BORG_PRUNE_ARGS[@]}" \
--prefix "${SYSTEM_NAME}-" \
@ -141,6 +155,7 @@ borg_verify () {
)
fi
log "Running borg check"
${BORG} check "${BORG_CHECK_ARGS[@]}" "${BORG_REPO}"
}