2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
9 # Copyright 2024 Ian Kelling
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
15 # http://www.apache.org/licenses/LICENSE-2.0
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
24 set -e; .
/usr
/local
/lib
/bash-bear
; set +e
28 Usage: switch-mail-host|switch-host2 [OPTIONS] push|pull HOST
30 Turn off mail receiving on OLD_HOST, run btrbk to move mail to NEW_HOST,
31 turn on mail receiving on NEW_HOST. Assumes we want to move all
32 filesystems unless passing -o.
34 -a Avoid snapshot /a, /q, and similar. If we haven't
35 made any changes in the last hour, there is no
36 need to snapshot anything but /o, and we will
38 -i Disallow incremental backup.
39 -o Only btrbk /o, instead of all filesystems.
40 --force Run even though our local state does not say that MAIL_HOST is
41 us when pushing or HOST when pulling.
42 -h|--help Print help and exit.
44 I used to adjust home network dns so NEW_HOST resolves locally if it is
45 on the local network, but its simpler just not to and just rely
46 on the internet. Email can wait.
48 Note: Uses GNU getopt options parsing style
53 script_name
="${BASH_SOURCE[0]}"
54 script_name
="${script_name##*/}"
56 restore_new_btrbk
=false
57 restore_old_btrbk
=false
59 if $restore_new_btrbk; then
60 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
61 e
$new_shell systemctl start btrbk.timer
63 if $restore_old_btrbk; then
64 e WARNING
: due to failure
, btrbk.timer may need manual restoration
:
65 e
$old_shell systemctl start btrbk.timer
70 m
() { printf "$pre %s\n" "$*"; "$@"; }
71 e
() { printf "$pre %s\n" "$*"; }
72 err
() { echo "$pre ERROR: $*" >&2; }
73 die
() { printf "%s\n" "$*" >&2; echo "exiting with status 1" >&2; exit 1; }
75 if [[ $EUID != 0 ]]; then
76 err
"requires running as root"
81 ##### begin command line parsing ########
88 mp_args
="-m /o,/a,/q,/qd,/qr"
91 if ! temp
=$
(getopt
-l check-installed
,force
,pull-reexec
,help afioh
"$@"); then
92 err
"args invalid. args=$*"
97 -a) snapshot_arg
=resume
;;
105 -i) incremental_arg
="-i" ;;
106 # internal option for rerunning under newer old_host when doing pull
107 --pull-reexec) pull_reexec
=true
;;
112 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
118 if (( $# != 2 )) && ! $check_installed; then
119 err expected
2 args
, got
$#
122 if [[ ! $HOSTNAME ]]; then
123 err
"\$HOSTNAME is unset"
127 uninstalled-file-die
() {
128 die
"on host=$HOSTNAME, uninstalled file $1. run install-my-scripts or rerun with -f"
132 source /a
/bin
/bash_unpublished
/source-state
139 if [[ $MAIL_HOST != "$HOST2" ]]; then
147 err unexpected
script name
153 elif $host2_only; then
154 mp_args
="-m /a,/q,/qd,/qr"
158 if ! $force && { $check_installed ||
[[ $direction == push
]]; } ; then
165 for f
in ${install_bin_files[@]}; do
166 if ! diff -q /a
/bin
/ds
/$f /usr
/local
/bin
/$f; then
167 uninstalled-file-die
$f
170 if ! diff -q /a
/bin
/bash-bear-trap
/bash-bear
/usr
/local
/lib
/bash-bear
; then
171 uninstalled-file-die err
173 if $check_installed; then
182 old_hostname
=$HOSTNAME
184 bbk_args
="-t $new_host"
185 new_shell
="ssh -F $HOME/.ssh/confighome root@$new_host"
186 if ! new_hostname
=$
($new_shell hostname
); then
187 echo "$pre: error: failed ssh. retrying failed $new_shell with -v for more info:"
188 $new_shell -v hostname
194 new_hostname
=$HOSTNAME
195 bbk_args
="-s $old_host"
196 old_shell
="ssh -F $HOME/.ssh/confighome root@$old_host"
197 # tests ssh connection. crafted this to not need to do escape chars
199 if ! $mail_only && ! $pull_reexec ; then
201 if ! $old_shell switch-mail-host
--check-installed; then
202 die
"failed: $old_shell switch-mail-host --check-installed"
207 /usr
/local
/{bin
/{unsaved-buffers
{,.el
},switch-mail-host
},lib
/bash-bear
}
209 m scp
-F $HOME/.ssh
/confighome \
210 ${files[@]/#/root@$old_host:} $tmpd
212 for f
in ${files[@]}; do
213 if ! diff -q $tmpd/${f##*/} $f; then
214 m
install -T $tmpd/${f##*/} $f
219 e
"found different version on old_host=$old_host, reexecing"
220 m
/usr
/local
/bin
/switch-mail-host
--pull-reexec "${orig_args[@]}"
226 f
=/a
/bin
/bash_unpublished
/source-state
227 if ! old_info
=$
($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f"); then
228 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
229 $old_shell -v hostname
232 read -d '' -r old_hostname MAIL_HOST HOST2
<<<"$old_info" ||
(( $?
== 1 ))
236 err invalid first argument
244 if [[ $old_hostname != "$HOST2" ]]; then
245 err
"\$old_hostname($old_hostname) != \$HOST2($HOST2). Rerun with --force if you really want this."
248 elif [[ $old_hostname != "$MAIL_HOST" ]]; then
249 err
"\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
254 if [[ ! $new_host ||
! $old_host ]]; then
255 echo "$0: bad args. see script"
260 ########### end initial processing, begin actually modifying things ##########
262 if $new_shell systemctl is-active btrbk.timer
; then
263 m
$new_shell systemctl stop btrbk.timer
264 restore_new_btrbk
=true
266 if $old_shell systemctl is-active btrbk.timer
; then
267 m
$old_shell systemctl stop btrbk.timer
268 restore_old_btrbk
=true
271 btrbk_test
="systemctl is-active btrbk.service"
275 for shell
in "$new_shell" "$old_shell"; do
277 status
=$
($shell $btrbk_test) ||
:
279 inactive|failed
) : ;;
281 # This covers conditions like "activating", which still return 3 from
282 # systemctl is-active.
284 e
"btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
292 if ! $host2_only; then
293 # ensure these are unused before doing anything
294 e
"On $new_host: umounting /m and /o, checking emacs"
296 cat /usr
/local
/bin
/unsaved-buffers
297 if ! $host2_only; then
300 if mountpoint -q /\$dir; then
301 echo On $new_host: umount /\$dir
307 } |
$new_shell bash
-s
310 if ! $mail_only; then
311 cat /usr
/local
/bin
/unsaved-buffers
- <<'EOF' | $old_shell bash -s
313 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
314 # But if emacs ignores the signal, let it live.
315 pkill -xf 'emacs( --daemon| -f znc-all)' ||:
317 if [[ -e /p/profanity-here ]]; then
318 systemctl disable --now profanity
323 # previously, I was checking to see if the new mail host
324 # is on my home network, then changing my home dns
325 # to resolve on the local network, so that I didnt
326 # have to send traffic out to the internet or rely
327 # on that. However, that breaks for a laptop that roams.
328 # So, we could have a cronjob that updates that dns,
329 # however, another solution is to just use ipv6,
332 # TODO: enable ipv6 for email. exim config setting disables it.
333 # need to add vpn support. need to add firewall / routing.
334 # I think exim will try ipv6 first, so no need to disable
338 e Running initial btrbk
339 m btrbk-run
-v $bbk_args $force_arg $incremental_arg $mp_args $snapshot_arg || ret
=$?
341 err
"failed initial btrbk"
345 if ! $mail_only; then
346 m
$old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
347 m
$new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
351 if [[ $old_hostname != "$MAIL_HOST" && $old_hostname != kd
]]; then
352 m
$old_shell systemctl
--now disable btrbk.timer
354 m
$new_shell systemctl
--now enable btrbk.timer
355 if [[ -e /p
/profanity-here
]]; then
356 m
$new_shell systemctl
--now enable profanity
361 m
$old_shell /a
/exe
/primary-setup
$new_hostname || ret
=$?
363 err
"failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
369 m btrbk-run
-v --fast $bbk_args $force_arg $incremental_arg -m /o || ret
=$?
372 e
$bang failed btrbk of
/o. restoring old
host as primary
373 if ! m
$old_shell /a
/exe
/primary-setup localhost
; then
374 die
"due to failed btrbk of /o, we tried to restore old host as primary, but then we failed at that too. To resolve: Fix & rerun switch-mail-host, or fix and rerun primary-setup localhost on old shell so you have a working mail server and then rerun switch-mail-host."
376 e finished restoring old
host as primary
, now exiting
$ret due to earlier failed btrbk of
/o.
380 # new system is usable at this point
381 blocks
=██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
382 printf "%s\n" "${blocks:0:${COLUMNS:-100}}"
384 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
386 # 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
388 # 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
390 # 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
392 m
$new_shell /a
/exe
/primary-setup localhost || ret
=$?
394 err
"failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"