-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrbk-gen-config
executable file
·76 lines (59 loc) · 1.9 KB
/
btrbk-gen-config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
_sdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
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 "$@" || true; }
# 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")
$_sdir/btrfs-ls --only-rw "$src" | while read sub; do
[[ "$(basename $sub)" == "tmp" ]] && { errcho "Skipping $sub..."; continue; }
[[ "$src" == "$sub" ]] && continue
rel=$(readlink -m "${sub#${src%/}}" || echo "")
[[ -z ${rel:-} ]] && { errcho "ERROR: Empty rel for: $sub minus $src"; exit 1; }
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/}"
[[ -n $target ]] && _target="$target/$(dirname $rel)"
if [[ ! -d "$new_snapshot_dir" ]]; then
errcho "Creating $new_snapshot_dir"
sudo mkdir -p "$new_snapshot_dir"
fi
if [[ -n "$target" && -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"
[[ -n $target ]] && \
echo " target $_target"
echo
done