4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
8 Usage: ${0##*/} OLD_HOST NEW_HOST
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.
14 -w Don't try to ssh to wrt. Should only be used in unusual network situation.
15 -h|--help Print help and exit.
17 Note: Uses GNU getopt options parsing style
22 ##### begin command line parsing ########
24 update_wrt
=true
# default
25 temp
=$
(getopt
-l help wh
"$@") || usage
1
29 -w) update_wrt
=false
; shift ;;
32 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
37 (( $# == 2 )) || usage
1
41 source /a
/bin
/bash_unpublished
/source-semi-priv
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
50 if [[ $new_host == "$HOSTNAME" ]]; then
55 new_shell
="ssh $new_host"
58 old_shell
="ssh $old_host"
59 if [[ $old_host == "$HOSTNAME" ]]; then
63 if [[ ! $new_host ||
! $old_host ]]; then
64 echo "$0: bad args. see script"
68 # TODO: turn this home dns update into a separate script that also runs
69 # as a cronjob for the MAIL_HOST, so it can update dns for a laptop that
70 # moves to a different network.
72 # This exists because if mail_host is on the home network with a vpn
73 # tunnel, and another host on the home network tries to connect to the
74 # public tunnel ip, it won't work because from the public machine's
75 # perspective, due to nat, both home network machines have the same
76 # ip. We could solve this by using ipv6 addresses, but not all networks
77 # i'm on with more than one machine are ipv6 enabled.
79 if timeout
-s 9 5 ssh-keyscan
-p 2220 -t rsa
10.0.0.1 2>/dev
/null |
grep -qFx '[10.0.0.1]:2220 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCH+/h1dGEfKEusBblndU2e6QT4wLCm5+yqr/sqh/0X9YfjR7BfWWm8nNmuP55cYc+Wuf5ljB1H1acXEcsl1y8e0j3agHfF0V74FE1N1zz5nn2Ep8NHnmqgEhza38ZxMPh+4p3X7zklEKU7+3SzybKBi8sg0wLzlS2LM0JaUN80zR2sK11Kye3dURUXPk78u5wodOkgcEYRwSYaDMJlUzWP+poRXIDJwFaMQnwmxbl/c84yOyaU0x/d6hFwoRscWecihX+vvBNeSyxR4xr2HDOyUWwJkctyAgt2p7w3tfkXOKcCRzTAjGVIMQLTvo0sG/yJbcyHoEFdFybCsgDvfyYn'; then
82 echo "$0: at_home = $at_home"
84 source /a
/bin
/bash_unpublished
/source-semi-priv
85 #### begin convert private hostnames to public hostnames ####
87 # for var in old_host new_host; do
90 # eval $var=$HOME_DOMAIN
95 #### end convert private hostnames to public hostnames ####
98 # because our port forward is not robust enough, we can't use proxy command,
99 # todo: setup vpn so this is all taken care of.
100 if ! $update_wrt; then
103 wrt_shell
="ssh wrt.b8.nz"
106 btrbk_test
="systemctl is-active btrbk.service"
107 while $new_shell $btrbk_test ||
$old_shell $btrbk_test; do
108 echo "$0: btrbk is running on new or old host. sleeping for 8 seconds"
110 echo "$0: testing for btrbk activity in 2 seconds"
114 new_hostname
=$
($new_shell hostname
)
116 ########### end initial processing, begin actually modifying things ##########
118 restore_new_btrbk
=false
119 if $new_shell systemctl is-active btrbk.timer
; then
120 $new_shell sudo systemctl stop btrbk.timer
121 restore_new_btrbk
=true
123 restore_old_btrbk
=false
124 if $old_shell systemctl is-active btrbk.timer
; then
125 $old_shell sudo systemctl stop btrbk.timer
126 restore_old_btrbk
=true
130 $new_shell bash
-xs <<'EOF'
132 if mountpoint -q /m; then sudo umount /m; fi
133 if mountpoint -q /o; then sudo umount /o; fi
136 # if new_host is not on home network, make mail.iankelling.org not resolve
137 # on the home network.
138 if [[ $new_host == $HOSTNAME ]] && ! $at_home; then
139 echo |
$wrt_shell cedit mail_host
/etc
/hosts ||
[[ $?
== 1 ]] # 1 means file changed.
141 $wrt_shell bash
-xs $new_host <<'EOFOUTER'
143 cedit mail_host /etc/hosts <<EOF || /etc/init.d/dnsmasq restart
144 $(grep "\b$new_hostname\b" /etc/hosts | awk '{print $1}') mail.iankelling.org
149 $old_shell primary-setup
$new_hostname
151 /a
/bin
/distro-setup
/install-my-scripts
153 if $localhost_new; then
154 btrbk-run
-s $old_host -m /o
156 btrbk-run
-t $new_host -m /o
159 $new_shell primary-setup
$new_hostname
161 if $restore_new_btrbk; then
162 $new_shell sudo systemctl start btrbk.timer
164 if $restore_old_btrbk; then
165 $old_shell sudo systemctl start btrbk.timer