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 --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 $new_shell -v hostname
96 new_hostname
=$
($new_shell hostname
)
101 new_hostname
=$HOSTNAME
102 bbk_args
="-s $old_host"
103 old_shell
="ssh -F $HOME/.ssh/confighome root@$old_host"
104 # tests ssh connection. crafted this to not need to do escape chars
105 f
=/a
/bin
/bash_unpublished
/source-state
106 if ! old_info
=($
($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f")); then
107 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
108 $old_shell -v hostname
111 old_hostname
=${old_info[0]}
112 MAIL_HOST
=${old_info[1]}
116 err invalid first argument
123 if [[ $MAIL_HOST != "$HOST2" ]]; then
131 err unexpected
script name
138 elif $host2_only; then
139 mp_args
="-m /a,/ar,/q,/qr"
144 if [[ $old_hostname != "$MAIL_HOST" ]] && ! $force; then
145 err
"\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
149 if [[ ! $new_host ||
! $old_host ]]; then
150 echo "$0: bad args. see script"
155 ########### end initial processing, begin actually modifying things ##########
157 if $new_shell systemctl is-active btrbk.timer
; then
158 m
$new_shell systemctl stop btrbk.timer
159 restore_new_btrbk
=true
161 if $old_shell systemctl is-active btrbk.timer
; then
162 m
$old_shell systemctl stop btrbk.timer
163 restore_old_btrbk
=true
166 btrbk_test
="systemctl is-active btrbk.service"
170 for shell
in "$new_shell" "$old_shell"; do
172 status
=$
($shell $btrbk_test) ||
:
174 inactive|failed
) : ;;
176 # This covers conditions like "activating", which still return 3 from
177 # systemctl is-active.
179 e
"btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
187 # ensure these are unused before doing anything
188 e
"On $new_host: umounting /m and /o, checking emacs"
192 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
193 export XDG_RUNTIME_DIR=/run/user/1000
194 bufs="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
196 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
201 if ! $host2_only; then
204 if mountpoint -q /$dir; then
205 echo On $new_host: umount /$dir
211 } |
$new_shell bash
-s
213 $old_shell bash
-s <<'EOF'
214 if pgrep -G iank -u iank -f 'emacs --daemon' &>/dev/null; then
215 export XDG_RUNTIME_DIR=/run/user/1000
216 bufs="$(emacsclient --eval "$(cat /a/bin/ds/unsaved-buffers.el)"| sed '/^"nil"$/d;s/^"(/E: /;s/)"$//')"
218 echo "error: on $HOSTNAME, unsaved emacs files: $bufs" >&2
224 # previously, I was checking to see if the new mail host
225 # is on my home network, then changing my home dns
226 # to resolve on the local network, so that I didnt
227 # have to send traffic out to the internet or rely
228 # on that. However, that breaks for a laptop that roams.
229 # So, we could have a cronjob that updates that dns,
230 # however, another solution is to just use ipv6,
233 # TODO: enable ipv6 for email. exim config setting disables it.
234 # need to add vpn support. need to add firewall / routing.
235 # I think exim will try ipv6 first, so no need to disable
239 e Running initial btrbk
240 if ! m btrbk-run
-v $bbk_args $incremental_arg $mp_args; then
242 err
"failed initial btrbk"
246 if ! $mail_only; then
247 m
$old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
248 m
$new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
255 if ! m
$old_shell /a
/exe
/primary-setup
$new_hostname; then
257 err
"failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
261 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
262 # But if emacs ignores the signal, let it live.
263 m
$new_shell killall
-q emacs ||
:
266 m btrbk-run
-v $bbk_args $incremental_arg -m /o || ret
=$?
268 bang
="$(printf "$
(tput setaf
5)█$
(tput sgr0
)%.0s
" 1 2 3 4 5 6 7)"
269 e
$bang failed btrbk of
/o. restoring old
host as primary
270 m
$old_shell /a
/exe
/primary-setup localhost
274 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
276 # 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
278 # 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
280 # 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
282 if ! m
$new_shell /a
/exe
/primary-setup localhost
; then
284 err
"failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"