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