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