minor bug fix
[distro-setup] / switch-mail-host
1 #!/bin/bash
2
3 source /usr/local/lib/err
4
5 usage() {
6 cat <<EOF
7 Usage: ${0##*/} OLD_HOST NEW_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 cleanup() {
28 if $restore_new_btrbk; then
29 m $new_shell sudo systemctl start btrbk.timer
30 fi
31 if $restore_old_btrbk; then
32 m $old_shell sudo systemctl start btrbk.timer
33 fi
34 }
35 _errcatch_cleanup=cleanup # used by sourced err
36
37 pre="${0##*/}:"
38 m() { printf "$pre %s\n" "$*"; "$@"; }
39 e() { printf "$pre %s\n" "$*"; }
40
41 ##### begin command line parsing ########
42
43 temp=$(getopt -l help owh "$@") || usage 1
44 mp_args="-m /o,/q,/a"
45 eval set -- "$temp"
46 while true; do
47 case $1 in
48 -o) mp_args="-m /o"; shift ;;
49 -h|--help) usage ;;
50 --) shift; break ;;
51 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
52 esac
53 done
54
55
56 (( $# == 2 )) || usage 1
57
58 old_host=$1
59 new_host=$2
60 source /a/bin/bash_unpublished/source-state
61
62 if [[ $old_host != "$MAIL_HOST" ]]; then
63 read -r -p "warning: \$old_host != \$MAIL_HOST: $old_host != $MAIL_HOST, proceed? y/N "
64 if [[ $REPLY != [yY] ]]; then
65 exit 1
66 fi
67 fi
68
69 if [[ $new_host == "$HOSTNAME" ]]; then
70 localhost_new=true
71 new_shell=
72 else
73 localhost_new=false
74 new_shell="ssh $new_host"
75 fi
76
77 old_shell="ssh $old_host"
78 if [[ $old_host == "$HOSTNAME" ]]; then
79 old_shell=
80 fi
81
82 if [[ ! $new_host || ! $old_host ]]; then
83 echo "$0: bad args. see script"
84 exit 1
85 fi
86
87 e $new_shell hostname
88 new_hostname=$($new_shell hostname)
89
90 ########### end initial processing, begin actually modifying things ##########
91
92 if $new_shell systemctl is-active btrbk.timer; then
93 m $new_shell sudo systemctl stop btrbk.timer
94 restore_new_btrbk=true
95 fi
96 if $old_shell systemctl is-active btrbk.timer; then
97 m $old_shell sudo systemctl stop btrbk.timer
98 restore_old_btrbk=true
99 fi
100
101 btrbk_test="systemctl is-active btrbk.service"
102 while true; do
103 for shell in "$new_shell" "$old_shell"; do
104 e $shell $btrbk_test
105 status=$($shell $btrbk_test) ||:
106 case $status in
107 inactive|failed) : ;;
108 *)
109 e "btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
110 sleep 8
111 continue
112 ;;
113 esac
114 done
115 break
116 done
117
118 # ensure these are unused before doing anything
119
120 e "umounting /m and /o via $new_shell"
121 $new_shell bash -xs <<'EOF'
122 set -eE
123 if mountpoint -q /m; then sudo umount /m; fi
124 if mountpoint -q /o; then sudo umount /o; fi
125 EOF
126
127 # previously, I was checking to see if the new mail host
128 # is on my home network, then changing my home dns
129 # to resolve on the local network, so that I didnt
130 # have to send traffic out to the internet or rely
131 # on that. However, that breaks for a laptop that roams.
132 # So, we could have a cronjob that updates that dns,
133 # however, another solution is to just use ipv6,
134 # and I prefer that.
135 #
136 # TODO: enable ipv6 for email. exim config setting disables it.
137 # need to add vpn support. need to add firewall / routing.
138 # I think exim will try ipv6 first, so no need to disable
139 # ipv6 i think.
140
141 m $old_shell /a/exe/primary-setup $new_hostname
142
143 if $localhost_new; then
144 m btrbk-run -v -s $old_host $mp_args
145 else
146 m btrbk-run -v -t $new_host $mp_args
147 fi
148
149 m $new_shell /a/exe/primary-setup $new_hostname