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
152 e
"On $new_host: umounting /m and /o, checking emacs"
153 $new_shell bash
-s <<'EOF'
155 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
156 export XDG_RUNTIME_DIR=/run/user/1000
157 bufs="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
159 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
164 if mountpoint -q /$dir; then
165 echo On $new_host: umount /$dir
171 $old_shell bash
-s <<'EOF'
172 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
173 export XDG_RUNTIME_DIR=/run/user/1000
174 bufs="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
176 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
182 # previously, I was checking to see if the new mail host
183 # is on my home network, then changing my home dns
184 # to resolve on the local network, so that I didnt
185 # have to send traffic out to the internet or rely
186 # on that. However, that breaks for a laptop that roams.
187 # So, we could have a cronjob that updates that dns,
188 # however, another solution is to just use ipv6,
191 # TODO: enable ipv6 for email. exim config setting disables it.
192 # need to add vpn support. need to add firewall / routing.
193 # I think exim will try ipv6 first, so no need to disable
197 e Running initial btrbk
198 if ! m btrbk-run
-v $bbk_args $incremental_arg $mp_args; then
200 err
"failed initial btrbk"
204 if ! m
$old_shell /a
/exe
/primary-setup
$new_hostname; then
206 err
"failed \$old_shell primary-setup \$new_hostname. fix and rerun switch-mail-host"
210 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
211 # But if emacs ignores the signal, let it live.
212 m
$new_shell killall
-q emacs ||
:
215 m btrbk-run
-v $bbk_args $incremental_arg -m /o || ret
=$?
217 bang
="$(printf "$
(tput setaf
5)█$
(tput sgr0
)%.0s
" 1 2 3 4 5 6 7)"
218 e
$bang failed btrbk of
/o. restoring old
host as primary
219 m
$old_shell /a
/exe
/primary-setup localhost
223 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
225 # 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
227 # 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
229 # 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
231 if ! m
$new_shell /a
/exe
/primary-setup localhost
; then
233 err
"failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"