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