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