#!/usr/bin/env bash # # backup_create.sh # # Companion to setup-git-server.sh: creates ONE zip file that contains # everything needed to bring a Gitea instance installed by that script back # with backup_restore.sh: # # - the database (as a portable SQL dump, SQLite) # - all repositories # - data/ (LFS objects, attachments, avatars, packages, ...) # - custom/ (templates, logos, ...) # - app.ini (including SECRET_KEY / JWT secrets -- needed for 2FA etc.) # - gitea-backup-info.txt (Gitea version, date, host -- lets the restore # refuse a too-old Gitea *before* it touches anything) # # It uses Gitea's own `gitea dump`, so the archive is also a perfectly normal # dump that `gitea doctor`/manual restores understand. # # By default Gitea is stopped while the dump runs, so the database and the # repositories are consistent with each other, and started again right after # (also if something goes wrong). Use --no-stop to back up a running server. # # Usage: # sudo ./backup_create.sh [options] # # Run with --help for the full list of options. set -euo pipefail # ---------------------------------------------------------------------------- # Defaults (same variables as setup-git-server.sh, overridable via env vars) # ---------------------------------------------------------------------------- GITEA_USER="${GITEA_USER:-git}" GITEA_HOME="${GITEA_HOME:-/home/${GITEA_USER}}" GITEA_WORK_DIR="${GITEA_WORK_DIR:-/var/lib/gitea}" GITEA_CONFIG_DIR="${GITEA_CONFIG_DIR:-/etc/gitea}" GITEA_CONFIG="${GITEA_CONFIG_DIR}/app.ini" GITEA_BIN="/usr/local/bin/gitea" OUTPUT="" KEEP="" STOP_SERVICE="true" STAGE_BASE="${STAGE_BASE:-}" # Filled in while the script runs DISTRO_FAMILY="" GITEA_GROUP="" OUTPUT_DIR="" OUTPUT_FILE="" FINAL="" PARTIAL="" STAGE="" STOPPED="false" BACKUP_VERSION="" # ---------------------------------------------------------------------------- # Small helpers # ---------------------------------------------------------------------------- C_GREEN='\033[0;32m'; C_YELLOW='\033[0;33m'; C_RED='\033[0;31m'; C_RESET='\033[0m' log() { printf '%b\n' "${C_GREEN}[+]${C_RESET} $*"; } warn() { printf '%b\n' "${C_YELLOW}[!]${C_RESET} $*" >&2; } error_exit() { printf '%b\n' "${C_RED}[x]${C_RESET} $*" >&2; exit 1; } start_service_if_stopped() { # Idempotent: only acts if *this script* stopped the service. if [[ "$STOPPED" == "true" ]]; then STOPPED="false" log "Starting Gitea again..." systemctl start gitea \ || warn "Could not start Gitea -- start it by hand: systemctl start gitea" fi } cleanup() { local rc=$? # Whatever happened, don't leave the server down. start_service_if_stopped || true if [[ -n "${STAGE:-}" && -d "$STAGE" ]]; then rm -rf -- "$STAGE" fi if [[ -n "${PARTIAL:-}" ]]; then rm -f -- "$PARTIAL" fi if [[ $rc -ne 0 ]]; then warn "The backup did not complete; no backup file was written." fi } trap cleanup EXIT print_help() { cat <<'EOF' Usage: sudo ./backup_create.sh [options] Creates a single zip file with the whole Gitea instance (database, repositories, data, custom files, app.ini + secrets). Restore it with backup_restore.sh. Options: -o, --output Where to put the backup. A folder gets a file named gitea-backup---