host info updates
[distro-setup] / switch-mail-host
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23
24 set -e; . /usr/local/lib/bash-bear; set +e
25
26 usage() {
27 cat <<EOF
28 Usage: switch-mail-host|switch-host2 [OPTIONS] push|pull HOST
29
30 Turn off mail receiving on OLD_HOST, run btrbk to move mail to NEW_HOST,
31 turn on mail receiving on NEW_HOST. Assumes we want to move all
32 filesystems unless passing -o.
33
34 -a Avoid snapshot /a, /q, and similar. If we haven't
35 made any changes in the last hour, there is no
36 need to snapshot anything but /o, and we will
37 just do that once.
38 -i Disallow incremental backup.
39 -o Only btrbk /o, instead of all filesystems.
40 --force Run even though our local state does not say that MAIL_HOST is
41 us when pushing or HOST when pulling.
42 -h|--help Print help and exit.
43
44 I used to adjust home network dns so NEW_HOST resolves locally if it is
45 on the local network, but its simpler just not to and just rely
46 on the internet. Email can wait.
47
48 Note: Uses GNU getopt options parsing style
49 EOF
50 exit 0
51 }
52
53 script_name="${BASH_SOURCE[0]}"
54 script_name="${script_name##*/}"
55
56 restore_new_btrbk=false
57 restore_old_btrbk=false
58 err-cleanup() {
59 if $restore_new_btrbk; then
60 e WARNING: due to failure, btrbk.timer may need manual restoration:
61 e $new_shell systemctl start btrbk.timer
62 fi
63 if $restore_old_btrbk; then
64 e WARNING: due to failure, btrbk.timer may need manual restoration:
65 e $old_shell systemctl start btrbk.timer
66 fi
67 }
68
69 pre="$script_name:"
70 m() { printf "$pre %s\n" "$*"; "$@"; }
71 e() { printf "$pre %s\n" "$*"; }
72 err() { echo "$pre ERROR: $*" >&2; }
73 die() { printf "%s\n" "$*" >&2; echo "exiting with status 1" >&2; exit 1; }
74
75 if [[ $EUID != 0 ]]; then
76 err "requires running as root"
77 exit 1
78 fi
79
80
81 ##### begin command line parsing ########
82
83 mail_only=false
84 host2_only=false
85 force=false
86 force_arg=
87 pull_reexec=false
88 mp_args="-m /o,/a,/q,/qd,/qr"
89 check_installed=false
90 orig_args=("$@")
91 if ! temp=$(getopt -l check-installed,force,pull-reexec,help afioh "$@"); then
92 err "args invalid. args=$*"
93 fi
94 eval set -- "$temp"
95 while true; do
96 case $1 in
97 -a) snapshot_arg=resume ;;
98 --force|-f)
99 force=true
100 force_arg=-f
101 ;;
102 --check-installed)
103 check_installed=true
104 ;;
105 -i) incremental_arg="-i" ;;
106 # internal option for rerunning under newer old_host when doing pull
107 --pull-reexec) pull_reexec=true;;
108 -o)
109 mail_only=true ;;
110 -h|--help) usage ;;
111 --) shift; break ;;
112 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
113 esac
114 shift
115 done
116
117
118 if (( $# != 2 )) && ! $check_installed; then
119 err expected 2 args, got $#
120 fi
121
122 if [[ ! $HOSTNAME ]]; then
123 err "\$HOSTNAME is unset"
124 exit 1
125 fi
126
127 uninstalled-file-die() {
128 die "on host=$HOSTNAME, uninstalled file $1. run install-my-scripts or rerun with -f"
129 }
130
131
132 source /a/bin/bash_unpublished/source-state
133
134 direction=$1
135 host=$2
136
137 case $script_name in
138 switch-mail-host)
139 if [[ $MAIL_HOST != "$HOST2" ]]; then
140 mail_only=true
141 fi
142 ;;
143 switch-host2)
144 host2_only=true
145 ;;
146 *)
147 err unexpected script name
148 ;;
149 esac
150
151 if $mail_only; then
152 mp_args="-m /o"
153 elif $host2_only; then
154 mp_args="-m /a,/q,/qd,/qr"
155 fi
156
157
158 if ! $force && { $check_installed || [[ $direction == push ]]; } ; then
159 install_bin_files=(
160 mount-latest-subvol
161 check-subvol-stale
162 btrbk-run
163 switch-mail-host
164 )
165 for f in ${install_bin_files[@]}; do
166 if ! diff -q /a/bin/ds/$f /usr/local/bin/$f; then
167 uninstalled-file-die $f
168 fi
169 done
170 if ! diff -q /a/bin/bash-bear-trap/bash-bear /usr/local/lib/bash-bear; then
171 uninstalled-file-die err
172 fi
173 if $check_installed; then
174 exit 0
175 fi
176 fi
177
178
179 case $direction in
180 push)
181 old_host=$HOSTNAME
182 old_hostname=$HOSTNAME
183 new_host=$host
184 bbk_args="-t $new_host"
185 new_shell="ssh -F $HOME/.ssh/confighome root@$new_host"
186 if ! new_hostname=$($new_shell hostname); then
187 echo "$pre: error: failed ssh. retrying failed $new_shell with -v for more info:"
188 $new_shell -v hostname
189 fi
190 ;;
191 pull)
192 old_host=$host
193 new_host=$HOSTNAME
194 new_hostname=$HOSTNAME
195 bbk_args="-s $old_host"
196 old_shell="ssh -F $HOME/.ssh/confighome root@$old_host"
197 # tests ssh connection. crafted this to not need to do escape chars
198
199 if ! $mail_only && ! $pull_reexec ; then
200 if ! $force; then
201 if ! $old_shell switch-mail-host --check-installed; then
202 die "failed: $old_shell switch-mail-host --check-installed"
203 fi
204 fi
205 tmpd=$(mktemp -d)
206 files=(
207 /usr/local/{bin/{unsaved-buffers{,.el},switch-mail-host},lib/bash-bear}
208 )
209 m scp -F $HOME/.ssh/confighome \
210 ${files[@]/#/root@$old_host:} $tmpd
211 diff=false
212 for f in ${files[@]}; do
213 if ! diff -q $tmpd/${f##*/} $f; then
214 m install -T $tmpd/${f##*/} $f
215 diff=true
216 fi
217 done
218 if $diff; then
219 e "found different version on old_host=$old_host, reexecing"
220 m /usr/local/bin/switch-mail-host --pull-reexec "${orig_args[@]}"
221 exit 0
222 fi
223 rm -r -- $tmpd
224 fi
225
226 f=/a/bin/bash_unpublished/source-state
227 if ! old_info=$($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f"); then
228 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
229 $old_shell -v hostname
230 exit 1
231 fi
232 read -d '' -r old_hostname MAIL_HOST HOST2 <<<"$old_info" || (( $? == 1 ))
233
234 ;;
235 *)
236 err invalid first argument
237 exit 1
238 ;;
239 esac
240
241
242 if ! $force; then
243 if $host2_only; then
244 if [[ $old_hostname != "$HOST2" ]]; then
245 err "\$old_hostname($old_hostname) != \$HOST2($HOST2). Rerun with --force if you really want this."
246 exit 1
247 fi
248 elif [[ $old_hostname != "$MAIL_HOST" ]]; then
249 err "\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
250 exit 1
251 fi
252 fi
253
254 if [[ ! $new_host || ! $old_host ]]; then
255 echo "$0: bad args. see script"
256 exit 1
257 fi
258
259
260 ########### end initial processing, begin actually modifying things ##########
261
262 if $new_shell systemctl is-active btrbk.timer; then
263 m $new_shell systemctl stop btrbk.timer
264 restore_new_btrbk=true
265 fi
266 if $old_shell systemctl is-active btrbk.timer; then
267 m $old_shell systemctl stop btrbk.timer
268 restore_old_btrbk=true
269 fi
270
271 btrbk_test="systemctl is-active btrbk.service"
272 active=true
273 while $active; do
274 active=false
275 for shell in "$new_shell" "$old_shell"; do
276 e $shell $btrbk_test
277 status=$($shell $btrbk_test) ||:
278 case $status in
279 inactive|failed) : ;;
280 *)
281 # This covers conditions like "activating", which still return 3 from
282 # systemctl is-active.
283 active=true
284 e "btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
285 sleep 8
286 break
287 ;;
288 esac
289 done
290 done
291
292 if ! $host2_only; then
293 # ensure these are unused before doing anything
294 e "On $new_host: umounting /m and /o, checking emacs"
295 {
296 cat /usr/local/bin/unsaved-buffers
297 if ! $host2_only; then
298 cat <<EOF
299 for dir in m o; do
300 if mountpoint -q /\$dir; then
301 echo On $new_host: umount /\$dir
302 umount /\$dir
303 fi
304 done
305 EOF
306 fi
307 } | $new_shell bash -s
308 fi
309
310 if ! $mail_only; then
311 cat /usr/local/bin/unsaved-buffers - <<'EOF' | $old_shell bash -s
312
313 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
314 # But if emacs ignores the signal, let it live.
315 pkill -xf 'emacs( --daemon| -f znc-all)' ||:
316
317 if [[ -e /p/profanity-here ]]; then
318 systemctl disable --now profanity
319 fi
320 EOF
321 fi
322
323 # previously, I was checking to see if the new mail host
324 # is on my home network, then changing my home dns
325 # to resolve on the local network, so that I didnt
326 # have to send traffic out to the internet or rely
327 # on that. However, that breaks for a laptop that roams.
328 # So, we could have a cronjob that updates that dns,
329 # however, another solution is to just use ipv6,
330 # and I prefer that.
331 #
332 # TODO: enable ipv6 for email. exim config setting disables it.
333 # need to add vpn support. need to add firewall / routing.
334 # I think exim will try ipv6 first, so no need to disable
335 # ipv6 i think.
336
337
338 e Running initial btrbk
339 m btrbk-run -v $bbk_args $force_arg $incremental_arg $mp_args $snapshot_arg || ret=$?
340 if (( ret )); then
341 err "failed initial btrbk"
342 exit $ret
343 fi
344
345 if ! $mail_only; then
346 m $old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a/bin/bash_unpublished/source-state
347 m $new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a/bin/bash_unpublished/source-state
348 fi
349
350 if $host2_only; then
351 if [[ $old_hostname != "$MAIL_HOST" && $old_hostname != kd ]]; then
352 m $old_shell systemctl --now disable btrbk.timer
353 fi
354 m $new_shell systemctl --now enable btrbk.timer
355 if [[ -e /p/profanity-here ]]; then
356 m $new_shell systemctl --now enable profanity
357 fi
358 exit 0
359 fi
360
361 m $old_shell /a/exe/primary-setup $new_hostname || ret=$?
362 if (( ret )); then
363 err "failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
364 exit $ret
365 fi
366
367
368 e Running main btrbk
369 m btrbk-run -v --fast $bbk_args $force_arg $incremental_arg -m /o || ret=$?
370 if (( ret )); then
371 bang="███████"
372 e $bang failed btrbk of /o. restoring old host as primary
373 if ! m $old_shell /a/exe/primary-setup localhost; then
374 die "due to failed btrbk of /o, we tried to restore old host as primary, but then we failed at that too. To resolve: Fix & rerun switch-mail-host, or fix and rerun primary-setup localhost on old shell so you have a working mail server and then rerun switch-mail-host."
375 fi
376 e finished restoring old host as primary, now exiting $ret due to earlier failed btrbk of /o.
377 exit $ret
378 fi
379
380 # new system is usable at this point
381 blocks=██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
382 printf "%s\n" "${blocks:0:${COLUMNS:-100}}"
383
384 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
385 #
386 # die=false; for d in o.leaf.2021-05-29T10:02:08-0400/m/{4e,md,4e2}/{,l/}!(*myarchive)/new; do if $die; then break; fi; find $d -type f -mtime -5 | while read -r f; do dir="${f%new/*}"; dir="btrbk/o.20210530T000011-0400/${dir#*/}"; fname="${f##*/}"; [[ -e $dir/new/$fname || -e $dir/cur/$fname ]] && continue; if ! e cp -a $f /${dir#*/*/}new; then echo failed cp; die=true; break; fi ; done; done
387
388 # once I accidentally sent mail from non-main mail host. to copy into the main mail host's sent dir, cd into dir of non-mail mail host Sent/cur, then
389 #
390 # shopt -s nullglob; find . -type f -mtime -2 | while read -r f; do a=( /m/4e/Sent/cur/${f%,*}* ); if (( ${#a[@]} )); then e exists $a; else m cp -a $f /m/4e/Sent/cur; fi; done
391
392 m $new_shell /a/exe/primary-setup localhost || ret=$?
393 if (( ret )); then
394 err "failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"
395 exit $ret
396 fi
397
398 m exit 0