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