mithlond-public/OKF/guides/11-backup-automated.md
Lukasz Boryn c12604ebb4 feat(template): publish the Mithlond self-hosted Gitea template
A spare-machine Gitea forge, hardened, backed up and with CI, as a template.
Personal values are placeholders listed in the README.
2026-10-16 15:04:18 +02:00

9.1 KiB
Raw Blame History

type, purpose, answers, cites
type purpose answers cites
Guide Set up the nightly restic backup with failure and weekly mail.
How is the nightly automated backup set up?
scripts/gitea-backup
systemd/gitea-backup.service
systemd/gitea-backup.timer

11 — Automated Backup

Nightly, unattended backup of gitea-data/ into a restic repository on the portable HDD (the backup drive), with mail on failure and a weekly proof-of-life report. Operated remotely over the router's WireGuard tunnel, so no new ports open.

The manual tar procedure in 10-backup-manual.md stays: use it for an extra standalone archive (e.g. before a risky change). Both coexist on the backup drive under mithlond/.

Design

Choice Why
restic Incremental and deduplicated: after the first 1.3G each night adds only changes. Stores owners/modes in its own metadata, so NTFS losing them is irrelevant (gitea-data/ssh/ comes back root:root 700). Encrypted at rest.
Stop Gitea during the snapshot SQLite must not be written mid-copy. Downtime ≈ snapshot time — about a minute after the first run.
03:30 nightly, systemd timer Persistent=true catches up on boot if the box was off.
Keep 7 daily / 4 weekly / 12 monthly restic forget --prune after every run.
Mount → back up → unmount The backup drive stays plugged in but mounted only ~2 min a night. ntfs3 cannot replay the NTFS journal, so a power cut while mounted leaves it dirty and every later run fails until a human runs ntfsfix. The script never repairs or forces unattended.
Weekly check --read-data + report mail (Sunday) Reads every pack back. The mail is proof of life: no Sunday mail = the timer stopped firing — failure mail alone can't catch that.
Mail via Gmail SMTP (msmtp) Failure mail from OnFailure=, scoped to the failed run's journal.
No off-site copy The backup drive shares fire/theft/surge risk with the server. Adding one later is outbound-only (server pushes; no open ports): a second restic repo + restic copy.

Files

Repo path Installed to
scripts/gitea-backup /usr/local/sbin/gitea-backup (root-owned — never run a root unit from the user-writable ~/mithlond)
systemd/gitea-backup.service, .timer, gitea-backup-failed.service /etc/systemd/system/
systemd/gitea-backup.env.example /etc/gitea-backup/env (600)
systemd/msmtprc.example /etc/msmtprc (600)
— /etc/gitea-backup/restic-password (600)

gitea-backup subcommands: init, run, snapshots, check, restore-test, test-mail, notify-failure. All need root.

Setup (one-time, in order)

1. Copy the files to the server

From this repo on your workstation (no rsync on the server):

for f in scripts/gitea-backup systemd/gitea-backup.service systemd/gitea-backup.timer \
         systemd/gitea-backup-failed.service systemd/gitea-backup.env.example systemd/msmtprc.example; do
  ssh -p 2234 admin@192.168.1.50 "mkdir -p ~/mithlond/$(dirname $f) && cat > ~/mithlond/$f" < $f
done

Everything below runs on the server.

2. Packages

sudo pacman -S restic msmtp

3. Install script and units

cd ~/mithlond
sudo install -o root -g root -m 755 scripts/gitea-backup /usr/local/sbin/gitea-backup
sudo install -o root -g root -m 644 systemd/gitea-backup.service systemd/gitea-backup.timer \
     systemd/gitea-backup-failed.service /etc/systemd/system/
sudo systemctl daemon-reload

4. Config and restic password

sudo install -d -o root -g root -m 700 /etc/gitea-backup
sudo install -o root -g root -m 600 systemd/gitea-backup.env.example /etc/gitea-backup/env
sudo sed -i 's/^MAILTO=.*/MAILTO=<you>@gmail.com/' /etc/gitea-backup/env   # no nano/vi on the server
sudo sh -c 'umask 077; openssl rand -base64 33 > /etc/gitea-backup/restic-password'
sudo cat /etc/gitea-backup/restic-password   # copy into your password manager NOW

Without this password the backups cannot be decrypted — it must live somewhere other than the server. Label it clearly (e.g. "mithlond restic"): it is 44 characters, the Gmail app password in step 5 is 16. Pasting the restic one into step 5 by mistake gets a 535 from Gmail, and means it has to be regenerated, because it has been sent to Gmail. If that happens before init, just rerun the openssl line; after init use restic key passwd.

