Add source

This commit is contained in:
Andras Schmelczer 2024-05-23 08:04:58 +01:00
parent b781c17c5a
commit 1b95abb89f
3 changed files with 110 additions and 0 deletions

59
src/backup-wrapper.sh Executable file
View file

@ -0,0 +1,59 @@
#!/bin/bash
echo "Starting backup wrapper script at `date`"
execute_script() {
echo "Executing script with:"
echo "BORG_PASSPHRASE=<redacted>"
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`"

31
src/backup.sh Executable file
View file

@ -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`"

20
src/schedule.sh Executable file
View file

@ -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