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 -a Avoid snapshot /a, /q, and similar. If we haven't
14 made any changes in the last hour, there is no
15 need to snapshot anything but /o, and we will
17 -i Disallow incremental backup.
18 -o Only btrbk /o, instead of all filesystems.
19 --force Run even though our local state does not say that MAIL_HOST is
20 us when pushing or HOST when pulling.
21 -h|--help Print help and exit.
23 I used to adjust home network dns so NEW_HOST resolves locally if it is
24 on the local network, but its simpler just not to and just rely
25 on the internet. Email can wait.
27 Note: Uses GNU getopt options parsing style
32 script_name
="${BASH_SOURCE[0]}"
33 script_name
="${script_name##*/}"
35 restore_new_btrbk
=false
36 restore_old_btrbk
=false
38 if $restore_new_btrbk; then
39 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
40 e
$new_shell systemctl start btrbk.timer
42 if $restore_old_btrbk; then
43 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
44 e
$old_shell systemctl start btrbk.timer
48 pre
="${SSH_CLIENT:+$HOSTNAME} $script_name:"
49 m
() { printf "$pre %s\n" "$*"; "$@"; }
50 e
() { printf "$pre %s\n" "$*"; }
51 err
() { echo "$pre ERROR: $*" >&2; }
53 if [[ $EUID != 0 ]]; then
54 err
"requires running as root"
59 ##### begin command line parsing ########
64 mp_args
="-m /o,/a,/ar,/q,/qr"
65 temp
=$
(getopt
-l force
,help aioh
"$@") || usage
1
69 -a) snapshot_arg
=resume
;;
70 --force) force
=true
;;
71 -i) incremental_arg
="-i" ;;
76 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
82 (( $# == 2 )) || usage
1
84 if [[ ! $HOSTNAME ]]; then
85 err
"\$HOSTNAME is unset"
89 source /a
/bin
/bash_unpublished
/source-state
96 old_hostname
=$HOSTNAME
98 bbk_args
="-t $new_host"
99 new_shell
="ssh -F $HOME/.ssh/confighome root@$new_host"
100 if ! new_hostname
=$
($new_shell hostname
); then
101 echo "$pre: error: failed ssh. retrying failed $new_shell with -v for more info:"
102 $new_shell -v hostname
108 new_hostname
=$HOSTNAME
109 bbk_args
="-s $old_host"
110 old_shell
="ssh -F $HOME/.ssh/confighome root@$old_host"
111 old_shelliank
="ssh -F $HOME/.ssh/confighome iank@$old_host"
112 # tests ssh connection. crafted this to not need to do escape chars
113 f
=/a
/bin
/bash_unpublished
/source-state
114 if ! old_info
=($
($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f")); then
115 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
116 $old_shell -v hostname
119 old_hostname
=${old_info[0]}
120 MAIL_HOST
=${old_info[1]}
124 err invalid first argument
131 if [[ $MAIL_HOST != "$HOST2" ]]; then
139 err unexpected
script name
145 elif $host2_only; then
146 mp_args
="-m /a,/ar,/q,/qr"
151 if [[ $old_hostname != "$HOST2" ]]; then
152 err
"\$old_hostname($old_hostname) != \$HOST2($HOST2). Rerun with --force if you really want this."
155 elif [[ $old_hostname != "$MAIL_HOST" ]]; then
156 err
"\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
161 if [[ ! $new_host ||
! $old_host ]]; then
162 echo "$0: bad args. see script"
167 ########### end initial processing, begin actually modifying things ##########
169 if $new_shell systemctl is-active btrbk.timer
; then
170 m
$new_shell systemctl stop btrbk.timer
171 restore_new_btrbk
=true
173 if $old_shell systemctl is-active btrbk.timer
; then
174 m
$old_shell systemctl stop btrbk.timer
175 restore_old_btrbk
=true
178 btrbk_test
="systemctl is-active btrbk.service"
182 for shell
in "$new_shell" "$old_shell"; do
184 status
=$
($shell $btrbk_test) ||
:
186 inactive|failed
) : ;;
188 # This covers conditions like "activating", which still return 3 from
189 # systemctl is-active.
191 e
"btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
199 # ensure these are unused before doing anything
200 e
"On $new_host: umounting /m and /o, checking emacs"
204 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
205 bufs="$(sudo -u iank env XDG_RUNTIME_DIR=/run/user/1000 emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
207 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
212 if ! $host2_only; then
215 if mountpoint -q /\$dir; then
216 echo On $new_host: umount /\$dir
222 } |
$new_shell bash
-s
224 $old_shell bash
-s <<'EOF'
225 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
226 bufs="$(sudo -u iank env XDG_RUNTIME_DIR=/run/user/1000 emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
228 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
234 # previously, I was checking to see if the new mail host
235 # is on my home network, then changing my home dns
236 # to resolve on the local network, so that I didnt
237 # have to send traffic out to the internet or rely
238 # on that. However, that breaks for a laptop that roams.
239 # So, we could have a cronjob that updates that dns,
240 # however, another solution is to just use ipv6,
243 # TODO: enable ipv6 for email. exim config setting disables it.
244 # need to add vpn support. need to add firewall / routing.
245 # I think exim will try ipv6 first, so no need to disable
249 e Running initial btrbk
250 m btrbk-run
-v $bbk_args $incremental_arg $mp_args $snapshot_arg || ret
=$?
252 err
"failed initial btrbk"
256 if ! $mail_only; then
257 m
$old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
258 m
$new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
262 if [[ $old_hostname != "$MAIL_HOST" && $old_hostname != kd
]]; then
263 m
$old_shell systemctl
--now disable btrbk.timer
265 m
$new_shell systemctl
--now enable btrbk.timer
269 m
$old_shell /a
/exe
/primary-setup
$new_hostname || ret
=$?
271 err
"failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
275 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
276 # But if emacs ignores the signal, let it live.
277 m
$new_shell killall
-q emacs ||
:
280 m btrbk-run
-v --fast $bbk_args $incremental_arg -m /o || ret
=$?
282 bang
="$(printf "$
(tput setaf
5)█$
(tput sgr0
)%.0s
" 1 2 3 4 5 6 7)"
283 e
$bang failed btrbk of
/o. restoring old
host as primary
284 m
$old_shell /a
/exe
/primary-setup localhost
288 # new system is usable at this point
289 printf "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s" $
(eval echo "{1..${COLUMNS:-60}}")
292 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
294 # 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
296 # 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
298 # 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
300 m
$new_shell /a
/exe
/primary-setup localhost || ret
=$?
302 err
"failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"