The server has no nano or vi; sed edits are used throughout (vim exists if you need an editor).

5. Mail

Create a Gmail app password at https://myaccount.google.com/apppasswords — Google no longer links that page from its menus, open the URL directly. Name it mithlond, Create, copy the 16 letters. Needs 2-Step Verification on; with only passkeys/security keys as second step, or Advanced Protection, the page says "not available" (add an authenticator app, or use another alert route). Then:

sudo install -o root -g root -m 600 systemd/msmtprc.example /etc/msmtprc
sudo sed -i 's/you@gmail.com/<you>@gmail.com/' /etc/msmtprc
read -rsp 'App password: ' P; echo         # -s: off screen, out of history
sudo sed -i "s/REPLACE_WITH_16_CHAR_APP_PASSWORD/${P// /}/" /etc/msmtprc; unset P   # strips Google's spaces
sudo grep -c REPLACE /etc/msmtprc            # must print 0
sudo awk '$1=="password"{print "password length:", length($2)}' /etc/msmtprc   # must be 16
sudo gitea-backup test-mail                  # no output = sent; check inbox and spam

Subject: [mithlond] gitea-backup test mail. If msmtp answers 535-5.7.8 Username and Password not accepted:

sudo grep -E '^(from|user)' /etc/msmtprc     # both your full Gmail address
sudo awk '$1=="password"{print "password length:", length($2)}' /etc/msmtprc

Length 44 = the restic password went in (see step 4); any length ≠ 16 = not an app password. Replace it (the placeholder is gone by now, so match the line):

read -rsp 'Gmail app password (16 letters): ' P; echo
sudo sed -i "s/^password .*/password       ${P// /}/" /etc/msmtprc; unset P

6. Create the repository

The backup drive plugged in (not necessarily mounted — the script mounts it):

sudo gitea-backup init

Ends created restic repository <id> at /mnt/backup/mithlond/restic, then ==> unmounted /mnt/backup. If it fails with "dirty", fix per ntfs-repair.md first.

7. First run, by hand

sudo systemctl start gitea-backup            # blocks until done; first run uploads all 1.3G
systemctl status gitea-backup --no-pager     # Result=success
journalctl -u gitea-backup -n 40 --no-pager  # "gitea down Ns", snapshot id
sudo gitea-backup snapshots

Run it via systemctl, not gitea-backup run, so the real unit path — including OnFailure= — is what gets proven. Measured on the reference server: first run 86 s Gitea downtime (1.2 GiB, all new); second run 5 s (14 KiB changed). The unit's "1.7G memory peak" is mostly page cache from reading gitea-data, not restic — free shows it reclaimable.

8. Restore test

sudo gitea-backup restore-test

Restores the latest snapshot into /var/tmp/gitea-restore-test, checks gitea-data/ssh/ is root:root 700, runs SQLite integrity_check, fscks every repo inside the gitea image, boots a throwaway Gitea (--network none), lists users — then deletes the scratch copy. Must end RESTORE TEST PASSED.

9. Prove failure mail

Unplug the backup drive first, then start the run — never unplug during one. Check it is free: findmnt /mnt/backup prints nothing and systemctl is-active gitea-backup is not activating.

sudo systemctl start gitea-backup            # fails at once: "HDD 'BACKUP' not attached"

A gitea-backup FAILED on mithlond (exit-code) mail must arrive with that error. Plug the backup drive back in.

Pulling the drive during a run instead (tested once, mid-prune) proves more, and costs more:

  • restic retried input/output error with back-off for minutes; the run looked hung. sudo systemctl stop gitea-backup ended it — a manual stop also counts as a failure (Result: signal) and sends the mail. (Ctrl+C on systemctl start only stops waiting; the run continues.)
  • The EXIT trap still ran: Gitea was already back (5 s down), the dead mount was detached.
  • The backup drive came back dirty with a $MFTMirr mismatch, then real NTFS damage → ntfs-repair.md rung 3 (chkdsk). A stale restic lock was left too.

10. Enable the timer

sudo systemctl enable --now gitea-backup.timer
systemctl list-timers gitea-backup.timer     # NEXT = tonight 03:30

Remote operation, real restore and troubleshooting: backup-operations.md.

Checklist

  • Packages, script, units, config installed (steps 2–4)
  • Restic password stored off-server
  • test-mail arrives (step 5)
  • First run via systemctl succeeds (step 7)
  • restore-test passes (step 8)
  • Failure mail proven (step 9)
  • Timer enabled (step 10)
  • First Sunday report mail received

Next step

Continue with 12-gitea-actions.md.