This commit is contained in:
Andras Schmelczer 2026-09-27 19:17:25 +01:00
commit 814e18cf12
3 changed files with 37 additions and 24 deletions

View file

@ -16,27 +16,7 @@ source /src/borg-common.sh
archive_prefix=$(borg_literal_prefix archive "$ARCHIVE_PREFIX")
archive_glob=$(borg_literal_prefix glob "$ARCHIVE_PREFIX")
# Only a missing repository (modern exit code 13) triggers initialization.
info_status=0
borg_wait info || info_status=$?
case "$info_status" in
0) ;;
13)
echo "Repository does not exist. Initializing Borg..."
init_status=0
borg_wait init --encryption=repokey || init_status=$?
case "$init_status" in
0) ;;
# Another client may have initialized the repository in between.
10) borg_wait info ;;
*) exit "$init_status" ;;
esac
;;
*)
echo "Cannot access repository (Borg exit status $info_status). Skipping backup." >&2
exit "$info_status"
;;
esac
borg_ensure_repository
cleanup() {
local status=$?
@ -54,7 +34,16 @@ fi
/src/snapshot.sh create /btrfs-root /snapshot/btrfs-root
cd "/snapshot/btrfs-root${BACKUP_RELATIVE_PATH:-}"
case ${BACKUP_RELATIVE_PATH:-} in
''|/*) ;;
*) echo 'BACKUP_RELATIVE_PATH must start with /.' >&2; exit 1 ;;
esac
IFS= read -r -d '' backup_directory < <(realpath -ez -- "/snapshot/btrfs-root${BACKUP_RELATIVE_PATH:-}")
if [[ $backup_directory != /snapshot/btrfs-root && $backup_directory != /snapshot/btrfs-root/* ]]; then
echo 'BACKUP_RELATIVE_PATH must resolve to a directory within the snapshot.' >&2
exit 1
fi
cd -- "$backup_directory"
borg_wait create --stats \
--list \

View file

@ -2,6 +2,26 @@
export BORG_RSH='ssh -oBatchMode=yes'
borg_ensure_repository() {
local status=0
borg_wait info || status=$?
(( status != 0 )) || return 0
if (( status == 13 )) || (( status == 15 )); then
echo 'Repository is missing or confirmed empty. Initializing Borg...'
status=0
borg_wait init --encryption=repokey || status=$?
# Another client may have initialized it after the initial check.
if (( status == 10 )); then
borg_wait info
else
return "$status"
fi
else
echo "Cannot access repository (Borg exit status $status). Skipping backup." >&2
return "$status"
fi
}
# Borg expands placeholders in both names and archive filters. Only the filter
# needs glob escaping; user-supplied prefixes are literal in both places.
borg_literal_prefix() {