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