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