mithlond-public/OKF/guides/10-backup-manual.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

249 lines
8.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
type: Guide
purpose: Archive Gitea's data volume to the backup drive by hand and prove it restores.
answers:
- "How is a manual backup made and restore-tested?"
cites:
- scripts/backup.sh
---
# 10 — Backup Procedure (manual)
Manual backup of Gitea's data volume to the portable HDD, followed by a restore
test on a scratch copy. Validated end to end on the reference server.
Nightly automated backups (restic) are in [11-backup-automated.md](11-backup-automated.md); this manual archive remains for standalone copies.
The whole procedure is scripted in [`scripts/backup.sh`](../../scripts/backup.sh).
The manual steps below are the same commands in the same order (numbered to
match the script), for when something goes off-script.
## What gets backed up
`gitea-data/` holds all Gitea state:
| Path | Contents |
|---|---|
| `gitea-data/gitea/gitea.db` | SQLite database (users, issues, settings) |
| `gitea-data/git/repositories/` | All bare git repositories |
| `gitea-data/gitea/conf/app.ini` | Gitea configuration |
| `gitea-data/gitea/attachments/`, `avatars/` | Uploads |
| `gitea-data/ssh/` | Gitea's SSH **host keys** — `root:root`, mode 700. Lose them and every client sees a changed host-key warning on port 2222 |
Not backed up: `caddy-data/` (Caddy re-issues certs) and any stateless apex
site (rebuild it from source).
## The backup drive
| | |
|---|---|
| Drive | any USB HDD (example: 1.8T) |
| Partition | `sdb1`, **NTFS**, label **`BACKUP`** (`/dev/disk/by-label/BACKUP`) |
| Backups live in | `mithlond/` at the drive root |
**Never format the backup drive** — it holds other data. Everything here only adds files
under `mithlond/`.
Because it is NTFS, Linux owners and modes cannot be stored as files on it.
So the backup is a single `tar -p` archive, which carries them inside — never
`cp -r` the directory across.
## Prerequisites (one-time)
```bash
sudo pacman -S ntfsprogs # ntfsfix — NOT in ntfs-3g, which only has the FUSE driver
```
Already on the server: `sqlite3`, `docker`. The kernel's built-in `ntfs3`
driver does the mounting. The server has **no `git`**; repo checks run inside
the `gitea/gitea` image.
## Quick path — the script
Plug in the backup drive, then on the server as the admin user:
```bash
~/mithlond/scripts/backup.sh # back up + restore test
~/mithlond/scripts/backup.sh verify gitea-backup-YYYYMMDD-HHMM.tar.gz # re-test an old archive
```
It asks for the sudo password up front (not mid-downtime), always restarts
Gitea even if a step fails, deletes a half-written archive, removes the scratch
copy (it holds host-key copies), and unmounts the backup drive if it mounted it. If the
volume is dirty it runs the `ntfsfix` dry run and asks before clearing the
flag. Gitea is down only for step 4 (~2 min for 1.3G).
The script must exist on the server: copy it over after editing
(no `rsync` there):
```bash
ssh -p 2234 admin@192.168.1.50 'mkdir -p ~/mithlond/scripts && cat > ~/mithlond/scripts/backup.sh && chmod +x ~/mithlond/scripts/backup.sh' < scripts/backup.sh
```
## Manual procedure
All on the server (`ssh -p 2234 admin@192.168.1.50`), as the admin user.
### 1. Find the drive
```bash
lsblk -o NAME,SIZE,FSTYPE,LABEL,MOUNTPOINTS,TRAN
# expect: sdb1 1.8T ntfs BACKUP ... usb
```
### 2. Mount it
```bash
sudo mkdir -p /mnt/backup
sudo mount -t ntfs3 /dev/disk/by-label/BACKUP /mnt/backup
ls /mnt/backup # your existing files — sanity check
```
**If it fails with `wrong fs type, bad option, bad superblock`**, check why:
```bash
sudo dmesg | tail
# ntfs3(sdb1): volume is dirty and "force" flag is not set!
```
Windows left the dirty flag (unsafe eject, or Fast Startup). Full triage —
including real damage and running `chkdsk` with no Windows machine — is in
[ntfs-repair.md](ntfs-repair.md). For the common flag-only case:
```bash
sudo ntfsfix -n /dev/disk/by-label/BACKUP # dry run, writes nothing — must end "processed successfully"
sudo ntfsfix -d /dev/disk/by-label/BACKUP # only if the dry run was clean: clears flag, resets journal
sudo mount -t ntfs3 /dev/disk/by-label/BACKUP /mnt/backup
```
`ntfsfix` is not a repair tool. If the dry run reports anything but a clean
pass, follow [ntfs-repair.md](ntfs-repair.md) instead. **Never `mount -o force`** — writing to a dirty volume risks the
other data on the backup drive.
### 3. Create the backup folder
```bash
sudo mkdir -p /mnt/backup/mithlond
```
### 4. Stop Gitea, archive, start Gitea
```bash
cd ~/mithlond
docker compose stop gitea # SQLite must not be written mid-copy
NAME=gitea-backup-$(date +%Y%m%d-%H%M)
sudo tar -czpf /mnt/backup/mithlond/$NAME.tar.gz -C ~/mithlond gitea-data
docker compose start gitea # do NOT skip — miss it and Gitea stays down
```
`sudo` because `gitea-data/ssh/` is root-only. `-p` keeps owners and modes.
Only Gitea stops; Caddy keeps serving the apex site.
### 5. Checksum
```bash
cd /mnt/backup/mithlond
sudo sh -c "sha256sum $NAME.tar.gz > $NAME.tar.gz.sha256"
ls -lh /mnt/backup/mithlond
sync
```
### 6. Confirm Gitea is back
```bash
docker compose -f ~/mithlond/compose.yml ps # gitea: Up
curl -sI https://git.example.com | head -1 # from anywhere: 200 (a 502 for ~20s while it boots)
```
### 7–13. Restore test
Proves the archive restores, without touching the live stack. Extract into
`~` — `/tmp` is a 1.5G tmpfs, too small.
```bash
# 7. checksum
cd /mnt/backup/mithlond && sha256sum -c $NAME.tar.gz.sha256 # OK
# 8. contents — host keys present with root:root 700
tar -tvzf $NAME.tar.gz | grep ' gitea-data/ssh/$' # drwx------ root/root
# 9. extract to scratch
mkdir ~/restore-test && tar -xzf $NAME.tar.gz -C ~/restore-test
R=~/restore-test/gitea-data
# 10. SQLite integrity + counts
sqlite3 -readonly $R/gitea/gitea.db 'PRAGMA integrity_check;' # ok
sqlite3 -readonly $R/gitea/gitea.db \
"select 'users=' || count(*) from user; select 'repos=' || count(*) from repository;"
# 11. git fsck every repo, using the gitea image's git (read-only mount)
IMG=$(docker inspect -f '{{.Config.Image}}' gitea)
docker run --rm --user 1000:1000 --entrypoint sh -v $R/git/repositories:/r:ro $IMG -c \
'cd /r; for d in */*.git; do out=$(git -c safe.directory="*" --git-dir=$d fsck --no-progress --no-dangling 2>&1) && r=${out:-OK} || r="FAILED $out"; printf "%-36s %s\n" $d "$r"; done'
diff -rq $R/git ~/mithlond/gitea-data/git && echo identical # differs only if pushed since
# 12. boot a throwaway Gitea on the extract — no network, live stack untouched
docker run -d --rm --name gitea-restore-test --network none \
-e USER_UID=1000 -e USER_GID=1000 -v $R:/data $IMG
sleep 25
docker exec gitea-restore-test wget -qO- http://127.0.0.1:3000/api/v1/version
docker exec gitea-restore-test su git -c 'gitea admin user list'
docker stop gitea-restore-test
# 13. cleanup — the extract holds copies of the SSH host keys
rm -rf ~/restore-test
```
An empty repo (never pushed, e.g. `test-private`) reports
`notice: HEAD points to an unborn branch` — that is fine.
### 14. Unmount
```bash
cd ~ # step 5 left this shell inside /mnt/backup/mithlond
sync
sudo umount /mnt/backup # umount — no "n"
findmnt /mnt/backup # no output = unmounted
```
`umount: /mnt/backup: target is busy` means some shell is still inside the
mount — the prompt reads `[… mithlond]$` both there and in `~/mithlond`, so
it is easy to miss. Find it with
`ls -l /proc/*/cwd 2>/dev/null | grep /mnt/backup`, `cd ~` in that shell, retry.
Unplug only after `findmnt` prints nothing. Eject safely on Windows too, or the dirty
flag comes back.
A real restore from an archive: [backup-operations.md](backup-operations.md#restore-from-a-tar-archive).
## Backup frequency
No automated schedule. Back up:
- Before any system update (`pacman -Syu`)
- Before any Docker image update
- After significant work (pushed important repos, added users)
- At minimum: weekly
## Retention
Keep at least two dated archives in `BACKUP:/mithlond/`. The script reports the
count but never deletes. Delete older ones by hand when space is needed — keep
at least one that predates any recent change you haven't verified.
## Log
| Date | Archive | Size | Restore test |
|---|---|---|---|
| YYYY-MM-DD | `gitea-backup-YYYYMMDD-HHMM.tar.gz` | — | pass / fail, and what was checked |
## Next step
Continue with [11-backup-automated.md](11-backup-automated.md).
## Checklist
- [ ] Performed first backup after Gitea setup
- [ ] Restore test completed on separate directory (`~/restore-test`, throwaway container)
- [ ] Repos and users verified intact after restore (integrity_check ok, fsck clean, `gitea admin user list`)