From 1b95abb89ffcc710a84d0e6d5d269a5f254a200d Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Thu, 23 May 2024 08:04:58 +0100 Subject: [PATCH] Add source --- src/backup-wrapper.sh | 59 +++++++++++++++++++++++++++++++++++++++++++ src/backup.sh | 31 +++++++++++++++++++++++ src/schedule.sh | 20 +++++++++++++++ 3 files changed, 110 insertions(+) create mode 100755 src/backup-wrapper.sh create mode 100755 src/backup.sh create mode 100755 src/schedule.sh diff --git a/src/backup-wrapper.sh b/src/backup-wrapper.sh new file mode 100755 index 0000000..6fb9fb0 --- /dev/null +++ b/src/backup-wrapper.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +echo "Starting backup wrapper script at `date`" + +execute_script() { + echo "Executing script with:" + echo "BORG_PASSPHRASE=" + echo "BORG_REMOTE_PATH='${BORG_REMOTE_PATH}'" + echo "BORG_REPO='${BORG_REPO}'" + /src/backup.sh +} + +configure_environment() { + local index=$1 + + local all_vars_set=true + # required variables + for var in BORG_PASSPHRASE BORG_REPO; do + local indexed_var_name="${var}_${index}" + if [[ -n "${!indexed_var_name}" ]]; then + export $var="${!indexed_var_name}" + else + all_vars_set=false + break + fi + done + + # optional variables + for var in BORG_REMOTE_PATH; do + local indexed_var_name="${var}_${index}" + export $var="${!indexed_var_name}" + done + + [[ $all_vars_set == true ]] +} + +main() { + if [ -n "$BORG_PASSPHRASE" ] && [ -n "$BORG_REPO" ]; then + execute_script + else + local index=0 + local configurations_found=false + + while configure_environment $index; do + execute_script || true + configurations_found=true + unset BORG_PASSPHRASE BORG_REMOTE_PATH BORG_REPO + ((index++)) + done + + if [[ $configurations_found == false ]]; then + echo "No valid configuration found. Please ensure environment variables are set properly." + fi + fi +} + +main + +echo "Finished backup wrapper script at `date`" diff --git a/src/backup.sh b/src/backup.sh new file mode 100755 index 0000000..3a99f80 --- /dev/null +++ b/src/backup.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +echo "Starting backup script at `date`" + +export BORG_RSH='ssh -oBatchMode=yes' # https://borgbackup.readthedocs.io/en/stable/usage/notes.html#ssh-batch-mode + +borg break-lock +borg info + +if [ $? -ne 0 ]; then + echo "Borg info returned a non-zero status. Initializing Borg..." + borg init --encryption=repokey +fi + +set -e + +if [ -d "/snapshot/source" ]; then + btrfs subvolume delete /snapshot/source +fi + +btrfs subvolume snapshot /source /snapshot + +cd /snapshot/source +borg create --stats --list --filter=AMCE --files-cache=ctime,size --compression=zstd,12 --exclude-from /exclude.conf ::"{hostname}-{now:%Y-%m-%dT%H:%M:%S}" . +cd - +borg prune --list --stats --keep-daily=7 --keep-weekly=4 --keep-monthly=12 --keep-yearly=5 +borg compact --threshold=5 --cleanup-commits + +btrfs subvolume delete /snapshot/source + +echo "Finished backup script at `date`" diff --git a/src/schedule.sh b/src/schedule.sh new file mode 100755 index 0000000..f582022 --- /dev/null +++ b/src/schedule.sh @@ -0,0 +1,20 @@ +#!/bin/bash + + +SLEEP_TIME=${SLEEP_TIME:-1h} + +log_message() { + stdbuf -o0 tee -a "$(get_log_file_name)" +} + +get_log_file_name() { + echo "/backup-logs/backup_$(date +%Y)_week_$(date +%U).log" +} + +echo "Starting schedule script at `date`" | log_message + +while true; do + exec /src/backup-wrapper.sh 2>&1 | log_message + echo "Sleeping for $SLEEP_TIME" | log_message + sleep "$SLEEP_TIME" +done