A backup can go wrong without anyone hearing about it in three ways:
You want a signal for each. Your logs can give you all three if the job writes one line.
Cron logs the start for free:
Dec 2 02:00:01 web01 CRON[20480]: (root) CMD (/usr/local/bin/backup.sh) If that line goes to a central log server, the backup's schedule is in your logs, and so is the night it didn't run. That's all a log line can tell you about the start. Whether it worked is up to the script.
Make the script say so, in one line, success or failure:
#!/bin/bashset -o pipefailf=/backup/app-$(date +%F).sql.gzif mysqldump app | gzip > "$f.tmp" && gzip -cd "$f.tmp" | tail -n 1 | grep -q '^-- Dump completed' && mv "$f.tmp" "$f"; then logger -t backup "ok db=app size=$(stat -c %s "$f")"else rm -f "$f.tmp" logger -t backup "FAILED db=app" exit 1fi
pipefail matters: without it the pipeline's exit status is gzip's, and a failed dump still counts as success. A file size check isn't enough, because an empty dump is still about 20 bytes once gzipped. mysqldump ends a complete dump with a -- Dump completed line, so the script checks for it: an empty or cut-off dump doesn't have it. The dump is written to a .tmp file and only gets its real name when both checks pass, so a failed run leaves no backup file behind. Keep the % signs in the script: in a crontab line they turn into newlines.
Then search your logs for backup. A FAILED line, or no ok line since last night, is the one to act on. One more check that doesn't trust the script, run on the backup server:
find /backup -name 'app-*.sql.gz' -mmin -1500 | grep -q . || logger -t backup "FAILED no backup newer than 25 h"
When backup01 pulls from web01 with rsync over SSH at 02:00, web01 logs the login every night:
Dec 2 02:00:04 web01 sshd[20511]: Accepted publickey for root from 192.0.2.10 port 51544 ssh2 Same user, same source IP, same time. Trunc learns that as a schedule, like a cron job. When backup01 stops coming, web01 never logs anything about it, and the alert reads:
SSH login as root from 192.0.2.10 did not show up on web01 at 02:00 PST.It happens daily at 02:00.Method: publickeyLast login: Tue 02:00 PST (25 h ago)Check the job on 192.0.2.10 that makes this login, and search your logs for 192.0.2.10. The same confirmed login also stops tripping the off-hours rules. A root login at 02:00 every night is exactly what behavior rules would flag, and once it's a schedule (same server, user and source IP, within a few minutes of its usual time) it doesn't. Details on missing log alerts.
For the backup, a ping monitor that hears about success is worth having on top. Healthchecks.io and Cronitor both work this way: the job calls a URL when it's done, and they alert when the call doesn't come. With &&, the ping only goes out when the script exits 0:
0 2 * * * /usr/local/bin/backup.sh && curl -fsS https://hc-ping.com/your-uuid Cron monitoring compares the approaches.
logger -t CRON '(root) CMD (/usr/local/bin/backup.sh)' or logger -t backup ok. Our alert counts the first as a run.FAILED line and the file check above.For a full example of what stops when cron does, see we stopped cron for 19 hours.
Test the restore, too. A backup nobody has restored is a guess.
Questions about Trunc or a trial: email us at sales@noc.org.
We love logs. In this section we will share some articles from our team to help you get better at logging.
Logging for fun and a good night of sleep.
Latest articles from our learning center.
Do you have an idea for an article that is not here? See something wrong? Contact us at support@noc.org
14 days free trial. No credit card required.