remove kinsis / unused input settings
[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 [[ $HOSTNAME == tp ]] || [[ $HOSTNAME == frodo ]] || timeout -s 9 5 ssh wrt.b8.nz :; 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 elif $at_home; then
93 wrt_shell="ssh wrt.b8.nz"
94 else
95 if [[ $old_host == iank.vpn.office.fsf.org || $new_host == iank.vpn.office.fsf.org ]]; then
96 wrt_shell="ssh iank.vpn.office.fsf.org ssh wrt.b8.nz"
97 else
98 wrt_shell="ssh $HOME_DOMAIN ssh wrt.b8.nz"
99 fi
100 fi
101
102 btrbk_test="systemctl is-active btrbk.service"
103 while $new_shell $btrbk_test || $old_shell $btrbk_test; do
104 echo "$0: btrbk is running on new or old host. sleeping for 8 seconds"
105 sleep 6
106 echo "$0: testing for btrbk activity in 2 seconds"
107 sleep 2
108 done
109
110 ########### end initial processing, begin actually modifying things ##########
111
112 restore_new_btrbk=false
113 if $new_shell systemctl is-active btrbk.timer; then
114 $new_shell sudo systemctl stop btrbk.timer
115 restore_new_btrbk=true
116 fi
117 restore_old_btrbk=false
118 if $old_shell systemctl is-active btrbk.timer; then
119 $old_shell sudo systemctl stop btrbk.timer
120 restore_old_btrbk=true
121 fi
122
123 for ((i=0; i<10; i++)); do
124 $old_shell killall arbtt-capture || break
125 sleep 1
126 if [[ i == 9 ]]; then
127 warn="WARNING!!! failed to kill arbtt-capture"
128 fi
129 done
130
131
132 $new_shell bash -s <<'EOF'
133 set -eE
134 if mountpoint /m; then sudo umount /m; fi
135 if mountpoint /o; then sudo umount /o; fi
136 EOF
137
138 # if new_host is not on home network, make mail.iankelling.org not resolve
139 # on the home network.
140 if [[ $new_host == $HOSTNAME ]] && ! $at_home; then
141 echo | $wrt_shell cedit mail_host /etc/hosts || [[ $? == 1 ]] # 1 means file changed.
142 else
143 $wrt_shell bash -s <<EOFOUTER
144 cedit mail_host /etc/hosts <<EOF || /etc/init.d/dnsmasq restart
145 \$(grep "\b$new_host\b" /etc/hosts | awk '{print \$1}') mail.iankelling.org
146 EOF
147 EOFOUTER
148 fi
149
150 mail-setup() {
151 shell="$1"
152 $shell sed -ri "s/MAIL_HOST=.*/MAIL_HOST=$new_host/" /a/bin/bash_unpublished/source-semi-priv
153 $shell /a/bin/distro-setup/mail-setup exim4
154 }
155
156 mail-setup "$old_shell"
157
158 sudo dd of=/etc/btrbk.conf <<'EOF'
159 ssh_identity /root/.ssh/home
160 # Just a guess that local7 is a good facility to pick.
161 # It's a bit odd that the transaction log has to be logged to
162 # a file or syslog, while other output is sent to std out.
163 # The man does not mention a way for them to be together, but
164 # I dunno if setting a log level like warn might also output
165 # transaction info.
166 transaction_syslog local7
167
168 # so we only run one at a time
169 lockfile /var/lock/btrbk.lock
170
171 # default format of short does not accomidate hourly preservation setting
172 timestamp_format long-iso
173
174 # only make a snapshot if things have changed
175 snapshot_create onchange
176 # I could make this different from target_preserve,
177 # if one disk had less space.
178 # for now, keeping them equal.
179 snapshot_preserve 36h 14d 8w 24m
180 snapshot_preserve_min 4h
181 snapshot_dir btrbk
182
183 # so, total backups = ~89
184 target_preserve 36h 14d 8w 24m
185 target_preserve_min 4h
186
187 # if something fails and it's not obvious, try doing
188 # btrbk -l debug -v dryrun
189
190
191 EOF
192
193
194 if $localhost_new; then
195 btrbk_src=ssh://$old_host/mnt/root
196 btrbk_dst=/mnt/root/btrbk
197 else
198 btrbk_src=/mnt/root
199 btrbk_dst=ssh://$new_host/mnt/root/btrbk
200 fi
201
202 sudo tee -a /etc/btrbk.conf <<EOF
203 volume $btrbk_src
204 subvolume o
205 target send-receive $btrbk_dst
206 EOF
207
208
209 sudo btrbk -l debug --progress run
210 $new_shell mount-latest-subvol
211
212 mail-setup "$new_shell"
213
214 if $restore_new_btrbk; then
215 $new_shell sudo systemctl start btrbk.timer
216 fi
217 if $restore_old_btrbk; then
218 $old_shell sudo systemctl start btrbk.timer
219 fi
220
221 $new_shell DISPLAY=:0 arbtt-capture --sample-rate=10 &
222
223 echo $warn