add switch-host2
[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 -i Disallow incremental backup.
14 -o Only btrbk /o, instead of all filesystems.
15 --force Run even though our local state does not say that MAIL_HOST is
16 us when pushing or HOST when pulling.
17 -h|--help Print help and exit.
18
19 I used to adjust home network dns so NEW_HOST resolves locally if it is
20 on the local network, but its simpler just not to and just rely
21 on the internet. Email can wait.
22
23 Note: Uses GNU getopt options parsing style
24 EOF
25 exit $1
26 }
27
28 script_name="${BASH_SOURCE[0]}"
29 script_name="${script_name##*/}"
30
31 restore_new_btrbk=false
32 restore_old_btrbk=false
33 err-cleanup() {
34 if $restore_new_btrbk; then
35 e WARNING: due to failure, btrbk.timer may need manual restoration:
36 e $new_shell systemctl start btrbk.timer
37 fi
38 if $restore_old_btrbk; then
39 e WARNING: due to failure, btrbk.timer may need manual restoration:
40 e $old_shell systemctl start btrbk.timer
41 fi
42 }
43
44 pre="$script_name:"
45 m() { printf "$pre %s\n" "$*"; "$@"; }
46 e() { printf "$pre %s\n" "$*"; }
47 err() { echo "$pre ERROR: $*" >&2; }
48
49 if [[ $EUID != 0 ]]; then
50 err "requires running as root"
51 exit 1
52 fi
53
54
55 ##### begin command line parsing ########
56
57 mail_only=false
58 host2_only=false
59 force=false
60 mp_args="-m /o,/q,/a"
61 temp=$(getopt -l force,help ioh "$@") || usage 1
62 eval set -- "$temp"
63 while true; do
64 case $1 in
65 --force) force=true ;;
66 -i) incremental_arg="-i" ;;
67 -o)
68 mail_only=true ;;
69 -h|--help) usage ;;
70 --) shift; break ;;
71 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
72 esac
73 shift
74 done
75
76
77 (( $# == 2 )) || usage 1
78
79 if [[ ! $HOSTNAME ]]; then
80 err "\$HOSTNAME is unset"
81 exit 1
82 fi
83
84 source /a/bin/bash_unpublished/source-state
85
86 direction=$1
87 host=$2
88 case $direction in
89 push)
90 old_host=$HOSTNAME
91 old_hostname=$HOSTNAME
92 new_host=$host
93 bbk_args="-t $new_host"
94 new_shell="ssh -F $HOME/.ssh/confighome root@$new_host"
95 $new_shell -v hostname
96 new_hostname=$($new_shell hostname)
97 ;;
98 pull)
99 old_host=$host
100 new_host=$HOSTNAME
101 new_hostname=$HOSTNAME
102 bbk_args="-s $old_host"
103 old_shell="ssh -F $HOME/.ssh/confighome root@$old_host"
104 # tests ssh connection. crafted this to not need to do escape chars
105 f=/a/bin/bash_unpublished/source-state
106 if ! old_info=($($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f")); then
107 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
108 $old_shell -v hostname
109 exit 1
110 fi
111 old_hostname=${old_info[0]}
112 MAIL_HOST=${old_info[1]}
113 HOST2=${old_info[2]}
114 ;;
115 *)
116 err invalid first argument
117 exit 1
118 ;;
119 esac
120
121 case $script_name in
122 switch-mail-host)
123 if [[ $MAIL_HOST != "$HOST2" ]]; then
124 mail_only=true
125 fi
126 ;;
127 switch-host2)
128 host2_only=true
129 ;;
130 *)
131 err unexpected script name
132 ;;
133 esac
134
135
136 if $mail_only; then
137 mp_args="-m /o"
138 elif $host2_only; then
139 mp_args="-m /a,/ar,/q,/qr"
140 fi
141
142
143
144 if [[ $old_hostname != "$MAIL_HOST" ]] && ! $force; then
145 err "\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
146 exit 1
147 fi
148
149 if [[ ! $new_host || ! $old_host ]]; then
150 echo "$0: bad args. see script"
151 exit 1
152 fi
153
154
155 ########### end initial processing, begin actually modifying things ##########
156
157 if $new_shell systemctl is-active btrbk.timer; then
158 m $new_shell systemctl stop btrbk.timer
159 restore_new_btrbk=true
160 fi
161 if $old_shell systemctl is-active btrbk.timer; then
162 m $old_shell systemctl stop btrbk.timer
163 restore_old_btrbk=true
164 fi
165
166 btrbk_test="systemctl is-active btrbk.service"
167 active=true
168 while $active; do
169 active=false
170 for shell in "$new_shell" "$old_shell"; do
171 e $shell $btrbk_test
172 status=$($shell $btrbk_test) ||:
173 case $status in
174 inactive|failed) : ;;
175 *)
176 # This covers conditions like "activating", which still return 3 from
177 # systemctl is-active.
178 active=true
179 e "btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
180 sleep 8
181 break
182 ;;
183 esac
184 done
185 done
186
187 # ensure these are unused before doing anything
188 e "On $new_host: umounting /m and /o, checking emacs"
189 {
190 cat <<'EOF'
191 set -eE
192 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
193 export XDG_RUNTIME_DIR=/run/user/1000
194 bufs="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
195 if [[ $bufs ]]; then
196 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
197 exit 1
198 fi
199 fi
200 EOF
201 if ! $host2_only; then
202 cat <<'EOF'
203 for dir in m o; do
204 if mountpoint -q /$dir; then
205 echo On $new_host: umount /$dir
206 umount /$dir
207 fi
208 done
209 EOF
210 fi
211 } | $new_shell bash -s
212
213 $old_shell bash -s <<'EOF'
214 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
215 export XDG_RUNTIME_DIR=/run/user/1000
216 bufs="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
217 if [[ $bufs ]]; then
218 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
219 exit 1
220 fi
221 fi
222 EOF
223
224 # previously, I was checking to see if the new mail host
225 # is on my home network, then changing my home dns
226 # to resolve on the local network, so that I didnt
227 # have to send traffic out to the internet or rely
228 # on that. However, that breaks for a laptop that roams.
229 # So, we could have a cronjob that updates that dns,
230 # however, another solution is to just use ipv6,
231 # and I prefer that.
232 #
233 # TODO: enable ipv6 for email. exim config setting disables it.
234 # need to add vpn support. need to add firewall / routing.
235 # I think exim will try ipv6 first, so no need to disable
236 # ipv6 i think.
237
238
239 e Running initial btrbk
240 if ! m btrbk-run -v $bbk_args $incremental_arg $mp_args; then
241 ret=$?
242 err "failed initial btrbk"
243 exit $ret
244 fi
245
246 if ! $mail_only; then
247 m $old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a/bin/bash_unpublished/source-state
248 m $new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a/bin/bash_unpublished/source-state
249 fi
250
251 if $host2_only; then
252 exit 0
253 fi
254
255 if ! m $old_shell /a/exe/primary-setup $new_hostname; then
256 ret=$?
257 err "failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
258 exit $ret
259 fi
260
261 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
262 # But if emacs ignores the signal, let it live.
263 m $new_shell killall -q emacs ||:
264
265 e Running main btrbk
266 m btrbk-run -v $bbk_args $incremental_arg -m /o || ret=$?
267 if (( ret )); then
268 bang="$(printf "$(tput setaf 5)█$(tput sgr0)%.0s" 1 2 3 4 5 6 7)"
269 e $bang failed btrbk of /o. restoring old host as primary
270 m $old_shell /a/exe/primary-setup localhost
271 exit $ret
272 fi
273
274 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
275 #
276 # 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
277
278 # 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
279 #
280 # 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
281
282 if ! m $new_shell /a/exe/primary-setup localhost; then
283 ret=$?
284 err "failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"
285 exit $ret
286 fi
287
288 m exit 0