feat: remove logging for systemd assumption

With systemd, we don't need to specify any logging output, and the
tee'ing is complicating things anyway.  This removes all the external
logging, and adds the systemd service and timer required to make it go.
This commit is contained in:
Alex Kelly 2023-01-16 22:43:32 -05:00
parent 248ef13ee9
commit 041b70319e
3 changed files with 33 additions and 24 deletions

View file

@ -8,11 +8,6 @@ set -e -o pipefail
BACKUP_PATHS="/" BACKUP_PATHS="/"
BACKUP_EXCLUDES="/etc/restic/exclude" BACKUP_EXCLUDES="/etc/restic/exclude"
LOG_DIR="/var/log/restic"
LOG_BASENAME="backup"
LOG_FILE="$LOG_DIR/$LOG_BASENAME-$(date +%Y%m%d%H%M%S)"
LOG_KEEP=14
BACKUP_TAG=$(hostname -s) BACKUP_TAG=$(hostname -s)
@ -26,7 +21,6 @@ source /etc/restic/env
# Paths to binaries # Paths to binaries
CURL="/usr/bin/curl" CURL="/usr/bin/curl"
TEE="/usr/bin/tee"
FIND="/usr/bin/find" FIND="/usr/bin/find"
RESTIC="/usr/bin/restic" RESTIC="/usr/bin/restic"
ECHO="/usr/bin/echo" ECHO="/usr/bin/echo"
@ -85,8 +79,6 @@ cleanup () {
# Remove locks in case other stale processes kept them in # Remove locks in case other stale processes kept them in
${ECHO} "Removing lockfile" ${ECHO} "Removing lockfile"
${RESTIC} unlock ${RESTIC} unlock
${ECHO} "Removing logs ($LOG_DIR/$LOG_BASE-*) older than $LOG_KEEP days"
${FIND} "$LOG_DIR" -name "$LOG_BASE-*" -mtime +$LOG_KEEP
} }
send_matrix () { send_matrix () {
@ -114,37 +106,33 @@ ERRORS=""
STATUS_TOTAL=0 STATUS_TOTAL=0
if run_checklock; then if run_checklock; then
${ECHO} "Restic backup on $HOST starting at $(timestamp)"| ${TEE} -a "$LOG_FILE" >(notify) ${ECHO} "Restic backup on $HOST starting at $(timestamp)"|notify
locks=0 locks=0
else else
${ECHO} "Backup on $HOST aborted at $(timestamp) due to locks" | ${TEE} -a "$LOG_FILE" >(notify) ${ECHO} "Backup on $HOST aborted at $(timestamp) due to locks" |notify
exit 1 exit 1
fi fi
if ! run_backup | tee -a $LOG_FILE; then if ! run_backup; then
backup_status=1 STATUS_TOTAL=$((STATUS_TOTAL+1))
ERRORS="$ERRORS [backup step failed]" ERRORS="$ERRORS [backup step failed]"
fi fi
if ! run_forget | tee -a $LOG_FILE; then if ! run_forget; then
forget_status=1 STATUS_TOTAL=$((STATUS_TOTAL+1))
ERRORS="$ERRORS [forget step failed]" ERRORS="$ERRORS [forget step failed]"
fi fi
if ! run_check | tee -a $LOG_FILE; then if ! run_check ; then
check_status-1 STATUS_TOTAL=$((STATUS_TOTAL+1))
ERRORS="$ERRORS [check step failed]" ERRORS="$ERRORS [check step failed]"
fi fi
cleanup | tee -a $LOG_FILE cleanup
# if (( STATUS_TOTAL == 0 ));then
#if run_backup && ${RESTIC} unlock && run_forget && cleanup; then ${ECHO} "Backup on $HOST completed successfully at $(timestamp)" |notify
# run_check
#cleanup
if (( status_total == 0 ));then
${ECHO} "Backup on $HOST completed successfully at $(timestamp)" | ${TEE} -a "$LOG_FILE" >(notify)
else else
${ECHO} "Backup on $HOST completed at $(timestamp) with issues: $ERRORS (see $LOG_FILE)" | ${TEE} -a "$LOG_FILE" >(notify) ${ECHO} "Backup on $HOST completed at $(timestamp) with issues: $ERRORS " |notify
fi fi

View file

@ -0,0 +1,11 @@
[Unit]
Description=Initiates restic backup script
Wants=restic_backup.timer
[Service]
Type=oneshot
ExecStart=/etc/restic/backup.sh
SyslogIdentifier=restic_backup
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,10 @@
[Unit]
Description=Executes the restic backup script
Requires=restic-backup.service
[Timer]
Unit=restic-backup.service
OnCalendar=*-*-* 22:40:00
[Install]
WantedBy=timers.target