3 source /usr
/local
/lib
/err
7 Usage: ${0##*/} push|pull HOST
9 Turn off mail receiving on OLD_HOST, run btrbk to move mail to NEW_HOST,
10 turn on mail receiving on NEW_HOST. Assumes we want to move all
11 filesystems unless passing -o.
13 -o Only btrbk /o, instead of all filesystems.
14 -h|--help Print help and exit.
16 I used to adjust home network dns so NEW_HOST resolves locally if it is
17 on the local network, but its simpler just not to and just rely
18 on the internet. Email can wait.
20 Note: Uses GNU getopt options parsing style
25 restore_new_btrbk
=false
26 restore_old_btrbk
=false
28 if $restore_new_btrbk; then
29 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
30 e
$new_shell sudo systemctl start btrbk.timer
32 if $restore_old_btrbk; then
33 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
34 e
$old_shell sudo systemctl start btrbk.timer
39 m
() { printf "$pre %s\n" "$*"; "$@"; }
40 e
() { printf "$pre %s\n" "$*"; }
41 err
() { echo "$pre: ERROR: $*" >&2; }
42 mexit
() { echo "$pre: exiting with status $1"; exit $1; }
44 ##### begin command line parsing ########
46 temp
=$
(getopt
-l help owh
"$@") || usage
1
51 -o) mp_args
="-m /o"; shift ;;
54 *) echo "$0: Internal error! unexpected args: $*" ; mexit
1 ;;
59 (( $# == 2 )) || usage
1
61 if [[ ! $HOSTNAME ]]; then
62 err
'$HOSTNAME is unset'
70 bbk_args
="-s $old_host"
71 new_shell
="ssh $new_host"
72 new_hostname
=$
($new_shell hostname
)
77 new_hostname
=$HOSTNAME
78 bbk_args
="-t $new_host"
79 bbk_args
="-s $old_host"
80 old_shell
="ssh $old_host"
85 err invalid first argument
90 source /a
/bin
/bash_unpublished
/source-state
92 if [[ $old_host != "$MAIL_HOST" ]]; then
93 e
"WARNING: \$old_host != \$MAIL_HOST. Sleeping for 5 seconds in case you want to reconsider"
97 if [[ ! $new_host ||
! $old_host ]]; then
98 echo "$0: bad args. see script"
103 ########### end initial processing, begin actually modifying things ##########
105 if $new_shell systemctl is-active btrbk.timer
; then
106 m
$new_shell sudo systemctl stop btrbk.timer
107 restore_new_btrbk
=true
109 if $old_shell systemctl is-active btrbk.timer
; then
110 m
$old_shell sudo systemctl stop btrbk.timer
111 restore_old_btrbk
=true
114 btrbk_test
="systemctl is-active btrbk.service"
116 for shell
in "$new_shell" "$old_shell"; do
118 status
=$
($shell $btrbk_test) ||
:
120 inactive|failed
) : ;;
122 e
"btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
131 # ensure these are unused before doing anything
133 e
"umounting /m and /o via $new_shell"
134 $new_shell bash
-xs <<'EOF'
136 if mountpoint -q /m; then sudo umount /m; fi
137 if mountpoint -q /o; then sudo umount /o; fi
140 # previously, I was checking to see if the new mail host
141 # is on my home network, then changing my home dns
142 # to resolve on the local network, so that I didnt
143 # have to send traffic out to the internet or rely
144 # on that. However, that breaks for a laptop that roams.
145 # So, we could have a cronjob that updates that dns,
146 # however, another solution is to just use ipv6,
149 # TODO: enable ipv6 for email. exim config setting disables it.
150 # need to add vpn support. need to add firewall / routing.
151 # I think exim will try ipv6 first, so no need to disable
155 e Running initial btrbk
156 if ! m btrbk-run
-v $bbk_args $mp_args; then
158 err
"failed initial btrbk"
162 m
$old_shell /a
/exe
/primary-setup
$new_hostname
165 if ! m btrbk-run
-v $bbk_args -m /o
; then
167 bang
="$(printf "$
(tput setaf
5)█$
(tput sgr0
)%.0s
" 1 2 3 4 5 6 7)"
168 e
$bang failed btrbk of
/o. restoring old
host as primary
169 m
$old_shell /a
/exe
/primary-setup localhost
173 m
$new_shell /a
/exe
/primary-setup localhost