diff options
author | Andrew Cady <d@jerkface.net> | 2023-05-21 17:20:21 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2023-05-21 17:20:21 -0400 |
commit | 8fb6bea29155b86138da4fc097e456f56124f053 (patch) | |
tree | d76059fd95800cec0b4dcf7ad8d8cfa3d374ef86 | |
parent | bc8450ee096c358c61f1911b677c9ff77df51913 (diff) |
use JSON config file instead of arguments
-rwxr-xr-x | go.sh | 60 |
1 files changed, 48 insertions, 12 deletions
@@ -1,20 +1,56 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | |||
3 | read_config() | ||
4 | { | ||
5 | eval config="($(jq -r '. | to_entries | .[] | "[\"" + .key + "\"]=" + (.value | @sh)'))" | ||
6 | } | ||
7 | |||
8 | check_dependencies() | ||
9 | { | ||
10 | for c in flock jq mountpoint btrfs pv | ||
11 | do | ||
12 | command -v $c >/dev/null | ||
13 | done | ||
14 | } | ||
15 | |||
16 | check_user_is_root() | ||
17 | { | ||
18 | [ "$UID" = 0 ] | ||
19 | } | ||
20 | |||
2 | set -e | 21 | set -e |
3 | wd=/root/src/local-btrfs-backup | 22 | check_dependencies |
4 | src=/ | 23 | check_user_is_root |
5 | dst=/srv/sdm1/borges | 24 | |
6 | parent_file=$wd/last-parent.txt | 25 | if [ $# != 1 ] |
26 | then | ||
27 | echo "Usage: $0 <config.json>" >&2 | ||
28 | exit 1 | ||
29 | fi | ||
30 | |||
31 | [ -r "$1" ] | ||
32 | exec 3<>"$1" | ||
33 | flock -n 3 | ||
34 | declare -A config | ||
35 | read_config <&3 | ||
7 | 36 | ||
8 | snapshot="${src%/}"/backup~$(date -Ins) | 37 | src=${config[source]} |
38 | dst=${config[destination]} | ||
39 | head=${config[head]} | ||
40 | new_snapshot=${src%/}/.snapshot~$(date -Ins) | ||
9 | 41 | ||
10 | set -x | 42 | mountpoint -q "$src" |
11 | if [ -e "$parent_file" ] | 43 | if [ "$head" ] |
12 | then | 44 | then |
13 | read parent < "$parent_file" | 45 | read parent_snapshot < "$head" |
46 | mountpoint -q "$parent_snapshot" | ||
47 | else | ||
48 | parent_snapshot= | ||
14 | fi | 49 | fi |
50 | mkdir -p "$dst" | ||
15 | 51 | ||
16 | btrfs subvolume snapshot -r "$src" "$snapshot" | 52 | btrfs subvolume snapshot -r "$src" "$new_snapshot" |
17 | btrfs send "$snapshot" ${parent:+ -p "$parent"} | pv | btrfs receive "$dst" | 53 | btrfs send "$new_snapshot" ${parent_snapshot:+ -p "$parent_snapshot"} | pv | btrfs receive "$dst" |
18 | printf "%s\n" "$snapshot" > "$parent_file"~tmp | 54 | printf "%s\n" "$new_snapshot" > "$head"~tmp |
19 | mv -T -- "$parent_file"~tmp "$parent_file" | 55 | mv -T -- "$head"~tmp "$head" |
20 | 56 | ||