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
49 m
() { printf "$pre %s\n" "$*"; "$@"; }
50 e
() { printf "$pre %s\n" "$*"; }
51 err
() { echo "$pre ERROR: $*" >&2; }
52 die
() { printf "%s\n" "$*" >&2; echo "exiting with status 1" >&2; exit 1; }
54 if [[ $EUID != 0 ]]; then
55 err
"requires running as root"
60 ##### begin command line parsing ########
67 mp_args
="-m /o,/a,/ar,/q,/qd,/qr"
70 if ! temp
=$
(getopt
-l check-installed
,force
,pull-reexec
,help afioh
"$@"); then
71 err
"args invalid. args=$*"
76 -a) snapshot_arg
=resume
;;
84 -i) incremental_arg
="-i" ;;
85 # internal option for rerunning under newer old_host when doing pull
86 --pull-reexec) pull_reexec
=true
;;
91 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
97 if (( $# != 2 )) && ! $check_installed; then
98 err expected
2 args
, got
$#
101 if [[ ! $HOSTNAME ]]; then
102 err
"\$HOSTNAME is unset"
106 uninstalled-file-die
() {
107 die
"on host=$HOSTNAME, uninstalled file $1. run install-my-scripts or rerun with -f"
111 source /a
/bin
/bash_unpublished
/source-state
117 if ! $force && { $check_installed ||
[[ $direction == push
]]; } ; then
124 for f
in ${install_bin_files[@]}; do
125 if ! diff -q /a
/bin
/ds
/$f /usr
/local
/bin
/$f; then
126 uninstalled-file-die
$f
129 if ! diff -q /a
/bin
/errhandle
/err
/usr
/local
/lib
/err
; then
130 uninstalled-file-die err
132 if $check_installed; then
141 old_hostname
=$HOSTNAME
143 bbk_args
="-t $new_host"
144 new_shell
="ssh -F $HOME/.ssh/confighome root@$new_host"
145 if ! new_hostname
=$
($new_shell hostname
); then
146 echo "$pre: error: failed ssh. retrying failed $new_shell with -v for more info:"
147 $new_shell -v hostname
153 new_hostname
=$HOSTNAME
154 bbk_args
="-s $old_host"
155 old_shell
="ssh -F $HOME/.ssh/confighome root@$old_host"
156 # tests ssh connection. crafted this to not need to do escape chars
158 if ! $mail_only && ! $pull_reexec ; then
160 if ! $old_shell switch-mail-host
--check-installed; then
161 die
"failed: $old_shell switch-mail-host --check-installed"
165 m scp
-F $HOME/.ssh
/confighome root@
$old_host:/usr
/local
/bin
/switch-mail-host
$tmpf
166 if ! diff -q $tmpf ${BASH_SOURCE[0]}; then
167 e
"found different version on old_host=$old_hostname, reexecing"
168 m
install -T $tmpf /usr
/local
/bin
/switch-mail-host
169 m
/usr
/local
/bin
/switch-mail-host
--pull-reexec "${orig_args[@]}"
174 f
=/a
/bin
/bash_unpublished
/source-state
175 if ! old_info
=$
($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f"); then
176 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
177 $old_shell -v hostname
180 read -d '' -r old_hostname MAIL_HOST HOST2
<<<"$old_info" ||
(( $?
== 1 ))
184 err invalid first argument
191 if [[ $MAIL_HOST != "$HOST2" ]]; then
199 err unexpected
script name
205 elif $host2_only; then
206 mp_args
="-m /a,/ar,/q,/qd,/qr"
211 if [[ $old_hostname != "$HOST2" ]]; then
212 err
"\$old_hostname($old_hostname) != \$HOST2($HOST2). Rerun with --force if you really want this."
215 elif [[ $old_hostname != "$MAIL_HOST" ]]; then
216 err
"\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
221 if [[ ! $new_host ||
! $old_host ]]; then
222 echo "$0: bad args. see script"
227 ########### end initial processing, begin actually modifying things ##########
229 if $new_shell systemctl is-active btrbk.timer
; then
230 m
$new_shell systemctl stop btrbk.timer
231 restore_new_btrbk
=true
233 if $old_shell systemctl is-active btrbk.timer
; then
234 m
$old_shell systemctl stop btrbk.timer
235 restore_old_btrbk
=true
238 btrbk_test
="systemctl is-active btrbk.service"
242 for shell
in "$new_shell" "$old_shell"; do
244 status
=$
($shell $btrbk_test) ||
:
246 inactive|failed
) : ;;
248 # This covers conditions like "activating", which still return 3 from
249 # systemctl is-active.
251 e
"btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
259 # ensure these are unused before doing anything
260 e
"On $new_host: umounting /m and /o, checking emacs"
264 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
265 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/)"$//')"
267 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
272 if ! $host2_only; then
275 if mountpoint -q /\$dir; then
276 echo On $new_host: umount /\$dir
282 } |
$new_shell bash
-s
284 $old_shell bash
-s <<'EOF'
285 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
286 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/)"$//')"
288 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
294 # previously, I was checking to see if the new mail host
295 # is on my home network, then changing my home dns
296 # to resolve on the local network, so that I didnt
297 # have to send traffic out to the internet or rely
298 # on that. However, that breaks for a laptop that roams.
299 # So, we could have a cronjob that updates that dns,
300 # however, another solution is to just use ipv6,
303 # TODO: enable ipv6 for email. exim config setting disables it.
304 # need to add vpn support. need to add firewall / routing.
305 # I think exim will try ipv6 first, so no need to disable
309 e Running initial btrbk
310 m btrbk-run
-v $bbk_args $force_arg $incremental_arg $mp_args $snapshot_arg || ret
=$?
312 err
"failed initial btrbk"
316 if ! $mail_only; then
317 m
$old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
318 m
$new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
322 if [[ $old_hostname != "$MAIL_HOST" && $old_hostname != kd
]]; then
323 m
$old_shell systemctl
--now disable btrbk.timer
325 m
$new_shell systemctl
--now enable btrbk.timer
329 m
$old_shell /a
/exe
/primary-setup
$new_hostname || ret
=$?
331 err
"failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
335 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
336 # But if emacs ignores the signal, let it live.
337 m
$new_shell killall
-q emacs ||
:
340 m btrbk-run
-v --fast $bbk_args $force_arg $incremental_arg -m /o || ret
=$?
342 bang
="$(printf "$
(tput setaf
5)█$
(tput sgr0
)%.0s
" 1 2 3 4 5 6 7)"
343 e
$bang failed btrbk of
/o. restoring old
host as primary
344 m
$old_shell /a
/exe
/primary-setup localhost
348 # new system is usable at this point
349 blocks
=██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
350 printf "%s\n" "$(tput setaf 5 2>/dev/null ||:)${blocks:0:${COLUMNS:-180}}$(tput sgr0 2>/dev/null||:)"
352 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
354 # 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
356 # 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
358 # 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
360 m
$new_shell /a
/exe
/primary-setup localhost || ret
=$?
362 err
"failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"