2d74ceb824d69ac62089b5491abdfad4db0424ba
[distro-setup] / switch-mail-host
1 #!/bin/bash
2 set -x
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5
6 usage() {
7 cat <<EOF
8 Usage: ${0##*/} OLD_HOST NEW_HOST
9
10 Adjust home network dns so NEW_HOST resolves locally if it is on the
11 local network. Turn off mail receiving on OLD_HOST, run btrbk to move
12 mail to NEW_HOST, turn on mail receiving on NEW_HOST.
13
14 -o Only btrbk /o, instead of all filesystems.
15 -w Don't try to ssh to wrt. Should only be used in unusual network situation.
16 -h|--help Print help and exit.
17
18 Note: Uses GNU getopt options parsing style
19 EOF
20 exit $1
21 }
22
23 ##### begin command line parsing ########
24
25 update_wrt=true # default
26 temp=$(getopt -l help owh "$@") || usage 1
27 mp_args="-m /o,/q,/a"
28 eval set -- "$temp"
29 while true; do
30 case $1 in
31 -o) mp_args="-m /o"; shift ;;
32 -w) update_wrt=false; shift ;;
33 -h|--help) usage ;;
34 --) shift; break ;;
35 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
36 esac
37 done
38
39
40 (( $# == 2 )) || usage 1
41
42 old_host=$1
43 new_host=$2
44 source /a/bin/bash_unpublished/source-state
45
46 if [[ $old_host != $MAIL_HOST ]]; then
47 read -p "warning: \$old_host != \$MAIL_HOST: $old_host != $MAIL_HOST, proceed? y/N "
48 if [[ $REPLY != [yY] ]]; then
49 exit 1
50 fi
51 fi
52
53 if [[ $new_host == "$HOSTNAME" ]]; then
54 localhost_new=true
55 new_shell=
56 else
57 localhost_new=false
58 new_shell="ssh $new_host"
59 fi
60
61 old_shell="ssh $old_host"
62 if [[ $old_host == "$HOSTNAME" ]]; then
63 old_shell=
64 fi
65
66 if [[ ! $new_host || ! $old_host ]]; then
67 echo "$0: bad args. see script"
68 exit 1
69 fi
70
71 at_home=false
72 if timeout -s 9 5 ssh-keyscan -p 2220 -t rsa 10.0.0.1 2>/dev/null | grep -qFx '[10.0.0.1]:2220 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCH+/h1dGEfKEusBblndU2e6QT4wLCm5+yqr/sqh/0X9YfjR7BfWWm8nNmuP55cYc+Wuf5ljB1H1acXEcsl1y8e0j3agHfF0V74FE1N1zz5nn2Ep8NHnmqgEhza38ZxMPh+4p3X7zklEKU7+3SzybKBi8sg0wLzlS2LM0JaUN80zR2sK11Kye3dURUXPk78u5wodOkgcEYRwSYaDMJlUzWP+poRXIDJwFaMQnwmxbl/c84yOyaU0x/d6hFwoRscWecihX+vvBNeSyxR4xr2HDOyUWwJkctyAgt2p7w3tfkXOKcCRzTAjGVIMQLTvo0sG/yJbcyHoEFdFybCsgDvfyYn'; then
73 at_home=true
74 fi
75 echo "$0: at_home = $at_home"
76
77 source /a/bin/bash_unpublished/source-state
78 #### begin convert private hostnames to public hostnames ####
79 #if ! $at_home; then
80 # for var in old_host new_host; do
81 # case ${!var} in
82 # tp)
83 # eval $var=$HOME_DOMAIN
84 # ;;
85 # esac
86 # done
87 #fi
88 #### end convert private hostnames to public hostnames ####
89
90
91 # because our port forward is not robust enough, we can't use proxy command,
92 # todo: setup vpn so this is all taken care of.
93 if ! $update_wrt; then
94 wrt_shell=:
95 else
96 wrt_shell="ssh wrt.b8.nz"
97 fi
98
99 new_hostname=$($new_shell hostname)
100
101 ########### end initial processing, begin actually modifying things ##########
102
103 restore_new_btrbk=false
104 if $new_shell systemctl is-active btrbk.timer; then
105 $new_shell sudo systemctl stop btrbk.timer
106 echo "$0: note: stopped btrbk.timer on $new_host"
107 restore_new_btrbk=true
108 fi
109 restore_old_btrbk=false
110 if $old_shell systemctl is-active btrbk.timer; then
111 $old_shell sudo systemctl stop btrbk.timer
112 echo "$0: note: stopped btrbk.timer on $old_host"
113 restore_old_btrbk=true
114 fi
115
116 btrbk_test="systemctl is-active btrbk.service"
117 while [[ $($new_shell $btrbk_test) != inactive ]] || [[ $($old_shell $btrbk_test) != inactive ]]; do
118 echo "$0: btrbk is running on new or old host. sleeping for 8 seconds"
119 sleep 6
120 echo "$0: testing for btrbk activity in 2 seconds"
121 sleep 2
122 done
123
124
125 $new_shell bash -xs <<'EOF'
126 set -eE
127 if mountpoint -q /m; then sudo umount /m; fi
128 if mountpoint -q /o; then sudo umount /o; fi
129 EOF
130
131 # previously, I was checking to see if the new mail host
132 # is on my home network, then changing my home dns
133 # to resolve on the local network, so that I didnt
134 # have to send traffic out to the internet or rely
135 # on that. However, that breaks for a laptop that roams.
136 # So, we could have a cronjob that updates that dns,
137 # however, another solution is to just use ipv6,
138 # and I prefer that.
139 #
140 # TODO: enable ipv6 for email. exim config setting disables it.
141 # need to add vpn support. need to add firewall / routing.
142 # I think exim will try ipv6 first, so no need to disable
143 # ipv6 i think.
144
145 $old_shell /a/exe/primary-setup $new_hostname
146
147 if $localhost_new; then
148 btrbk-run -s $old_host $mp_args
149 else
150 btrbk-run -t $new_host $mp_args
151 fi
152
153 $new_shell /a/exe/primary-setup $new_hostname
154
155 if $restore_new_btrbk; then
156 $new_shell sudo systemctl start btrbk.timer
157 fi
158 if $restore_old_btrbk; then
159 $old_shell sudo systemctl start btrbk.timer
160 fi