Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 11.1DevOps tools, Chapter 11

A day on call: service, logs, backup with rsync, cron and logrotate

Level
Medium
50 min
Machines
two Linux machines or VMs (server and backup), root on the server
Status
Not started

Goal

Do the daily work of a Linux operator in one story. A small app runs as a systemd service, it crashes, you find the reason in the journal, you make it restart by itself, you back up its data to a second machine with SSH keys and rsync, you schedule the backup, rotate the app log, and you write a short runbook so the next person can do the same at 3 a.m.

Before you start

  • Two machines: app1 (server) and backup1. You can log in to both with a normal user that has sudo.
  • Make the app on app1: a script /usr/local/bin/ticker.sh that writes the date to /var/log/ticker/ticker.log every 5 seconds and exits with an error when the file /etc/ticker/stop exists.

Tasks

  1. Write the unit ticker.service with Restart=on-failure, RestartSec=5, a dedicated system user ticker and ProtectSystem=strict with write access only to /var/log/ticker and /var/lib/ticker. Enable and start it.
  2. Make it fail (create /etc/ticker/stop). Find the reason and the exit code with systemctl status and journalctl. Remove the file and check that systemd restarted the service by itself.
  3. Make an SSH key only for backups on app1 and allow it on backup1 with restrictions in authorized_keys (no shell, no forwarding).
  4. Back up /var/lib/ticker and /etc/ticker to backup1:/srv/backup/app1/ with rsync (archive, compress, delete removed files, keep 7 dated copies with hard links).
  5. Run the backup every night at 02:15 with cron; send output to a log; prevent two backups at the same time with flock.
  6. Rotate /var/log/ticker/ticker.log daily, keep 14 files, compress, and test the rule.
  7. Write a 10-line runbook "ticker is down": symptoms, checks, fix, escalation.

Check your work

systemctl is-active ticker && systemctl show ticker -p NRestarts
journalctl -u ticker --since "-30 min" -p warning --no-pager | tail -5
ssh -i ~/.ssh/backup_ed25519 backup1 true; echo "exit=$?"
ls /srv/backup/app1/
sudo logrotate -d /etc/logrotate.d/ticker 2>&1 | tail -5
crontab -l
  • active and NRestarts at least 1 (the automatic restart).
  • The journal shows exited, code=exited, status=1/FAILURE.
  • A normal SSH command with the backup key is refused, but rsync with it works.
  • backup1 has dated folders like 2026-11-14 and a latest link.
  • logrotate -d shows the rule without errors.

Hints

Show the hints

Solution

Show the solution
  1. The app and the unit.
    /usr/local/bin/ticker.sh
    #!/bin/bash
    while true; do
      [ -e /etc/ticker/stop ] && { echo "stop file found" >&2; exit 1; }
      date >> /var/log/ticker/ticker.log
      sleep 5
    done
    /etc/systemd/system/ticker.service
    [Unit]
    Description=Ticker demo app
    After=network.target
     
    [Service]
    User=ticker
    ExecStart=/usr/local/bin/ticker.sh
    Restart=on-failure
    RestartSec=5
    ProtectSystem=strict
    ReadWritePaths=/var/log/ticker /var/lib/ticker
    NoNewPrivileges=yes
     
    [Install]
    WantedBy=multi-user.target
    sudo useradd --system --no-create-home --shell /usr/sbin/nologin ticker
    sudo mkdir -p /var/log/ticker /var/lib/ticker /etc/ticker && sudo chown ticker: /var/log/ticker /var/lib/ticker
    sudo chmod 755 /usr/local/bin/ticker.sh
    sudo systemctl daemon-reload && sudo systemctl enable --now ticker
  2. Break, read, recover.
    sudo touch /etc/ticker/stop
    systemctl status ticker --no-pager | head -6
    journalctl -u ticker -n 10 --no-pager
    sudo rm /etc/ticker/stop
    sleep 6; systemctl is-active ticker
    active
  3. A restricted backup key.
    ssh-keygen -t ed25519 -f ~/.ssh/backup_ed25519 -N "" -C "backup app1"
    ssh backup1 'sudo mkdir -p /srv/backup/app1 && sudo chown $USER: /srv/backup/app1'
    ssh-copy-id -i ~/.ssh/backup_ed25519.pub backup1

    On backup1, put the options in front of that key in ~/.ssh/authorized_keys:

    ~/.ssh/authorized_keys (backup1)
    command="rrsync -wo /srv/backup/app1",restrict ssh-ed25519 AAAA...key... backup app1
  4. The backup script.
    /usr/local/bin/backup-app1.sh
    #!/bin/bash
    set -euo pipefail
    DAY=$(date +%F)
    SSH="ssh -i $HOME/.ssh/backup_ed25519 -o UserKnownHostsFile=$HOME/.ssh/known_hosts"
    sudo rsync -az --delete -e "$SSH" --link-dest=../latest /var/lib/ticker /etc/ticker "$USER@backup1:$DAY/"
    echo "$(date -Is) backup $DAY ok"

    sudo is needed because only the user ticker can read its data; allow exactly this one command without a password in /etc/sudoers.d/backup (youruser ALL=(root) NOPASSWD: /usr/bin/rsync), so cron can run it.

    The latest link and the cleanup of copies older than 7 days run on backup1 itself (its own cron job: ln -sfn to the newest folder and find /srv/backup/app1 -maxdepth 1 -mtime +7 -exec rm -rf {} +), because the restricted key can only write.

  5. Cron with a lock.
    crontab -e
    crontab
    15 2 * * * flock -n /tmp/backup-app1.lock /usr/local/bin/backup-app1.sh >> $HOME/backup-app1.log 2>&1
  6. Log rotation.
    /etc/logrotate.d/ticker
    /var/log/ticker/ticker.log {
        daily
        rotate 14
        compress
        delaycompress
        missingok
        notifempty
        copytruncate
    }
    sudo logrotate -d /etc/logrotate.d/ticker
    sudo logrotate -f /etc/logrotate.d/ticker && ls /var/log/ticker/
  7. The runbook.
    RUNBOOK-ticker.md
    # ticker is down
    Symptom: no new lines in /var/log/ticker/ticker.log, monitoring alert "ticker inactive".
    1. systemctl status ticker            -> active? how many restarts?
    2. journalctl -u ticker -n 50         -> last error message
    3. Known cause: /etc/ticker/stop exists -> remove it if nobody planned a stop
    4. Disk full? df -h /var              -> clean or extend
    5. systemctl restart ticker; tail -f /var/log/ticker/ticker.log
    6. Not fixed in 15 min -> call the app owner (see the on-call list), write the incident note.

My notes

No notes yet.

Lessons for this lab

Commands in this lab