summaryrefslogtreecommitdiff
path: root/src/btrfs-scan
blob: 2e83f1a787f4b072c4fc4987ec537fec54584af9 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
set -e -o pipefail

confdir=/etc/btrfs
blacklist=$confdir/blacklist.txt

with_subvolumes()
{
    btrfs subvolume list / -a |
        while read _ id _ gen _ _ toplevel _ path
        do
            "$@"
        done
}

fixup_path()
{
    if ! [ "$root" ]
    then
        case "$path" in
            '<FS_TREE>'/* ) root=${path#*/} ;;
            * ) exit 1 ;;
        esac
    fi
    case "$path" in
        '<FS_TREE>'/"$root"/* ) path_fixed=${path#*/$root} ;;
        "$root"/* ) path_fixed=${path#$root} ;;
        * ) return ;;
    esac
    compute_paths
    "$@"
}

is_our_snapshot()
{
  case "$1" in
    */.snapshot~* ) true ;;
    *) false ;;
  esac
}

filter_blacklist()
{
    if is_our_snapshot "$path_fixed"
    then
        if [ -e "$conf_name" ]
        then
            printf '%20s %s\n' delete "$conf_name" >&2
            printf '%20s %s\n' '(snapshot) lose' "$path_fixed" >&2
        fi
        return

    # Re-reading the blacklist file once per snapshot is not ideal,
    # but is fine.
    elif [ "$blacklist" -a -f "$blacklist" ]
    then
        while read pattern
        do
            if [[ $path_fixed == $pattern ]]
            then
                if [ -e "$conf_name" ]
                then
                    printf '%20s %s\n' delete "$conf_name" >&2
                fi
                printf '%20s %s\n' '(blacklist) lose' "$path_fixed" >&2
                return
            fi
        done < "$blacklist"
    fi
    "$@"
}

compute_paths()
{
    source=$path_fixed
    source_escaped=$(systemd-escape "$source")
    conf_dir=/etc/btrfs/remotes
    conf_name="$conf_dir"/$destination_host$source_escaped.json
}

mirror()
{
    if [ -e "$conf_name" ]
    then
        return
    fi
    set -- \
        jq -c -n '{source: $source, destination: $destination}' \
        --arg destination "$destination$source_escaped" \
        --arg source "$source"

    if [ ! "$NO_ACT" ]
    then
        conf_temp=$conf_name~$(date -Ins)
        {
            "$@" > "$conf_temp"
            mv -T -- "$conf_temp" "$conf_name"
            printf '%20s %s\n' 'add' "$path_fixed" >&2
        } || {
            rm -- "$conf_temp"
            false
        }
    fi

    if [ "$ONLY_ONCE" ]
    then
        exit 0
    fi
}

usage()
{
cat <<END
usage: $0 [-n] [-r]
END
}

case "$0" in
    ./* ) PATH=${0%/*}:$PATH ;;
esac

case "$(id -u)" in
          0 ) ;;
          * ) exec sudo --preserve-env=VERBOSE "$0" "$@" || exit ;;
esac

while [ $# -gt 0 ]
do
    case "$1" in
        -- ) shift
             break ;;
        -n ) NO_ACT=y ;;
        -r ) export RESUME=y ;;
        -v ) VERBOSE=y ;;
        --once ) ONLY_ONCE=y ;;
        --destination ) DESTINATION=$2
                        shift ;;
        -* ) usage
             exit 1 ;;
        * ) break ;;
    esac
    shift
done

destination_file=/etc/btrfs/destination.txt
[ "$DESTINATION" ] || read DESTINATION < "$destination_file"

destination_host=${DESTINATION%%:*}
destination_basename=$(hostname) && [ "$destination_basename" ] || destination_basename=btrfs
case "$DESTINATION" in
    */ ) destination=$DESTINATION$destination_basename ;;
    [a-zA-Z]* ) destination=$DESTINATION ;;
    * ) exit 1 ;;
esac

ssh root@"$destination_host" true || read -p 'Could not log in.  Continue? '

with_subvolumes fixup_path filter_blacklist mirror