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