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