lots of 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
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 if (( $# != 2 )) || [[ $1 == -* || $2 == -* ]]; then
25 usage 1
26 fi
27
28 old_host=$1
29 new_host=$2
30
31 if [[ $old_host != $MAIL_HOST ]]; then
32 read -p "warning: \$old_host != \$MAIL_HOST: $old_host != $MAIL_HOST, proceed? y/N "
33 if [[ $REPLY != [yY] ]]; then
34 exit 1
35 fi
36 fi
37
38 if [[ $new_host == "$HOSTNAME" ]]; then
39 localhost_new=true
40 new_shell=
41 else
42 localhost_new=false
43 new_shell="ssh $new_host"
44 fi
45
46 old_shell="ssh $old_host"
47 if [[ $old_host == "$HOSTNAME" ]]; then
48 old_shell=
49 fi
50
51 if [[ ! $new_host || ! $old_host ]]; then
52 echo "$0: bad args. see script"
53 exit 1
54 fi
55
56 at_home=false
57 if [[ $HOSTNAME == treetowl ]] || [[ $HOSTNAME == frodo ]] || timeout -s 9 5 ssh wrt.b8.nz :; then
58 at_home=true
59 fi
60 echo "$0: at_home = $at_home"
61
62 source /a/bin/bash_unpublished/source-semi-priv
63 #### begin convert private hostnames to public hostnames ####
64 if ! $at_home; then
65 for var in old_host new_host; do
66 case ${!var} in
67 treetowl)
68 eval $var=$HOME_DOMAIN
69 ;;
70 esac
71 done
72 fi
73 #### end convert private hostnames to public hostnames ####
74
75
76 # because our port forward is not robust enough, we can't use proxy command,
77 # todo: setup vpn so this is all taken care of.
78 if ! $at_home; then
79 if [[ $old_host == iank.vpn.office.fsf.org || $new_host == iank.vpn.office.fsf.org ]]; then
80 wrt_shell="ssh iank.vpn.office.fsf.org ssh wrt.b8.nz"
81 else
82 wrt_shell="ssh $HOME_DOMAIN ssh wrt.b8.nz"
83 fi
84 else
85 wrt_shell="ssh wrt.b8.nz"
86 fi
87
88 btrbk_test="systemctl is-active btrbk.service"
89 while $new_shell $btrbk_test || $old_shell $btrbk_test; do
90 echo "$0: btrbk is running on new or old host. sleeping for 8 seconds"
91 sleep 6
92 echo "$0: testing for btrbk activity in 2 seconds"
93 sleep 2
94 done
95
96 ########### end initial processing, begin actually modifying things ##########
97
98 restore_new_btrbk=false
99 if $new_shell systemctl is-active btrbk.timer; then
100 $new_shell sudo systemctl stop btrbk.timer
101 restore_new_btrbk=true
102 fi
103 restore_old_btrbk=false
104 if $old_shell systemctl is-active btrbk.timer; then
105 $old_shell sudo systemctl stop btrbk.timer
106 restore_old_btrbk=true
107 fi
108
109 $new_shell bash -s <<'EOF'
110 set -eE
111 if mountpoint /m; then sudo umount /m; fi
112 if mountpoint /o; then sudo umount /o; fi
113 EOF
114
115 # if new_host is not on home network, make mail.iankelling.org not resolve
116 # on the home network.
117 if [[ $new_host == $HOSTNAME ]] && ! $at_home; then
118 echo | $wrt_shell cedit mail_host /etc/hosts || [[ $? == 1 ]] # 1 means file changed.
119 else
120 $wrt_shell bash -s <<EOFOUTER
121 cedit mail_host /etc/hosts <<EOF || /etc/init.d/dnsmasq restart
122 \$(grep "\b$new_host\b" /etc/hosts | awk '{print \$1}') mail.iankelling.org
123 EOF
124 EOFOUTER
125 fi
126
127 mail-setup() {
128 shell="$1"
129 $shell sed -ri "s/MAIL_HOST=.*/MAIL_HOST=$new_host/" /a/bin/bash_unpublished/source-semi-priv
130 $shell /a/bin/distro-setup/mail-setup exim4
131 }
132
133 mail-setup "$old_shell"
134
135 sudo dd of=/etc/btrbk.conf <<'EOF'
136 ssh_identity /root/.ssh/id_rsa
137 # Just a guess that local7 is a good facility to pick.
138 # It's a bit odd that the transaction log has to be logged to
139 # a file or syslog, while other output is sent to std out.
140 # The man does not mention a way for them to be together, but
141 # I dunno if setting a log level like warn might also output
142 # transaction info.
143 transaction_syslog local7
144
145 # so we only run one at a time
146 lockfile /var/lock/btrbk.lock
147
148 # default format of short does not accomidate hourly preservation setting
149 timestamp_format long-iso
150
151 # only make a snapshot if things have changed
152 snapshot_create onchange
153 # I could make this different from target_preserve,
154 # if one disk had less space.
155 # for now, keeping them equal.
156 snapshot_preserve 36h 14d 8w 24m
157 snapshot_preserve_min 4h
158 snapshot_dir btrbk
159
160 # so, total backups = ~89
161 target_preserve 36h 14d 8w 24m
162 target_preserve_min 4h
163
164 # if something fails and it's not obvious, try doing
165 # btrbk -l debug -v dryrun
166
167
168 EOF
169
170
171 if $localhost_new; then
172 btrbk_src=ssh://$old_host/mnt/root
173 btrbk_dst=/mnt/root/btrbk
174 else
175 btrbk_src=/mnt/root
176 btrbk_dst=ssh://$new_host/mnt/root/btrbk
177 fi
178
179 sudo tee -a /etc/btrbk.conf <<EOF
180 volume $btrbk_src
181 subvolume o
182 target send-receive $btrbk_dst
183 EOF
184
185
186 sudo btrbk --progress run
187 $new_shell mount-latest-subvol
188
189 mail-setup
190
191 if $restore_new_btrbk; then
192 $new_shell sudo systemctl start btrbk.timer
193 fi
194 if $restore_old_btrbk; then
195 $old_shell sudo systemctl start btrbk.timer
196 fi