finally fully use gnu license recommendations
[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
138 if ! $force && { $check_installed || [[ $direction == push ]]; } ; then
139 install_bin_files=(
140 mount-latest-subvol
141 check-subvol-stale
142 btrbk-run
143 switch-mail-host
144 )
145 for f in ${install_bin_files[@]}; do
146 if ! diff -q /a/bin/ds/$f /usr/local/bin/$f; then
147 uninstalled-file-die $f
148 fi
149 done
150 if ! diff -q /a/bin/bash-bear-trap/bash-bear /usr/local/lib/bash-bear; then
151 uninstalled-file-die err
152 fi
153 if $check_installed; then
154 exit 0
155 fi
156 fi
157
158
159 case $direction in
160 push)
161 old_host=$HOSTNAME
162 old_hostname=$HOSTNAME
163 new_host=$host
164 bbk_args="-t $new_host"
165 new_shell="ssh -F $HOME/.ssh/confighome root@$new_host"
166 if ! new_hostname=$($new_shell hostname); then
167 echo "$pre: error: failed ssh. retrying failed $new_shell with -v for more info:"
168 $new_shell -v hostname
169 fi
170 ;;
171 pull)
172 old_host=$host
173 new_host=$HOSTNAME
174 new_hostname=$HOSTNAME
175 bbk_args="-s $old_host"
176 old_shell="ssh -F $HOME/.ssh/confighome root@$old_host"
177 # tests ssh connection. crafted this to not need to do escape chars
178
179 if ! $mail_only && ! $pull_reexec ; then
180 if ! $force; then
181 if ! $old_shell switch-mail-host --check-installed; then
182 die "failed: $old_shell switch-mail-host --check-installed"
183 fi
184 fi
185 tmpd=$(mktemp -d)
186 files=(
187 /usr/local/{bin/{unsaved-buffers{,.el},switch-mail-host},lib/bash-bear}
188 )
189 m scp -F $HOME/.ssh/confighome \
190 ${files[@]/#/root@$old_host:} $tmpd
191 diff=false
192 for f in ${files[@]}; do
193 if ! diff -q $tmpd/${f##*/} $f; then
194 m install -T $tmpd/${f##*/} $f
195 diff=true
196 fi
197 done
198 if $diff; then
199 e "found different version on old_host=$old_hostname, reexecing"
200 m /usr/local/bin/switch-mail-host --pull-reexec "${orig_args[@]}"
201 exit 0
202 fi
203 rm -r -- $tmpd
204 fi
205
206 f=/a/bin/bash_unpublished/source-state
207 if ! old_info=$($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f"); then
208 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
209 $old_shell -v hostname
210 exit 1
211 fi
212 read -d '' -r old_hostname MAIL_HOST HOST2 <<<"$old_info" || (( $? == 1 ))
213
214 ;;
215 *)
216 err invalid first argument
217 exit 1
218 ;;
219 esac
220
221 case $script_name in
222 switch-mail-host)
223 if [[ $MAIL_HOST != "$HOST2" ]]; then
224 mail_only=true
225 fi
226 ;;
227 switch-host2)
228 host2_only=true
229 ;;
230 *)
231 err unexpected script name
232 ;;
233 esac
234
235 if $mail_only; then
236 mp_args="-m /o"
237 elif $host2_only; then
238 mp_args="-m /a,/q,/qd,/qr"
239 fi
240
241 if ! $force; then
242 if $host2_only; then
243 if [[ $old_hostname != "$HOST2" ]]; then
244 err "\$old_hostname($old_hostname) != \$HOST2($HOST2). Rerun with --force if you really want this."
245 exit 1
246 fi
247 elif [[ $old_hostname != "$MAIL_HOST" ]]; then
248 err "\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
249 exit 1
250 fi
251 fi
252
253 if [[ ! $new_host || ! $old_host ]]; then
254 echo "$0: bad args. see script"
255 exit 1
256 fi
257
258
259 ########### end initial processing, begin actually modifying things ##########
260
261 if $new_shell systemctl is-active btrbk.timer; then
262 m $new_shell systemctl stop btrbk.timer
263 restore_new_btrbk=true
264 fi
265 if $old_shell systemctl is-active btrbk.timer; then
266 m $old_shell systemctl stop btrbk.timer
267 restore_old_btrbk=true
268 fi
269
270 btrbk_test="systemctl is-active btrbk.service"
271 active=true
272 while $active; do
273 active=false
274 for shell in "$new_shell" "$old_shell"; do
275 e $shell $btrbk_test
276 status=$($shell $btrbk_test) ||:
277 case $status in
278 inactive|failed) : ;;
279 *)
280 # This covers conditions like "activating", which still return 3 from
281 # systemctl is-active.
282 active=true
283 e "btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
284 sleep 8
285 break
286 ;;
287 esac
288 done
289 done
290
291 # ensure these are unused before doing anything
292 e "On $new_host: umounting /m and /o, checking emacs"
293 {
294 cat /usr/local/bin/unsaved-buffers
295 if ! $host2_only; then
296 cat <<EOF
297 for dir in m o; do
298 if mountpoint -q /\$dir; then
299 echo On $new_host: umount /\$dir
300 umount /\$dir
301 fi
302 done
303 EOF
304 fi
305 } | $new_shell bash -s
306
307 if ! $mail_only; then
308 cat /usr/local/bin/unsaved-buffers - <<'EOF' | $old_shell bash -s
309
310 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
311 # But if emacs ignores the signal, let it live.
312 pkill -xf 'emacs( --daemon| -f znc-all)' ||:
313
314 if [[ -e /p/profanity-here ]]; then
315 systemctl disable --now profanity
316 fi
317 EOF
318 fi
319
320 # previously, I was checking to see if the new mail host
321 # is on my home network, then changing my home dns
322 # to resolve on the local network, so that I didnt
323 # have to send traffic out to the internet or rely
324 # on that. However, that breaks for a laptop that roams.
325 # So, we could have a cronjob that updates that dns,
326 # however, another solution is to just use ipv6,
327 # and I prefer that.
328 #
329 # TODO: enable ipv6 for email. exim config setting disables it.
330 # need to add vpn support. need to add firewall / routing.
331 # I think exim will try ipv6 first, so no need to disable
332 # ipv6 i think.
333
334
335 e Running initial btrbk
336 m btrbk-run -v $bbk_args $force_arg $incremental_arg $mp_args $snapshot_arg || ret=$?
337 if (( ret )); then
338 err "failed initial btrbk"
339 exit $ret
340 fi
341
342 if ! $mail_only; then
343 m $old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a/bin/bash_unpublished/source-state
344 m $new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a/bin/bash_unpublished/source-state
345 fi
346
347 if $host2_only; then
348 if [[ $old_hostname != "$MAIL_HOST" && $old_hostname != kd ]]; then
349 m $old_shell systemctl --now disable btrbk.timer
350 fi
351 m $new_shell systemctl --now enable btrbk.timer
352 if [[ -e /p/profanity-here ]]; then
353 m $new_shell systemctl --now enable profanity
354 fi
355 exit 0
356 fi
357
358 m $old_shell /a/exe/primary-setup $new_hostname || ret=$?
359 if (( ret )); then
360 err "failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
361 exit $ret
362 fi
363
364
365 e Running main btrbk
366 m btrbk-run -v --fast $bbk_args $force_arg $incremental_arg -m /o || ret=$?
367 if (( ret )); then
368 bang="███████"
369 e $bang failed btrbk of /o. restoring old host as primary
370 if ! m $old_shell /a/exe/primary-setup localhost; then
371 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."
372 fi
373 e finished restoring old host as primary, now exiting $ret due to earlier failed btrbk of /o.
374 exit $ret
375 fi
376
377 # new system is usable at this point
378 blocks=██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
379 printf "%s\n" "${blocks:0:${COLUMNS:-100}}"
380
381 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
382 #
383 # 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
384
385 # 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
386 #
387 # 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
388
389 m $new_shell /a/exe/primary-setup localhost || ret=$?
390 if (( ret )); then
391 err "failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"
392 exit $ret
393 fi
394
395 if ! $mail_only && [[ -e /p/profanity-here ]]; then
396 m $new_shell systemctl --now enable profanity || ret=$?
397 if (( ret )); then
398 err "failed final systemctl --now enable profanity, just fix and rerun"
399 exit $ret
400 fi
401 fi
402
403 m exit 0