Skip to content

Instantly share code, notes, and snippets.

@CLandel89
Last active March 9, 2020 10:05
Show Gist options
  • Save CLandel89/ebbea57224b57eae750b7ced0bb005dc to your computer and use it in GitHub Desktop.
Save CLandel89/ebbea57224b57eae750b7ced0bb005dc to your computer and use it in GitHub Desktop.
Bash script for use with borgbackup: Creates a structured symlink tree to the archives in a borg repo.
#!/bin/bash -e
## borg-tree
# With this Bash script, you can create backups like this (note the different types of slashes!):
# # borg create '/path/to/repo::category\subdir\2018-04-15'
# Create the symlink tree like this:
# # borg-tree /path/to/repo /path/for/browsing
# And then browse the archives in paths like this:
# # /path/for/browsing/category/subdir/2018-04-15
# Note that the latter is a SYMLINK; command-line tools like cp should be used with caution.
# Also note that I cannot be held responsible for any loss resulting from using this script,
# so use with caution and at your own risk.
function log () {
printf '%s\n' "$1"
}
if [[ -z "$2" || -n "$3" ]] ; then
log 'Please pass 2 arguments: <borg repo> <mount and link target>'
log 'The target directory will be created automatically, and the repo will be mounted in target/\mnt.'
exit -1
fi
WORKDIR="$(pwd)"
REPO="$1"
if [[ "${2:0:1}" == / ]] ; then
TARGET="$2"
else
TARGET="$WORKDIR"/"$2"
fi
mkdir -p "$TARGET"/\\mnt
borg mount "$REPO" "$TARGET"/\\mnt
cd "$TARGET"/\\mnt
for arch in * ; do
cd "$WORKDIR"
arch_slash="$(printf '%s' "$arch" | sed 's,\\,/,g')"
mkdir -p "$TARGET"/"$arch_slash"
rmdir "$TARGET"/"$arch_slash"
ln -s "$TARGET"/\\mnt/"$arch" "$TARGET"/"$arch_slash"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment