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