summaryrefslogtreecommitdiff
path: root/src/push-btrfs-scan
blob: 06c5d429b068cea36d861dabe053e7dfe8db5885 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/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
    "$@"
}

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

filter_readonly()
{
    if btrfs subvolume show -r "$id" / | sed -Ene '/^\tFlags:.*\breadonly\b/q' -e '$q1'
    then
        if [ -e "$conf_name" ]
        then
            printf '%20s %s\n' rm "$conf_name" >&2
            rm "$conf_name"
        fi
        printf '%20s %s\n' '(readonly) lose' "$path_fixed" >&2
        return
    fi
    "$@"
}

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

systemctl_disable_quiet()
{
    ! systemctl is-enabled "$@" 2>/dev/null || systemctl disable "$@"
}

timer_prefix=/etc/systemd/system/default.target.wants/push-btrfs@borges
unset blacklist_patterns
declare -a blacklist_patterns
filter_blacklist()
{
    if [ "$blacklist" -a -f "$blacklist" ]
    then
        if ! [ -v blacklist_patterns ]
        then
            readarray -t blacklist_patterns < "$blacklist"
        fi

        for pattern in "${blacklist_patterns[@]}"
        do
            if [[ $path_fixed == $pattern ]]
            then
                printf '%20s %s\n' '(blacklist) lose' "$path_fixed" >&2
                if [ -e "$conf_name" ]
                then
                    printf '%20s %s\n' rm "$conf_name" >&2
                    rm "$conf_name"
                fi
                timer=$destination_host$(systemd-escape "$path_fixed").timer
                systemctl_disable_quiet "$timer"
                return
            fi
        done
    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 \
                compute_paths \
                filter_snapshot_filename \
                filter_readonly \
                filter_blacklist \
                mirror