add pull support, untested
[distro-setup] / btrbk-run
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 # todo: if we cancel in the middle of a btrfs send, then run again immediately, the received subvolume doesn't get a Received UUID: field, and we won't mount it. Need to figure out a solution that will fix this.
18
19 set -eE -o pipefail
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21
22 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
23
24 usage() {
25 cat <<'EOF'
26 btrbk-run [OPTIONS]
27 usually -t TARGET_HOST
28 EOF
29 echo "top of script file:"
30 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
31 exit $1
32 }
33
34 script_dir=$(dirname $(readlink -f "$BASH_SOURCE"))
35
36 # note q is owned by root:1000
37 # note p/m is owned 1000:1000 and chmod 700
38
39
40 mountpoints=()
41
42 rsync_mountpoint=/q
43
44 # default options
45 conf_only=false
46 dry_run=false # mostly for testing
47 rate_limit=no
48 verbose=true; verbose_arg=-v
49 progress_arg="--progress"
50
51 default_args_file=/etc/btrbk-run.conf
52 if [[ -s $default_args_file ]]; then
53 set -- $(< $default_args_file) "$@"
54 fi
55
56 temp=$(getopt -l help cl:m:npt:vh "$@") || usage 1
57 eval set -- "$temp"
58 while true; do
59 case $1 in
60 # only creates the config file, does not run btrbk
61 -c) conf_only=true; shift ;;
62 # bytes per second, suffix k m g
63 -l) rate_limit=$2; shift 2 ;;
64 # Comma separated mountpoints to backup. This has defaults set below.
65 -m) IFS=, mountpoints=($2); unset IFS; shift 2 ;;
66 -n) dry_run=true; dry_run_arg=-n; shift ;;
67 -p) progress_arg="--progress"; shift ;;
68 -q) verbose=false; verbose_arg=; progress_arg=; shift ;;
69 # source host to receive a backup from
70 -s) source=$2; shift 2 ;;
71 # target hosts to send to. empty is valid for just doing local
72 # snapshot. we have default hosts we will populate.
73 -t) IFS=, targets=($2); unset IFS; shift 2 ;;
74 -v) verbose=true; verbose_arg=-v; shift ;;
75 -h|--help) usage ;;
76 --) shift; break ;;
77 *) echo "$0: Internal error!" ; exit 1 ;;
78 esac
79 done
80
81 # usefull commands are resume and archive
82 cmd_arg=${1:-run}
83
84 if [[ -s $default_args_file ]]; then
85 echo "$0: warning: default btrbk-run options set in $default_args_file (sleeping 5 seconds):"
86 cat $default_args_file
87 sleep 5
88 fi
89
90 if [[ -v targets && -v source ]]; then
91 echo "$0: error: -t and -s are mutually exclusive" >&2
92 exit 1
93 fi
94
95 echo -e "$0: options: conf_only=$conf_only\ndry_run=$dry_run\nrate_limit=$rate_limit\nverbose=$verbose\ncmd_arg=$cmd_arg"
96
97 # set default targets
98 if [[ ! -v targets && ! -v source ]]; then
99 case $HOSTNAME in
100 x2|kw)
101 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
102 targets=($HOME_DOMAIN)
103 fi
104 ;;
105 tp)
106 targets=(frodo)
107 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
108 if timeout -s 9 10 ssh x2 :; then
109 targets+=(x2)
110 fi
111 fi
112 ;;
113 frodo)
114 targets=()
115 ;;
116 *)
117 echo "$0: error: no default targets for this host, use -t"
118 exit 1
119 ;;
120 esac
121 fi
122
123 if [[ -v targets ]]; then
124 echo "targets: ${targets[*]}"
125 fi
126
127 if [[ -v source ]]; then
128 echo "source: $source"
129 fi
130
131
132
133 # set default mountpoints
134 case $HOSTNAME in
135 # no remote backups atm. note, if we do enable this, configuration below will need some changes.
136 # frodo)
137 # prospective_mps=(/i)
138 # ;;
139 *)
140 prospective_mps=(/a /q)
141 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
142 prospective_mps+=(/o)
143 fi
144 ;;
145 esac
146 case ${targets[0]} in
147 kw|kww)
148 prospective_mps=(/a)
149 ;;
150 esac
151
152 for mp in ${prospective_mps[@]}; do # default mountpoints to sync
153 if [[ -e /nocow/btrfs-stale/$mp ]]; then
154 echo "$0: warning: $mp stale, not adding to default mountpoints"
155 continue
156 fi
157 if awk '{print $2}' /etc/fstab | grep -xF $mp &>/dev/null; then
158 mountpoints+=($mp)
159 fi
160 done
161
162 echo "mountpoints: ${mountpoints[*]}"
163
164 ##### end command line parsing ########
165
166 rsync-dirs() {
167 local host=$1
168 local path=$2
169 m rsync $dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
170 }
171
172
173 m() { printf "%s: %s\n" "${0##*/}" "$*"; "$@"; }
174
175
176 if ! which btrbk &>/dev/null; then
177 echo "$0: error: no btrbk binary found"
178 exit 1
179 fi
180 # if our mountpoints are from stale snapshots,
181 # it doesn't make sense to do a backup.
182 check-subvol-stale ${mountpoints[@]} || exit 1
183
184
185 cat >/etc/btrbk.conf <<EOF
186 ssh_identity /root/.ssh/home
187 # Just a guess that local7 is a good facility to pick.
188 # It's a bit odd that the transaction log has to be logged to
189 # a file or syslog, while other output is sent to std out.
190 # The man does not mention a way for them to be together, but
191 # I dunno if setting a log level like warn might also output
192 # transaction info.
193 transaction_syslog local7
194
195 # 20%ish speedup[]
196 stream_buffer 512m
197
198 # so we only run one at a time
199 lockfile /var/lock/btrbk.lock
200
201 # default format of short does not accomidate hourly preservation setting
202 timestamp_format long-iso
203
204 # only make a snapshot if things have changed
205 snapshot_create onchange
206 # I could make this different from target_preserve,
207 # if one disk had less space.
208 # for now, keeping them equal.
209 snapshot_preserve 36h 14d 8w 24m
210 snapshot_preserve_min 4h
211 snapshot_dir btrbk
212
213 # so, total backups = ~89
214 target_preserve 36h 14d 8w 24m
215 target_preserve_min 4h
216
217 # if something fails and it's not obvious, try doing
218 # btrbk -l debug -v dryrun
219
220 rate_limit $rate_limit
221 EOF
222
223
224
225 for tg in ${targets[@]:-localhost}; do
226 # for an initial run, btrbk requires the dir to exist.
227 ssh root@$tg mkdir -p /mnt/root/btrbk
228 done
229
230
231
232 vol=/mnt/root
233 for m in ${mountpoints[@]}; do
234 sub=${m##*/}
235 if [[ -v source ]]; then
236 cat >>/etc/btrbk.conf <<EOF
237 volume ssh://$source$vol/btrbk
238 subvolume $sub
239 target send-receive $vol/btrbk
240 EOF
241 else
242 cat >>/etc/btrbk.conf <<EOF
243 volume $vol
244 subvolume $sub
245 EOF
246 for tg in ${targets[@]}; do
247 cat >>/etc/btrbk.conf <<EOF
248 target send-receive ssh://$tg$vol/btrbk
249 EOF
250 done
251 fi
252 done
253
254
255 # todo: umount first to ensure we don't have any errors
256 # todo: do some kill fuser stuff to make umount more reliable
257
258
259 if $conf_only; then
260 exit
261 fi
262
263
264
265 if $dry_run; then
266 m btrbk -v -n $cmd_arg
267 elif [[ $cmd_arg == archive ]]; then
268 if [[ -v source ]]; then
269 m btrbk $verbose_arg $progress_arg $cmd_arg ssh://$source$vol $vol
270 else
271 for tg in ${targets[@]}; do
272 m btrbk $verbose_arg $progress_arg $cmd_arg $vol ssh://$tg$vol
273 done
274 fi
275 else
276 # -q and just using the syslog option seemed nice,
277 # but it doesn't show when a send has a parent and when it doesn't.
278 m btrbk $verbose_arg $progress_arg $cmd_arg
279 fi
280
281 # if we have it, sync to systems which don't
282 if mountpoint $rsync_mountpoint >/dev/null; then
283 for tg in ${targets[@]}; do
284 case $tg in
285 li|lk)
286 for x in /p/c/machine_specific/*.hosts; do
287 if grep -qxF $tg $x; then
288 dir=${x%.hosts}
289 rsync-dirs $tg $dir
290 fi
291 done
292 ;;
293 esac
294 done
295 fi
296
297 if ! $dry_run; then
298 # note, targets will be empty if source is set.
299 m $script_dir/mount-latest-remote ${targets[@]}
300 fi
301
302
303 # todo: move variable data we don't care about backing up
304 # to /nocow and symlink it.
305
306
307 # background on btrbk timezones. with short/long, timestamps use local time.
308 # for long, if your local time moves backwards, by moving timezones or
309 # for an hour when daylight savings changes it, you will temporarily get
310 # a more aggressive retention policy for the overlapping period, and
311 # vice versa for the opposite timezone move. The alternative is using
312 # long-iso, which puts timezone info into the timestamp, which means
313 # that instead of shifting time, you shift the start of day/week/month
314 # which is used for retention to your new local time, which means for
315 # example, if you moved forward by 8 hours, the daily/weekly/monthly
316 # retention will be 8 hours more aggressive since midnight is at a new
317 # time, unless you fake the timzeone using the TZ env variable.
318 # However, in the short term, there will be no inconsistencies.
319 # I don't see any problem with shifting when the day starts for
320 # retention, so I'm using long-iso.
321
322 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z