3 source /usr
/local
/lib
/err
7 Usage: switch-mail-host|switch-host2 [OPTIONS] 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 -i Disallow incremental backup.
14 -o Only btrbk /o, instead of all filesystems.
15 --force Run even though our local state does not say that MAIL_HOST is
16 us when pushing or HOST when pulling.
17 -h|--help Print help and exit.
19 I used to adjust home network dns so NEW_HOST resolves locally if it is
20 on the local network, but its simpler just not to and just rely
21 on the internet. Email can wait.
23 Note: Uses GNU getopt options parsing style
28 script_name
="${BASH_SOURCE[0]}"
29 script_name
="${script_name##*/}"
31 restore_new_btrbk
=false
32 restore_old_btrbk
=false
34 if $restore_new_btrbk; then
35 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
36 e
$new_shell systemctl start btrbk.timer
38 if $restore_old_btrbk; then
39 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
40 e
$old_shell systemctl start btrbk.timer
45 m
() { printf "$pre %s\n" "$*"; "$@"; }
46 e
() { printf "$pre %s\n" "$*"; }
47 err
() { echo "$pre ERROR: $*" >&2; }
49 if [[ $EUID != 0 ]]; then
50 err
"requires running as root"
55 ##### begin command line parsing ########
61 temp
=$
(getopt
-l force
,help ioh
"$@") || usage
1
65 --force) force
=true
;;
66 -i) incremental_arg
="-i" ;;
71 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
77 (( $# == 2 )) || usage
1
79 if [[ ! $HOSTNAME ]]; then
80 err
"\$HOSTNAME is unset"
84 source /a
/bin
/bash_unpublished
/source-state
91 old_hostname
=$HOSTNAME
93 bbk_args
="-t $new_host"
94 new_shell
="ssh -F $HOME/.ssh/confighome root@$new_host"
95 if ! new_hostname
=$
($new_shell hostname
); then
96 echo "$pre: error: failed ssh. retrying failed $new_shell with -v for more info:"
97 $new_shell -v hostname
103 new_hostname
=$HOSTNAME
104 bbk_args
="-s $old_host"
105 old_shell
="ssh -F $HOME/.ssh/confighome root@$old_host"
106 old_shelliank
="ssh -F $HOME/.ssh/confighome iank@$old_host"
107 # tests ssh connection. crafted this to not need to do escape chars
108 f
=/a
/bin
/bash_unpublished
/source-state
109 if ! old_info
=($
($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f")); then
110 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
111 $old_shell -v hostname
114 old_hostname
=${old_info[0]}
115 MAIL_HOST
=${old_info[1]}
119 err invalid first argument
126 if [[ $MAIL_HOST != "$HOST2" ]]; then
134 err unexpected
script name
140 elif $host2_only; then
141 mp_args
="-m /a,/ar,/q,/qr"
146 if [[ $old_hostname != "$HOST2" ]]; then
147 err
"\$old_hostname($old_hostname) != \$HOST2($HOST2). Rerun with --force if you really want this."
150 elif [[ $old_hostname != "$MAIL_HOST" ]] && ! $force; then
151 err
"\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
155 if [[ ! $new_host ||
! $old_host ]]; then
156 echo "$0: bad args. see script"
161 ########### end initial processing, begin actually modifying things ##########
163 if $new_shell systemctl is-active btrbk.timer
; then
164 m
$new_shell systemctl stop btrbk.timer
165 restore_new_btrbk
=true
167 if $old_shell systemctl is-active btrbk.timer
; then
168 m
$old_shell systemctl stop btrbk.timer
169 restore_old_btrbk
=true
172 btrbk_test
="systemctl is-active btrbk.service"
176 for shell
in "$new_shell" "$old_shell"; do
178 status
=$
($shell $btrbk_test) ||
:
180 inactive|failed
) : ;;
182 # This covers conditions like "activating", which still return 3 from
183 # systemctl is-active.
185 e
"btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
193 # ensure these are unused before doing anything
194 e
"On $new_host: umounting /m and /o, checking emacs"
198 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
199 bufs="$(sudo -u iank emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
201 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
206 if ! $host2_only; then
209 if mountpoint -q /$dir; then
210 echo On $new_host: umount /$dir
216 } |
$new_shell bash
-s
218 $old_shell bash
-s <<'EOF'
219 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
220 bufs="$(sudo -u iank emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
222 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
228 # previously, I was checking to see if the new mail host
229 # is on my home network, then changing my home dns
230 # to resolve on the local network, so that I didnt
231 # have to send traffic out to the internet or rely
232 # on that. However, that breaks for a laptop that roams.
233 # So, we could have a cronjob that updates that dns,
234 # however, another solution is to just use ipv6,
237 # TODO: enable ipv6 for email. exim config setting disables it.
238 # need to add vpn support. need to add firewall / routing.
239 # I think exim will try ipv6 first, so no need to disable
243 e Running initial btrbk
244 if ! m btrbk-run
-v $bbk_args $incremental_arg $mp_args; then
246 err
"failed initial btrbk"
250 if ! $mail_only; then
251 m
$old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
252 m
$new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
256 if [[ $old_hostname != "$MAIL_HOST" ]]; then
257 m
$old_shell systemctl
--now disable btrbk.timer
259 m
$new_shell systemctl
--now enable btrbk.timer
263 if ! m
$old_shell /a
/exe
/primary-setup
$new_hostname; then
265 err
"failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
269 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
270 # But if emacs ignores the signal, let it live.
271 m
$new_shell killall
-q emacs ||
:
274 m btrbk-run
-v $bbk_args $incremental_arg -m /o || ret
=$?
276 bang
="$(printf "$
(tput setaf
5)█$
(tput sgr0
)%.0s
" 1 2 3 4 5 6 7)"
277 e
$bang failed btrbk of
/o. restoring old
host as primary
278 m
$old_shell /a
/exe
/primary-setup localhost
282 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
284 # die=false; for d in o.leaf.2021-05-29T10:02:08-0400/m/{4e,md,4e2}/{,l/}!(*myarchive)/new; do if $die; then break; fi; find $d -type f -mtime -5 | while read -r f; do dir="${f%new/*}"; dir="btrbk/o.20210530T000011-0400/${dir#*/}"; fname="${f##*/}"; [[ -e $dir/new/$fname || -e $dir/cur/$fname ]] && continue; if ! e cp -a $f /${dir#*/*/}new; then echo failed cp; die=true; break; fi ; done; done
286 # once I accidentally sent mail from non-main mail host. to copy into the main mail host's sent dir, cd into dir of non-mail mail host Sent/cur, then
288 # shopt -s nullglob; find . -type f -mtime -2 | while read -r f; do a=( /m/4e/Sent/cur/${f%,*}* ); if (( ${#a[@]} )); then e exists $a; else m cp -a $f /m/4e/Sent/cur; fi; done
290 if ! m
$new_shell /a
/exe
/primary-setup localhost
; then
292 err
"failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"