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
138 if ! $force && { $check_installed ||
[[ $direction == push
]]; } ; then
145 for f
in ${install_bin_files[@]}; do
146 if ! diff -q /a
/bin
/ds
/$f /usr
/local
/bin
/$f; then
147 uninstalled-file-die
$f
150 if ! diff -q /a
/bin
/bash-bear-trap
/bash-bear
/usr
/local
/lib
/bash-bear
; then
151 uninstalled-file-die err
153 if $check_installed; then
162 old_hostname
=$HOSTNAME
164 bbk_args
="-t $new_host"
165 new_shell
="ssh -F $HOME/.ssh/confighome root@$new_host"
166 if ! new_hostname
=$
($new_shell hostname
); then
167 echo "$pre: error: failed ssh. retrying failed $new_shell with -v for more info:"
168 $new_shell -v hostname
174 new_hostname
=$HOSTNAME
175 bbk_args
="-s $old_host"
176 old_shell
="ssh -F $HOME/.ssh/confighome root@$old_host"
177 # tests ssh connection. crafted this to not need to do escape chars
179 if ! $mail_only && ! $pull_reexec ; then
181 if ! $old_shell switch-mail-host
--check-installed; then
182 die
"failed: $old_shell switch-mail-host --check-installed"
187 /usr
/local
/{bin
/{unsaved-buffers
{,.el
},switch-mail-host
},lib
/bash-bear
}
189 m scp
-F $HOME/.ssh
/confighome \
190 ${files[@]/#/root@$old_host:} $tmpd
192 for f
in ${files[@]}; do
193 if ! diff -q $tmpd/${f##*/} $f; then
194 m
install -T $tmpd/${f##*/} $f
199 e
"found different version on old_host=$old_hostname, reexecing"
200 m
/usr
/local
/bin
/switch-mail-host
--pull-reexec "${orig_args[@]}"
206 f
=/a
/bin
/bash_unpublished
/source-state
207 if ! old_info
=$
($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f"); then
208 echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:"
209 $old_shell -v hostname
212 read -d '' -r old_hostname MAIL_HOST HOST2
<<<"$old_info" ||
(( $?
== 1 ))
216 err invalid first argument
223 if [[ $MAIL_HOST != "$HOST2" ]]; then
231 err unexpected
script name
237 elif $host2_only; then
238 mp_args
="-m /a,/q,/qd,/qr"
243 if [[ $old_hostname != "$HOST2" ]]; then
244 err
"\$old_hostname($old_hostname) != \$HOST2($HOST2). Rerun with --force if you really want this."
247 elif [[ $old_hostname != "$MAIL_HOST" ]]; then
248 err
"\$old_hostname($old_hostname) != \$MAIL_HOST($MAIL_HOST). Rerun with --force if you really want this."
253 if [[ ! $new_host ||
! $old_host ]]; then
254 echo "$0: bad args. see script"
259 ########### end initial processing, begin actually modifying things ##########
261 if $new_shell systemctl is-active btrbk.timer
; then
262 m
$new_shell systemctl stop btrbk.timer
263 restore_new_btrbk
=true
265 if $old_shell systemctl is-active btrbk.timer
; then
266 m
$old_shell systemctl stop btrbk.timer
267 restore_old_btrbk
=true
270 btrbk_test
="systemctl is-active btrbk.service"
274 for shell
in "$new_shell" "$old_shell"; do
276 status
=$
($shell $btrbk_test) ||
:
278 inactive|failed
) : ;;
280 # This covers conditions like "activating", which still return 3 from
281 # systemctl is-active.
283 e
"btrbk active on shell:$shell, status:$status, sleeping 8 seconds"
291 # ensure these are unused before doing anything
292 e
"On $new_host: umounting /m and /o, checking emacs"
294 cat /usr
/local
/bin
/unsaved-buffers
295 if ! $host2_only; then
298 if mountpoint -q /\$dir; then
299 echo On $new_host: umount /\$dir
305 } |
$new_shell bash
-s
307 if ! $mail_only; then
308 cat /usr
/local
/bin
/unsaved-buffers
- <<'EOF' | $old_shell bash -s
310 # Try to prevent emacs from saving stale data it has in memory to disk. eg: files, recentf list, etc.
311 # But if emacs ignores the signal, let it live.
312 pkill -xf 'emacs( --daemon| -f znc-all)' ||:
314 if [[ -e /p/profanity-here ]]; then
315 systemctl disable --now profanity
320 # previously, I was checking to see if the new mail host
321 # is on my home network, then changing my home dns
322 # to resolve on the local network, so that I didnt
323 # have to send traffic out to the internet or rely
324 # on that. However, that breaks for a laptop that roams.
325 # So, we could have a cronjob that updates that dns,
326 # however, another solution is to just use ipv6,
329 # TODO: enable ipv6 for email. exim config setting disables it.
330 # need to add vpn support. need to add firewall / routing.
331 # I think exim will try ipv6 first, so no need to disable
335 e Running initial btrbk
336 m btrbk-run
-v $bbk_args $force_arg $incremental_arg $mp_args $snapshot_arg || ret
=$?
338 err
"failed initial btrbk"
342 if ! $mail_only; then
343 m
$old_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
344 m
$new_shell sed -ri "s/HOST2=.*/HOST2=$new_hostname/" /a
/bin
/bash_unpublished
/source-state
348 if [[ $old_hostname != "$MAIL_HOST" && $old_hostname != kd
]]; then
349 m
$old_shell systemctl
--now disable btrbk.timer
351 m
$new_shell systemctl
--now enable btrbk.timer
352 if [[ -e /p
/profanity-here
]]; then
353 m
$new_shell systemctl
--now enable profanity
358 m
$old_shell /a
/exe
/primary-setup
$new_hostname || ret
=$?
360 err
"failed \$old_shell primary-setup \$new_hostname. fix and rerun $script_name"
366 m btrbk-run
-v --fast $bbk_args $force_arg $incremental_arg -m /o || ret
=$?
369 e
$bang failed btrbk of
/o. restoring old
host as primary
370 if ! m
$old_shell /a
/exe
/primary-setup localhost
; then
371 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."
373 e finished restoring old
host as primary
, now exiting
$ret due to earlier failed btrbk of
/o.
377 # new system is usable at this point
378 blocks
=██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
379 printf "%s\n" "${blocks:0:${COLUMNS:-100}}"
381 # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail:
383 # 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
385 # 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
387 # 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
389 m
$new_shell /a
/exe
/primary-setup localhost || ret
=$?
391 err
"failed final primary-setup, just fix and rerun: $new_shell /a/exe/primary-setup localhost"
395 if ! $mail_only && [[ -e /p
/profanity-here
]]; then
396 m
$new_shell systemctl
--now enable profanity || ret
=$?
398 err
"failed final systemctl --now enable profanity, just fix and rerun"