btrbk
btrbk copied to clipboard
Add option for dynamic snapshot_name (in conjunction with wildcards)
Wildcards are not very useful if the subvolume names are identical.
E.g. lxc containers are created as <container_name>/rootfs, which results in all btrbk snapshots named rootfs when configuring subvolume */rootfs.
See #71 (this comment)
See discussion: #230 Subvolume wildcards are not recursive
This goal can also be accomplished by dynamically generating the static BTRBK configuration file. Here is the proof of concept script that I'm currently using, gen-config.sh:
#!/bin/bash
set -eu
config=${1:-}
[[ -f $config ]] || { \
cat << EOL
Description:
Generates BTRBK config file by adding subvolumes into the
configuration under \$subvolume.
Required \$snapshot_dir's are created automatically.
Usage:
$(basename $0) path/to/orig.config > generated.config
Then use the generated.config as usual.
EOL
exit 1;
}
errcho(){ >&2 echo -e "$@"; }
btrfs_ls(){
sudo ./btrbk ls "$1" | tail -n+2 | awk '{print $4}' | sort -r
}
# Print the original config
cat $config
echo
# Print generated config
while read key value; do
case $key in
snapshot_dir|volume|subvolume|target)
declare $key=$value
;;
esac
done < $config
# TODO: assign them to an array
src="$volume/$subvolume"
dest="$volume/$snapshot_dir"
target=$target
exclude_list=("tmp")
btrfs_ls "$src" | while read sub; do
[[ "$(basename $sub)" == "tmp" ]] && { errcho "Skipping $sub..."; continue; }
rel=$(readlink -m "${sub#${src%/}}")
new_snapshot_dir=$(readlink -m $dest/$(dirname $rel))
#echo will backup $sub like $new_snapshot_dir/$(basename $sub).1234567890;
_subvolume="${sub#$volume/}"
_snapshot_dir="${new_snapshot_dir#$volume/}"
_target="$target/$(dirname $rel)"
if [[ ! -d "$new_snapshot_dir" ]]; then
errcho "Creating $new_snapshot_dir"
sudo mkdir -p "$new_snapshot_dir"
fi
if [[ -d "$target" && ! -d "$_target" ]]; then
errcho "Creating $_target"
sudo mkdir -p "$_target"
fi
# Generate actual BTRBK configuration
echo "volume $volume"
echo " subvolume $_subvolume"
echo " snapshot_dir $_snapshot_dir"
echo " target $_target"
echo
done
Changelog
- Fixed "do not generate snapshot_dir's if they exist" logic.
This script generates a configuration from:
timestamp_format long
snapshot_preserve_min 2d
snapshot_preserve 14d
target_preserve_min 5h
target_preserve 20d 10w *m
snapshot_dir snapshots/erik3
volume /mnt/heybe-root
target /mnt/masa-root/snapshots/erik3
subvolume rootfs
...the following one:
timestamp_format long
snapshot_preserve_min 2d
snapshot_preserve 14d
target_preserve_min 5h
target_preserve 20d 10w *m
snapshot_dir snapshots/erik3
volume /mnt/heybe-root
target /mnt/masa-root/snapshots/erik3
subvolume rootfs
volume /mnt/heybe-root
subvolume rootfs/var/lib/lxc/fc4/rootfs
snapshot_dir snapshots/erik3/var/lib/lxc/fc4
target /mnt/masa-root/snapshots/erik3//var/lib/lxc/fc4
volume /mnt/heybe-root
subvolume rootfs/var/lib/lxc/couchdb-test/rootfs
snapshot_dir snapshots/erik3/var/lib/lxc/couchdb-test
target /mnt/masa-root/snapshots/erik3//var/lib/lxc/couchdb-test
volume /mnt/heybe-root
subvolume rootfs/var/lib/lxc/couchdb-erik/rootfs
snapshot_dir snapshots/erik3/var/lib/lxc/couchdb-erik
target /mnt/masa-root/snapshots/erik3//var/lib/lxc/couchdb-erik
volume /mnt/heybe-root
subvolume rootfs/var/cache/lxc/debian/rootfs-stretch-amd64
snapshot_dir snapshots/erik3/var/cache/lxc/debian
target /mnt/masa-root/snapshots/erik3//var/cache/lxc/debian
volume /mnt/heybe-root
subvolume rootfs/var/cache/lxc/debian/rootfs-buster-amd64
snapshot_dir snapshots/erik3/var/cache/lxc/debian
target /mnt/masa-root/snapshots/erik3//var/cache/lxc/debian
volume /mnt/heybe-root
subvolume rootfs/home/ceremcem/foo/bar
snapshot_dir snapshots/erik3/home/ceremcem/foo
target /mnt/masa-root/snapshots/erik3//home/ceremcem/foo