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