This commit is contained in:
Andras Schmelczer 2026-03-15 14:21:26 +00:00
parent c445ea9582
commit 99bf0f857f
3 changed files with 16 additions and 22 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
echo "Starting backup wrapper script at `date`" echo "Starting backup wrapper script at $(date)"
execute_script() { execute_script() {
echo "Executing script with:" echo "Executing script with:"
@ -28,18 +28,13 @@ configure_environment() {
done done
# optional variables # optional variables
for var in BORG_REMOTE_PATH; do local indexed_var_name="BORG_REMOTE_PATH_${index}"
local indexed_var_name="${var}_${index}" export BORG_REMOTE_PATH="${!indexed_var_name}"
export $var="${!indexed_var_name}"
done
[[ $all_vars_set == true ]] [[ $all_vars_set == true ]]
} }
main() { main() {
# Clear health log at start - only a fully successful run should be marked healthy
rm -f /health/backup_completion_time.log
if [ -n "$BORG_REPO" ]; then if [ -n "$BORG_REPO" ]; then
# fallback case if multi-target backup isn't needed # fallback case if multi-target backup isn't needed
if execute_script; then if execute_script; then
@ -74,4 +69,4 @@ main() {
main main
echo "Finished backup wrapper script at `date`" echo "Finished backup wrapper script at $(date)"

View file

@ -5,15 +5,14 @@ KEEP_WEEKLY=${KEEP_WEEKLY:-3}
KEEP_MONTHLY=${KEEP_MONTHLY:-48} KEEP_MONTHLY=${KEEP_MONTHLY:-48}
KEEP_YEARLY=${KEEP_YEARLY:-10} KEEP_YEARLY=${KEEP_YEARLY:-10}
echo "Starting backup script at `date`" echo "Starting backup script at $(date)"
export BORG_RSH='ssh -oBatchMode=yes' # https://borgbackup.readthedocs.io/en/stable/usage/notes.html#ssh-batch-mode export BORG_RSH='ssh -oBatchMode=yes' # https://borgbackup.readthedocs.io/en/stable/usage/notes.html#ssh-batch-mode
# break any stale locks in case the script was interrupted # break any stale locks in case the script was interrupted
borg break-lock borg break-lock
borg info # test whether we have a valid repository if ! borg info; then
if [ $? -ne 0 ]; then
echo "Borg info returned a non-zero status. Initializing Borg..." echo "Borg info returned a non-zero status. Initializing Borg..."
borg init --encryption=repokey borg init --encryption=repokey
fi fi
@ -32,18 +31,18 @@ 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)
if [ "${IGNORE_GIT_UNTRACKED:-false}" = "true" ]; then if [ "${IGNORE_GIT_UNTRACKED:-false}" = "true" ]; then
echo "Generating exclusions for git-untracked files..." echo "Generating exclusions for git-untracked files..."
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 gitdir; do find . -name .git -type d 2>/dev/null | 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 file; do git ls-files --others --exclude-standard 2>/dev/null | 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
@ -53,7 +52,7 @@ if [ "${IGNORE_GIT_UNTRACKED:-false}" = "true" ]; then
excluded_count=$(wc -l < "$GIT_EXCLUDE_FILE") excluded_count=$(wc -l < "$GIT_EXCLUDE_FILE")
echo "Found $excluded_count git-untracked files to exclude" echo "Found $excluded_count git-untracked files to exclude"
EXCLUDE_ARGS="$EXCLUDE_ARGS --exclude-from $GIT_EXCLUDE_FILE" EXCLUDE_ARGS+=(--exclude-from "$GIT_EXCLUDE_FILE")
fi fi
borg create --stats \ borg create --stats \
@ -61,7 +60,7 @@ borg create --stats \
--filter=AMCE \ --filter=AMCE \
--files-cache=ctime,size,inode \ --files-cache=ctime,size,inode \
--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 # Clean up temporary exclude file
if [ -n "$GIT_EXCLUDE_FILE" ] && [ -f "$GIT_EXCLUDE_FILE" ]; then if [ -n "$GIT_EXCLUDE_FILE" ] && [ -f "$GIT_EXCLUDE_FILE" ]; then
@ -71,10 +70,10 @@ fi
cd - cd -
borg prune --list --stats \ borg prune --list --stats \
--keep-daily=$KEEP_DAILY \ --keep-daily="$KEEP_DAILY" \
--keep-weekly=$KEEP_WEEKLY \ --keep-weekly="$KEEP_WEEKLY" \
--keep-monthly=$KEEP_MONTHLY \ --keep-monthly="$KEEP_MONTHLY" \
--keep-yearly=$KEEP_YEARLY --keep-yearly="$KEEP_YEARLY"
borg compact --threshold=5 --cleanup-commits --verbose --progress borg compact --threshold=5 --cleanup-commits --verbose --progress

View file

@ -7,7 +7,7 @@ if [ -f /health/backup_completion_time.log ]; then
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