minor fix and add automatic updates
[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=
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-semi-priv
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-semi-priv
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 btrbk_test="systemctl is-active btrbk.service"
100 while $new_shell $btrbk_test || $old_shell $btrbk_test; do
101 echo "$0: btrbk is running on new or old host. sleeping for 8 seconds"
102 sleep 6
103 echo "$0: testing for btrbk activity in 2 seconds"
104 sleep 2
105 done
106
107 new_hostname=$($new_shell hostname)
108
109 ########### end initial processing, begin actually modifying things ##########
110
111 restore_new_btrbk=false
112 if $new_shell systemctl is-active btrbk.timer; then
113 $new_shell sudo systemctl stop btrbk.timer
114 restore_new_btrbk=true
115 fi
116 restore_old_btrbk=false
117 if $old_shell systemctl is-active btrbk.timer; then
118 $old_shell sudo systemctl stop btrbk.timer
119 restore_old_btrbk=true
120 fi
121
122
123 $new_shell bash -xs <<'EOF'
124 set -eE
125 if mountpoint -q /m; then sudo umount /m; fi
126 if mountpoint -q /o; then sudo umount /o; fi
127 EOF
128
129 # previously, I was checking to see if the new mail host
130 # is on my home network, then changing my home dns
131 # to resolve on the local network, so that I didnt
132 # have to send traffic out to the internet or rely
133 # on that. However, that breaks for a laptop that roams.
134 # So, we could have a cronjob that updates that dns,
135 # however, another solution is to just use ipv6,
136 # and I prefer that.
137 #
138 # TODO: enable ipv6 for email. exim config setting disables it.
139 # need to add vpn support. need to add firewall / routing.
140 # I think exim will try ipv6 first, so no need to disable
141 # ipv6 i think.
142
143 $old_shell primary-setup $new_hostname
144
145 if $localhost_new; then
146 btrbk-run -s $old_host $mp_args
147 else
148 btrbk-run -t $new_host $mp_args
149 fi
150
151 $new_shell primary-setup $new_hostname
152
153 if $restore_new_btrbk; then
154 $new_shell sudo systemctl start btrbk.timer
155 fi
156 if $restore_old_btrbk; then
157 $old_shell sudo systemctl start btrbk.timer
158 fi