various fixes and improvements
[distro-setup] / switch-mail-host
1 #!/bin/bash
2
3 source /usr/local/lib/err
4
5 usage() {
6 cat <<EOF
7 Usage: ${0##*/} 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 -o Only btrbk /o, instead of all filesystems.
14 -h|--help Print help and exit.
15
16 I used to adjust home network dns so NEW_HOST resolves locally if it is
17 on the local network, but its simpler just not to and just rely
18 on the internet. Email can wait.
19
20 Note: Uses GNU getopt options parsing style
21 EOF
22 exit $1
23 }
24
25 restore_new_btrbk=false
26 restore_old_btrbk=false
27 errcatch-cleanup() {
28 if $restore_new_btrbk; then
29 e WARNING: due to failure, btrbk.timer may need manual restoration:
30 e $new_shell sudo systemctl start btrbk.timer
31 fi
32 if $restore_old_btrbk; then
33 e WARNING: due to failure, btrbk.timer may need manual restoration:
34 e $old_shell sudo systemctl start btrbk.timer
35 fi
36 }
37
38 pre="${0##*/}:"
39 m() { printf "$pre %s\n" "$*"; "$@"; }
40 e() { printf "$pre %s\n" "$*"; }
41 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
42 mexit() { echo "exiting with status $1"; exit $1; }
43
44 ##### begin command line parsing ########
45
46 temp=$(getopt -l help owh "$@") || usage 1
47 mp_args="-m /o,/q,/a"
48 eval set -- "$temp"
49 while true; do
50 case $1 in
51 -o) mp_args="-m /o"; shift ;;
52 -h|--help) usage ;;
53 --) shift; break ;;
54 *) echo "$0: Internal error! unexpected args: $*" ; mexit 1 ;;
55 esac
56 done
57
58
59 (( $# == 2 )) || usage 1
60
61
62 case $1 in
63 push)
64 new_host=$2
65 bbk_args="-s $old_host"
66 new_shell="ssh $new_host"
67 old_host=$HOSTNAME
68 ;;
69 pull)
70 old_host=$2
71 bbk_args="-t $new_host"
72 bbk_args="-s $old_host"
73 new_host=$HOSTNAME
74 old_shell="ssh $old_host"
75 ;;
76 *)
77 err invalid first argument
78 mexit 1
79 ;;
80 esac
81
82
83 source /a/bin/bash_unpublished/source-state
84
85 if [[ $old_host != "$MAIL_HOST" ]]; then
86 e "WARNING: \$old_host != \$MAIL_HOST. Sleeping for 5 seconds in case you want to reconsider"
87 sleep 5
88 fi
89
90 if [[ ! $new_host || ! $old_host ]]; then
91 echo "$0: bad args. see script"
92 mexit 1
93 fi
94
95 e $new_shell hostname
96 new_hostname=$($new_shell hostname)
97
98 ########### end initial processing, begin actually modifying things ##########
99
100 if $new_shell systemctl is-active btrbk.timer; then
101 m $new_shell sudo systemctl stop btrbk.timer
102 restore_new_btrbk=true
103 fi
104 if $old_shell systemctl is-active btrbk.timer; then
105 m $old_shell sudo systemctl stop btrbk.timer
106 restore_old_btrbk=true
107 fi
108
109 btrbk_test="systemctl is-active btrbk.service"
110 while true; do
111 for shell in "$new_shell" "$old_shell"; do
112 e $shell $btrbk_test
113 status=$($shell $btrbk_test) ||:
114 case $status in
115 inactive|failed) : ;;
116 *)
117 e "btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
118 sleep 8
119 continue
120 ;;
121 esac
122 done
123 break
124 done
125
126 # ensure these are unused before doing anything
127
128 e "umounting /m and /o via $new_shell"
129 $new_shell bash -xs <<'EOF'
130 set -eE
131 if mountpoint -q /m; then sudo umount /m; fi
132 if mountpoint -q /o; then sudo umount /o; fi
133 EOF
134
135 # previously, I was checking to see if the new mail host
136 # is on my home network, then changing my home dns
137 # to resolve on the local network, so that I didnt
138 # have to send traffic out to the internet or rely
139 # on that. However, that breaks for a laptop that roams.
140 # So, we could have a cronjob that updates that dns,
141 # however, another solution is to just use ipv6,
142 # and I prefer that.
143 #
144 # TODO: enable ipv6 for email. exim config setting disables it.
145 # need to add vpn support. need to add firewall / routing.
146 # I think exim will try ipv6 first, so no need to disable
147 # ipv6 i think.
148
149
150 e Running initial btrbk
151 if ! m btrbk-run -v $bbk_args $mp_args; then
152 ret=$?
153 err "failed initial btrbk"
154 mexit $ret
155 fi
156
157 m $old_shell /a/exe/primary-setup $new_hostname
158
159 e Running main btrbk
160 if ! m btrbk-run -v $bbk_args -m /o; then
161 ret=$?
162 bang="$(printf "$(tput setaf 5)█$(tput sgr0)%.0s" 1 2 3 4 5 6 7)"
163 e $bang failed btrbk of /o. restoring old host as primary
164 m $old_shell /a/exe/primary-setup localhost
165 mexit $ret
166 fi
167
168 m $new_shell /a/exe/primary-setup localhost
169
170 mexit 0