Skip to content

Instantly share code, notes, and snippets.

@xemle
Created July 23, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xemle/4ac5c5d65d22b828198c32b0ac58a07c to your computer and use it in GitHub Desktop.
Save xemle/4ac5c5d65d22b828198c32b0ac58a07c to your computer and use it in GitHub Desktop.
Reorder borg (borgbackup) segments if core repository config segments_per_dir was changed
#!/bin/sh
if [ -z "$BORG_REPO" ]; then
echo "BORG_REPO not set. Export variable like:"
echo "export BORG_REPO=/mnt/borg"
exit 1
fi
BORG_REPO=$(readlink -f "$BORG_REPO")
if [ ! -e "$BORG_REPO/config" ]; then
echo "Invalid BORG_REPO dir: $BORG_REPO"
exit 1
fi
SEGMENTS_PER_DIR=$(cat "$BORG_REPO/config" | grep segments_per_dir | sed 's/\s//g' | cut -d'=' -f2)
if [ -z "$SEGMENTS_PER_DIR" -o "$SEGMENTS_PER_DIR" -le 0 ]; then
echo "Could not read segments_per_dir"
exit 1
fi
echo "Migrating borg repository $BORG_REPO with $SEGMENTS_PER_DIR segments per dir"
DATA_DIR="$BORG_REPO/data";
find "$DATA_DIR" -type f | while read SEGMENT; do
SEGMENT_ID=$(basename $SEGMENT)
ORIG_DIR=$(basename $(dirname $SEGMENT))
DIR=$(($SEGMENT_ID / $SEGMENTS_PER_DIR))
if [ "$ORIG_DIR" != "$DIR" ]; then
if [ ! -d "$DATA_DIR/$DIR" ]; then
mkdir "$DATA_DIR/$DIR"
fi
echo "Move segment $ORIG_DIR/$SEGMENT_ID -> $DIR/$SEGMENT_ID"
mv "$DATA_DIR/$ORIG_DIR/$SEGMENT_ID" "$DATA_DIR/$DIR/$SEGMENT_ID"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment