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