Compare commits

...

6 commits

Author SHA1 Message Date
31bc548fdd Lint
Some checks failed
ShellCheck / shellcheck (push) Successful in 48s
Docker / build (push) Failing after 28m4s
2026-03-22 17:41:39 +00:00
fd581ef877 Always clean up 2026-03-22 17:41:30 +00:00
bfea98db61 Don't start unhealthy 2026-03-22 17:40:57 +00:00
8bc697a7f4 Migrate to forgejo 2026-03-22 17:40:02 +00:00
5d49069b7b Safer default 2026-03-22 17:39:50 +00:00
202ea7356c Small clean up 2026-03-22 17:39:45 +00:00
9 changed files with 110 additions and 19 deletions

View file

@ -0,0 +1,58 @@
name: Docker
on:
push:
branches: [ "main" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]
env:
REGISTRY: ${{ forgejo.server_url }}
IMAGE_NAME: ${{ forgejo.repository }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: https://code.forgejo.org/actions/checkout@v4
# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: https://github.com/docker/setup-buildx-action@v3
# Login against the Forgejo container registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: forgejo.event_name != 'pull_request'
uses: https://github.com/docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ forgejo.actor }}
password: ${{ secrets.FORGEJO_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: https://github.com/docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: https://github.com/docker/build-push-action@v5
with:
context: .
push: ${{ forgejo.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

View file

@ -0,0 +1,19 @@
name: ShellCheck
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
shellcheck:
runs-on: ubuntu-latest
steps:
- uses: https://code.forgejo.org/actions/checkout@v4
- name: Install ShellCheck
run: apt-get update && apt-get install -y shellcheck
- name: Run ShellCheck
run: find ./src -name '*.sh' -exec shellcheck {} +

View file

@ -19,7 +19,7 @@ Over the past 2 years, this backup setup has enabled me to successfully restore
- **Scheduled Backups**: Automates backups according to a defined schedule. - **Scheduled Backups**: Automates backups according to a defined schedule.
- **Log Rotation**: Maintains weekly logs of all backup activities. - **Log Rotation**: Maintains weekly logs of all backup activities.
- **Multi-Repository Backups**: Allows backups to multiple BorgBackup repositories simultaneously. - **Multi-Repository Backups**: Allows backups to multiple BorgBackup repositories simultaneously.
- **Healtcheck**: The healthcheck is based on the time of the last successful backup. - **Healthcheck**: The healthcheck is based on the time of the last successful backup.
### Multi-target backups ### Multi-target backups

View file

@ -1,4 +1,4 @@
Host * Host *
StrictHostKeyChecking no StrictHostKeyChecking accept-new
ServerAliveInterval 30 ServerAliveInterval 30
ServerAliveCountMax 3 ServerAliveCountMax 3

View file

@ -3,7 +3,6 @@ services:
image: ghcr.io/schmelczer/backup-container:latest image: ghcr.io/schmelczer/backup-container:latest
container_name: backup container_name: backup
init: true init: true
tty: true
environment: environment:
- TZ=Europe/London - TZ=Europe/London
- SLEEP_TIME=1h # we will wait an hour between backups - SLEEP_TIME=1h # we will wait an hour between backups

View file

@ -20,7 +20,7 @@ configure_environment() {
for var in BORG_PASSPHRASE BORG_REPO; do for var in BORG_PASSPHRASE BORG_REPO; do
local indexed_var_name="${var}_${index}" local indexed_var_name="${var}_${index}"
if [[ -n "${!indexed_var_name}" ]]; then if [[ -n "${!indexed_var_name}" ]]; then
export $var="${!indexed_var_name}" export "$var"="${!indexed_var_name}"
else else
all_vars_set=false all_vars_set=false
break break

View file

@ -22,13 +22,24 @@ fi
# we want to stop the script immediately. # we want to stop the script immediately.
set -e set -e
cleanup() {
if [ -n "${GIT_EXCLUDE_FILE:-}" ] && [ -f "$GIT_EXCLUDE_FILE" ]; then
rm -f "$GIT_EXCLUDE_FILE"
fi
if [ -d "/snapshot/btrfs-root" ]; then
cd /
btrfs subvolume delete /snapshot/btrfs-root || true
fi
}
trap cleanup EXIT
if [ -d "/snapshot/btrfs-root" ]; then if [ -d "/snapshot/btrfs-root" ]; then
btrfs subvolume delete /snapshot/btrfs-root btrfs subvolume delete /snapshot/btrfs-root
fi fi
btrfs subvolume snapshot /btrfs-root /snapshot btrfs subvolume snapshot /btrfs-root /snapshot
cd "/snapshot/btrfs-root$BACKUP_RELATIVE_PATH" cd "/snapshot/btrfs-root${BACKUP_RELATIVE_PATH:-}"
# Generate exclusions for git-untracked files if enabled # Generate exclusions for git-untracked files if enabled
EXCLUDE_ARGS=(--exclude-from /exclude.conf) EXCLUDE_ARGS=(--exclude-from /exclude.conf)
@ -37,12 +48,12 @@ if [ "${IGNORE_GIT_UNTRACKED:-false}" = "true" ]; then
GIT_EXCLUDE_FILE=$(mktemp) GIT_EXCLUDE_FILE=$(mktemp)
# Find all git repositories and list their untracked files # Find all git repositories and list their untracked files
find . -name .git -type d 2>/dev/null | while read -r gitdir; do find . -name .git -type d | while read -r gitdir; do
repo_dir=$(dirname "$gitdir") repo_dir=$(dirname "$gitdir")
( (
cd "$repo_dir" cd "$repo_dir"
# Get untracked files (respecting .gitignore) # Get untracked files (respecting .gitignore)
git ls-files --others --exclude-standard 2>/dev/null | while read -r file; do git ls-files --others --exclude-standard | while read -r file; do
# Output path relative to backup root # Output path relative to backup root
echo "${repo_dir#./}/$file" echo "${repo_dir#./}/$file"
done done
@ -62,11 +73,6 @@ borg create --stats \
--compression=zstd,12 \ --compression=zstd,12 \
"${EXCLUDE_ARGS[@]}" ::"{hostname}-{now:%Y-%m-%dT%H:%M:%S}" . "${EXCLUDE_ARGS[@]}" ::"{hostname}-{now:%Y-%m-%dT%H:%M:%S}" .
# Clean up temporary exclude file
if [ -n "$GIT_EXCLUDE_FILE" ] && [ -f "$GIT_EXCLUDE_FILE" ]; then
rm -f "$GIT_EXCLUDE_FILE"
fi
cd - cd -
borg prune --list --stats \ borg prune --list --stats \
@ -76,6 +82,3 @@ borg prune --list --stats \
--keep-yearly="$KEEP_YEARLY" --keep-yearly="$KEEP_YEARLY"
borg compact --threshold=5 --cleanup-commits --verbose --progress borg compact --threshold=5 --cleanup-commits --verbose --progress
btrfs subvolume delete /snapshot/btrfs-root

View file

@ -2,15 +2,26 @@
set -e set -e
MAX_BACKUP_AGE_SECONDS="${MAX_BACKUP_AGE_SECONDS:-86400}"
if [ -f /health/backup_completion_time.log ]; then if [ -f /health/backup_completion_time.log ]; then
current_time=$(date +%s) current_time=$(date +%s)
backup_time=$(date --file /health/backup_completion_time.log +%s) backup_time=$(date --file /health/backup_completion_time.log +%s)
age_in_seconds=$((current_time - backup_time)) age_in_seconds=$((current_time - backup_time))
if [ ${age_in_seconds} -lt "${MAX_BACKUP_AGE_SECONDS}" ]; then if [ "${age_in_seconds}" -lt "${MAX_BACKUP_AGE_SECONDS}" ]; then
echo "Backup completed within the last ${MAX_BACKUP_AGE_SECONDS} seconds. Healthcheck passed." echo "Backup completed within the last ${MAX_BACKUP_AGE_SECONDS} seconds. Healthcheck passed."
exit 0 exit 0
fi fi
elif [ -f /health/container_start_time.log ]; then
current_time=$(date +%s)
start_time=$(date --file /health/container_start_time.log +%s)
age_in_seconds=$((current_time - start_time))
if [ "${age_in_seconds}" -lt "${MAX_BACKUP_AGE_SECONDS}" ]; then
echo "No backup completed yet, but container started less than ${MAX_BACKUP_AGE_SECONDS} seconds ago. Healthcheck passed."
exit 0
fi
fi fi
echo "Backup not completed within the last ${MAX_BACKUP_AGE_SECONDS} seconds. Healthcheck failed." echo "Backup not completed within the last ${MAX_BACKUP_AGE_SECONDS} seconds. Healthcheck failed."

View file

@ -11,9 +11,10 @@ get_log_file_name() {
} }
echo "Starting schedule script at $(date)" | log_message echo "Starting schedule script at $(date)" | log_message
date > /health/container_start_time.log
while true; do while true; do
exec /src/backup-wrapper.sh 2>&1 | log_message /src/backup-wrapper.sh 2>&1 | log_message
echo "Sleeping for $SLEEP_TIME" | log_message echo "Sleeping for $SLEEP_TIME" | log_message
# Using a simple sleep loop to schedule backups instead of cron to avoid concurrency issues # Using a simple sleep loop to schedule backups instead of cron to avoid concurrency issues