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) andbackup1. You can log in to both with a normal user that hassudo. - Make the app on
app1: a script/usr/local/bin/ticker.shthat writes the date to/var/log/ticker/ticker.logevery 5 seconds and exits with an error when the file/etc/ticker/stopexists.
Tasks
Tick a task when it is done. Only your browser remembers it.
- Write the unit
ticker.servicewithRestart=on-failure,RestartSec=5, a dedicated system usertickerandProtectSystem=strictwith write access only to/var/log/tickerand/var/lib/ticker. Enable and start it. - Make it fail (create
/etc/ticker/stop). Find the reason and the exit code withsystemctl statusandjournalctl. Remove the file and check that systemd restarted the service by itself. - Make an SSH key only for backups on
app1and allow it onbackup1with restrictions inauthorized_keys(no shell, no forwarding). - Back up
/var/lib/tickerand/etc/tickertobackup1:/srv/backup/app1/withrsync(archive, compress, delete removed files, keep 7 dated copies with hard links). - Run the backup every night at 02:15 with cron; send output to a log; prevent two backups at the same time with
flock. - Rotate
/var/log/ticker/ticker.logdaily, keep 14 files, compress, and test the rule. - 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 -lactiveandNRestartsat 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
rsyncwith it works. backup1has dated folders like2026-11-14and alatestlink.logrotate -dshows the rule without errors.
Hints
Show the hints
journalctl -u ticker -b -n 30,systemctl status tickershowsMain PID ... (code=exited, status=1).authorized_keysoptions:command="rrsync -wo /srv/backup/app1",restrict ssh-ed25519 AAAA....rrsynccomes with rsync (on some systems in/usr/share/rsync/scripts/or/usr/bin/rrsync).- Snapshots with hard links:
rsync -a --delete --link-dest=../latest SRC DEST/$(date +%F)/. flock -n /run/lock/backup.lock CMDexits when the lock is taken.
Solution
Show the solution
- 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 - Break, read, recover.
- 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 backup1On
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 - 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"sudois needed because only the usertickercan 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
latestlink and the cleanup of copies older than 7 days run onbackup1itself (its own cron job:ln -sfnto the newest folder andfind /srv/backup/app1 -maxdepth 1 -mtime +7 -exec rm -rf {} +), because the restricted key can only write. - Cron with a lock.
crontab -ecrontab15 2 * * * flock -n /tmp/backup-app1.lock /usr/local/bin/backup-app1.sh >> $HOME/backup-app1.log 2>&1 - Log rotation./etc/logrotate.d/ticker
/var/log/ticker/ticker.log { daily rotate 14 compress delaycompress missingok notifempty copytruncate } - 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.