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

233 lines
8.4 KiB
Bash
Executable File
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.

#!/usr/bin/env bash
# Back up Gitea's data volume to the portable HDD, then prove the archive restores.
#
# Run on the server as the admin user (not root), from anywhere:
# ~/mithlond/scripts/backup.sh # back up + verify
# ~/mithlond/scripts/backup.sh verify ARCHIVE # re-verify an existing archive
#
# ARCHIVE is a path, or a bare file name inside $MOUNT/$DEST_DIR.
# Procedure and rationale: OKF/guides/10-backup-manual.md. Each step below is numbered to match.
set -euo pipefail
LABEL=${LABEL:-BACKUP} # filesystem label of the portable HDD
MOUNT=${MOUNT:-/mnt/backup}
DEST_DIR=${DEST_DIR:-mithlond} # folder at the HDD root — the rest of the drive is untouched
STACK=$(cd "$(dirname "$0")/.." && pwd)
SCRATCH=$HOME/restore-test # not /tmp: it is a 1.5G tmpfs, too small for the extract
TEST_CONTAINER=gitea-restore-test
mounted_by_us=0
gitea_stopped=0
partial_archive=
scratch_ours=0
log() { printf '\n==> %s\n' "$*"; }
die() { printf '\nERROR: %s\n' "$*" >&2; exit 1; }
usage() { sed -n '2,9p' "$0" | sed 's/^# \{0,1\}//'; exit 2; }
# Whatever fails, never leave Gitea stopped, a half-written archive, host-key
# copies in $SCRATCH, or the HDD mounted.
cleanup() {
local rc=$?
if (( gitea_stopped )); then
echo "==> restarting gitea (exit path)"
(cd "$STACK" && docker compose start gitea) \
|| echo "!! gitea did not start — run: cd $STACK && docker compose start gitea" >&2
fi
if [[ -n $partial_archive ]]; then
echo "==> removing incomplete archive $partial_archive"
sudo rm -f "$partial_archive" "$partial_archive.sha256"
fi
docker rm -f "$TEST_CONTAINER" >/dev/null 2>&1 || true
if (( scratch_ours )); then rm -rf "$SCRATCH"; fi
if (( mounted_by_us )); then
cd "$HOME" # a cwd inside $MOUNT (e.g. launched from there) makes umount "target is busy"
sync
sudo umount "$MOUNT" && echo "==> unmounted $MOUNT — safe to unplug" \
|| echo "!! umount failed — run: sudo umount $MOUNT" >&2
fi
exit "$rc"
}
trap cleanup EXIT
# 0. Preflight
preflight() {
(( EUID != 0 )) || die "run as the admin user, not root (sudo is called where needed)"
local c
for c in docker sqlite3 sha256sum tar findmnt; do
command -v "$c" >/dev/null || die "missing command: $c"
done
[[ -d $STACK/gitea-data ]] || die "no gitea-data in $STACK"
docker inspect gitea >/dev/null 2>&1 || die "no gitea container — is the stack up?"
}
# 1–2. Find the HDD by label and mount it with the kernel ntfs3 driver
mount_hdd() {
local dev=/dev/disk/by-label/$LABEL
[[ -e $dev ]] || die "HDD '$LABEL' not attached. Check: lsblk -o NAME,SIZE,FSTYPE,LABEL,TRAN"
dev=$(readlink -f "$dev")
if findmnt -rn "$MOUNT" >/dev/null; then
local src
src=$(findmnt -rno SOURCE "$MOUNT")
[[ $src == "$dev" ]] || die "$MOUNT already holds $src, not $LABEL ($dev)"
log "$LABEL ($dev) already mounted at $MOUNT — leaving it mounted afterwards"
return
fi
log "mounting $LABEL ($dev) at $MOUNT"
sudo mkdir -p "$MOUNT"
if ! sudo mount -t ntfs3 "$dev" "$MOUNT"; then
sudo dmesg | tail -n 20 | grep -q 'volume is dirty' \
|| die "mount failed and the volume is not merely dirty — see: sudo dmesg | tail"
# Windows left the dirty flag (unsafe eject / Fast Startup). Never mount -o force.
command -v ntfsfix >/dev/null || die "volume is dirty; install ntfsprogs (ntfsfix is not in ntfs-3g)"
log "volume is dirty — dry run: ntfsfix -n"
sudo ntfsfix -n "$dev" || die "ntfsfix dry run found problems — run chkdsk /f on Windows instead"
local answer
read -rp "Dry run clean. Clear the dirty flag with 'ntfsfix -d'? [y/N] " answer
[[ $answer == [yY] ]] || die "aborted — volume left dirty and unmounted"
sudo ntfsfix -d "$dev"
sudo mount -t ntfs3 "$dev" "$MOUNT"
fi
mounted_by_us=1
}
# 3–6. Stop Gitea, archive gitea-data, start Gitea, write checksum
backup() {
local dir=$MOUNT/$DEST_DIR name
name=gitea-backup-$(date +%Y%m%d-%H%M).tar.gz
archive=$dir/$name
[[ ! -e $archive ]] || die "$archive already exists"
sudo mkdir -p "$dir"
local need have
need=$(sudo du -sb "$STACK/gitea-data" | cut -f1)
have=$(df --output=avail -B1 "$MOUNT" | tail -n1)
(( have > need )) || die "not enough space on $LABEL: need $need bytes, have $have"
log "stopping gitea (SQLite must not be written mid-copy)"
cd "$STACK"
gitea_stopped=1
docker compose stop gitea
local t0=$SECONDS
# tar, not cp -r: NTFS cannot hold Linux owners/modes, and gitea-data/ssh
# (host keys) is root:root 700. -p keeps them inside the archive.
log "archiving gitea-data -> $archive"
partial_archive=$archive
sudo tar -czpf "$archive" -C "$STACK" gitea-data
log "starting gitea"
docker compose start gitea
gitea_stopped=0
echo "gitea was down $((SECONDS - t0))s"
log "writing checksum"
(cd "$dir" && sudo sh -c 'sha256sum "$1" > "$1.sha256"' sh "$name")
sync
partial_archive=
}
# 7–12. Restore test: extract to scratch, check DB + repos, boot a throwaway Gitea
verify() {
local archive=$1
[[ $archive == */* ]] || archive=$MOUNT/$DEST_DIR/$archive
[[ -f $archive && -f $archive.sha256 ]] || die "need $archive and $archive.sha256"
local dir name
dir=$(dirname "$archive") name=$(basename "$archive")
log "7. checksum"
(cd "$dir" && sha256sum -c "$name.sha256")
log "8. archive contents"
echo "entries: $(tar -tzf "$archive" | wc -l)"
tar -tvzf "$archive" | grep -E '^drwx------ root/root .* gitea-data/ssh/$' >/dev/null \
|| die "gitea-data/ssh/ missing or lost its root:root 700 — archive not restorable as-is"
echo "gitea-data/ssh/ present, root:root 700"
log "9. extracting to $SCRATCH"
[[ ! -e $SCRATCH ]] || die "$SCRATCH exists — inspect and remove it first"
mkdir "$SCRATCH"
scratch_ours=1
tar -xzf "$archive" -C "$SCRATCH"
local r=$SCRATCH/gitea-data
[[ -f $r/gitea/gitea.db && -d $r/git/repositories ]] || die "extract lacks gitea.db or repositories"
log "10. SQLite integrity"
local ok
ok=$(sqlite3 -readonly "$r/gitea/gitea.db" 'PRAGMA integrity_check;')
[[ $ok == ok ]] || die "gitea.db integrity_check: $ok"
echo "integrity_check: ok"
sqlite3 -readonly -list -noheader "$r/gitea/gitea.db" \
"select 'users=' || count(*) from user; select 'repos=' || count(*) from repository;"
# The server has no git; the gitea image does.
log "11. git fsck every repo (inside the gitea image, read-only mount)"
local img
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; fail=0
for d in */*.git; do
if out=$(git -c safe.directory="*" --git-dir="$d" fsck --no-progress --no-dangling 2>&1); then
printf "%-36s %s\n" "$d" "${out:-OK}"
else
printf "%-36s FAILED\n%s\n" "$d" "$out"; fail=1
fi
done
exit $fail' || die "git fsck failed on at least one repo"
echo
if diff -rq "$r/git" "$STACK/gitea-data/git" >/dev/null; then
echo "repos identical to live gitea-data/git"
else
echo "note: repos differ from live — expected if anything was pushed since this backup"
fi
log "12. boot a throwaway Gitea on the extract (--network none, live stack untouched)"
docker run -d --rm --name "$TEST_CONTAINER" --network none \
-e USER_UID=1000 -e USER_GID=1000 -v "$r:/data" "$img" >/dev/null
local i version=
for i in $(seq 1 30); do
version=$(docker exec "$TEST_CONTAINER" wget -qO- http://127.0.0.1:3000/api/v1/version 2>/dev/null) && break
sleep 2
done
[[ -n $version ]] || { docker logs --tail 20 "$TEST_CONTAINER"; die "restored Gitea did not answer within 60s"; }
echo "restored Gitea answers: $version"
docker exec "$TEST_CONTAINER" su git -c 'gitea admin user list'
docker stop "$TEST_CONTAINER" >/dev/null
log "13. cleanup: removing $SCRATCH (holds copies of the SSH host keys)"
rm -rf "$SCRATCH"
scratch_ours=0
log "VERIFIED: $archive"
}
# 14. Retention report — never deletes anything
retention() {
local dir=$MOUNT/$DEST_DIR n
log "backups on $LABEL"
ls -lh "$dir"
n=$(find "$dir" -maxdepth 1 -name 'gitea-backup-*.tar.gz' | wc -l)
(( n >= 2 )) || echo "note: only $n backup(s) on $LABEL — keep at least two"
}
archive=
case ${1:-} in
"")
preflight
sudo -v # ask for the password now, not during Gitea downtime
mount_hdd
backup
verify "$archive"
retention
;;
verify)
[[ -n ${2:-} ]] || usage
preflight
mount_hdd
verify "$2"
;;
*) usage ;;
esac