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