robustness and satisfy shellcheck
[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 $new_shell sudo systemctl start btrbk.timer
30 fi
31 if $restore_old_btrbk; then
32 $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 -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
88 source /a/bin/bash_unpublished/source-state
89
90
91 new_hostname=$($new_shell hostname)
92
93 ########### end initial processing, begin actually modifying things ##########
94
95 if $new_shell systemctl is-active btrbk.timer; then
96 m $new_shell sudo systemctl stop btrbk.timer
97 restore_new_btrbk=true
98 fi
99 if $old_shell systemctl is-active btrbk.timer; then
100 m $old_shell sudo systemctl stop btrbk.timer
101 restore_old_btrbk=true
102 fi
103
104 btrbk_test="systemctl is-active btrbk.service"
105 while [[ $($new_shell $btrbk_test) != inactive ]] || [[ $($old_shell $btrbk_test) != inactive ]]; do
106 echo "$0: btrbk is running on new or old host. sleeping for 8 seconds"
107 sleep 6
108 echo "$0: testing for btrbk activity in 2 seconds"
109 sleep 2
110 done
111
112 # ensure these are unused before doing anything
113
114 e "umounting /m and /o via $new_shell"
115 $new_shell bash -xs <<'EOF'
116 set -eE
117 if mountpoint -q /m; then sudo umount /m; fi
118 if mountpoint -q /o; then sudo umount /o; fi
119 EOF
120
121 # previously, I was checking to see if the new mail host
122 # is on my home network, then changing my home dns
123 # to resolve on the local network, so that I didnt
124 # have to send traffic out to the internet or rely
125 # on that. However, that breaks for a laptop that roams.
126 # So, we could have a cronjob that updates that dns,
127 # however, another solution is to just use ipv6,
128 # and I prefer that.
129 #
130 # TODO: enable ipv6 for email. exim config setting disables it.
131 # need to add vpn support. need to add firewall / routing.
132 # I think exim will try ipv6 first, so no need to disable
133 # ipv6 i think.
134
135 m $old_shell /a/exe/primary-setup $new_hostname
136
137 if $localhost_new; then
138 m btrbk-run -v -s $old_host $mp_args
139 else
140 m btrbk-run -v -t $new_host $mp_args
141 fi
142
143 m $new_shell /a/exe/primary-setup $new_hostname