added backup script

This commit is contained in:
2026-02-25 01:47:35 +03:00
parent 9f36a48864
commit 9c0a3c19f6
4 changed files with 155 additions and 1 deletions

View File

@@ -0,0 +1,12 @@
# Copy to restic-backup.env and set values.
# restic-backup.env is not committed (add to .gitignore if needed).
# Repository: local path, sftp, rclone, or S3-compatible.
# Examples:
# RESTIC_REPOSITORY=/backup/restic
# RESTIC_REPOSITORY=sftp:user@backup-host:/restic
# RESTIC_REPOSITORY=s3:s3.amazonaws.com/bucket-name
export RESTIC_REPOSITORY=""
# Repository password (used for encryption).
export RESTIC_PASSWORD=""

57
scripts/restic-backup.sh Executable file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="${SCRIPT_DIR}/restic-backup.env"
HOMELAB_ROOT="/srv/homelab"
STACKS_STOPPED=false
start_stacks() {
if [[ "$STACKS_STOPPED" != true ]]; then
return 0
fi
echo "Starting Docker stacks..."
cd "$HOMELAB_ROOT"
docker compose -f stacks/media/compose.yaml up -d
docker compose -f stacks/monitoring/compose.yaml up -d
docker compose -f stacks/storage/compose.yaml up -d
echo "Stacks started."
}
trap start_stacks EXIT
if [[ -f "$ENV_FILE" ]]; then
set -a
# shellcheck source=/dev/null
source "$ENV_FILE"
set +a
fi
if [[ -z "${RESTIC_REPOSITORY:-}" ]]; then
echo "Error: RESTIC_REPOSITORY is not set. Create ${ENV_FILE} or export it." >&2
echo "Example: RESTIC_REPOSITORY=sftp:user@host:/backups/restic" >&2
exit 1
fi
if [[ -z "${RESTIC_PASSWORD:-}" ]]; then
echo "Error: RESTIC_PASSWORD is not set. Create ${ENV_FILE} or export it." >&2
exit 1
fi
CADDYFILE="/etc/caddy/Caddyfile"
UNBOUND_CONF="/etc/unbound/unbound.conf"
echo "Stopping Docker stacks..."
cd "$HOMELAB_ROOT"
docker compose -f stacks/media/compose.yaml down
docker compose -f stacks/monitoring/compose.yaml down
docker compose -f stacks/storage/compose.yaml down
STACKS_STOPPED=true
echo "Starting restic backup..."
restic backup \
"$HOMELAB_ROOT" \
"$CADDYFILE" \
"$UNBOUND_CONF"
echo "Backup finished. Snapshot list:"
restic snapshots --latest 5