3 source /usr
/local
/lib
/err
7 Usage: ${0##*/} [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 -h|--help Print help and exit.
17 I used to adjust home network dns so NEW_HOST resolves locally if it is
18 on the local network, but its simpler just not to and just rely
19 on the internet. Email can wait.
21 Note: Uses GNU getopt options parsing style
27 restore_new_btrbk
=false
28 restore_old_btrbk
=false
30 if $restore_new_btrbk; then
31 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
32 e
$new_shell systemctl start btrbk.timer
34 if $restore_old_btrbk; then
35 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
36 e
$old_shell systemctl start btrbk.timer
41 m
() { printf "$pre %s\n" "$*"; "$@"; }
42 e
() { printf "$pre %s\n" "$*"; }
43 err
() { echo "$pre ERROR: $*" >&2; }
45 if [[ $EUID != 0 ]]; then
46 err
"requires running as root"
51 ##### begin command line parsing ########
55 temp
=$
(getopt
-l force
,help ioh
"$@") || usage
1
59 --force) force
=true
;;
60 -i) incremental_arg
="-i" ;;
61 -o) mp_args
="-m /o" ;;
64 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
70 (( $# == 2 )) || usage
1
72 if [[ ! $HOSTNAME ]]; then
73 err
"\$HOSTNAME is unset"
80 old_hostname
=$HOSTNAME
82 bbk_args
="-t $new_host"
83 new_shell
="ssh -F $HOME/.ssh/confighome root@$new_host"
84 $new_shell -v hostname
85 new_hostname
=$
($new_shell hostname
)
90 new_hostname
=$HOSTNAME
91 bbk_args
="-s $old_host"
92 old_shell
="ssh -F $HOME/.ssh/confighome root@$old_host"
93 # tests ssh connection
94 if ! old_hostname
=$
($old_shell hostname
); then
95 echo "retrying failed $old_shell with -v"
96 $old_shell -v hostname
101 err invalid first argument
106 source /a
/bin
/bash_unpublished
/source-state
108 if [[ $old_hostname != "$MAIL_HOST" ]] && ! $force; then
109 err
"\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
113 if [[ ! $new_host ||
! $old_host ]]; then
114 echo "$0: bad args. see script"
119 ########### end initial processing, begin actually modifying things ##########
121 if $new_shell systemctl is-active btrbk.timer
; then
122 m
$new_shell systemctl stop btrbk.timer
123 restore_new_btrbk
=true
125 if $old_shell systemctl is-active btrbk.timer
; then
126 m
$old_shell systemctl stop btrbk.timer
127 restore_old_btrbk
=true
130 btrbk_test
="systemctl is-active btrbk.service"
134 for shell
in "$new_shell" "$old_shell"; do
136 status
=$
($shell $btrbk_test) ||
:
138 inactive|failed
) : ;;
140 # This covers conditions like "activating", which still return 3 from
141 # systemctl is-active.
143 e
"btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
151 # ensure these are unused before doing anything
153 e
"On $new_host: umounting /m and /o, checking emacs"
154 $new_shell bash
-s <<'EOF'
156 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
157 export XDG_RUNTIME_DIR=/run/user/1000
158 bufs="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
160 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
165 if mountpoint -q /$dir; then
166 echo On $new_host: umount /$dir
172 $old_shell bash
-s <<'EOF'
173 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
174 export XDG_RUNTIME_DIR=/run/user/1000
175 bufs="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
177 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
183 # previously, I was checking to see if the new mail host
184 # is on my home network, then changing my home dns
185 # to resolve on the local network, so that I didnt
186 # have to send traffic out to the internet or rely
187 # on that. However, that breaks for a laptop that roams.
188 # So, we could have a cronjob that updates that dns,
189 # however, another solution is to just use ipv6,
192 # TODO: enable ipv6 for email. exim config setting disables it.
193 # need to add vpn support. need to add firewall / routing.
194 # I think exim will try ipv6 first, so no need to disable
198 e Running initial btrbk
199 if ! m btrbk-run
-v $bbk_args $incremental_arg $mp_args; then
201 err
"failed initial btrbk"
205 if ! m
$old_shell /a
/exe
/primary-setup
$new_hostname; then
207 err
"failed \$old_shell primary-setup \$new_hostname. fix and rerun switch-mail-host"
211 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
212 # But if emacs ignores the signal, let it live.
213 m
$new_shell killall
-q emacs ||
:
216 m btrbk-run
-v $bbk_args $incremental_arg -m /o || ret
=$?
218 bang
="$(printf "$
(tput setaf
5)█$
(tput sgr0
)%.0s
" 1 2 3 4 5 6 7)"
219 e
$bang failed btrbk of
/o. restoring old
host as primary
220 m
$old_shell /a
/exe
/primary-setup localhost
224 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
226 # 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
228 # 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
230 # 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
232 if ! m
$new_shell /a
/exe
/primary-setup localhost
; then
234 err
"failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"