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