minor fix and add automatic updates
[distro-setup] / switch-primary-host
1 #!/bin/bash
2
3 # this pulls from host $1 to the current host.
4 # not currently used, but it might be useful at some point
5
6 source /a/bin/errhandle/err
7
8 cd /
9
10 old=$1
11 new=$HOSTNAME
12
13 if [[ ! $old ]]; then
14 echo "$0: error: no \$1 given, should be old host"
15 exit 1
16 fi
17
18 if ! timeout -s 9 2 ssh $old : ; then
19 echo "$0: error: can't ssh to $old"
20 fi
21
22 for p in emacs firefox pidgin; do
23 ssh $old killall $p ||:
24 done
25
26 # note: duplicated in check-subvol-stale
27 last_a=$(
28 vol=a
29 for s in /mnt/root/btrbk/$vol.*; do
30 f=${s##*/}
31 unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
32 printf "%s\n" $unix_time
33 done | sort -r | head -n 1
34 )
35
36 if [[ $last_a != [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]]; then
37 echo "$0: error: last_a bad value: $last_a"
38 exit 1
39 fi
40
41 # if last_a is recent enough, skip doing btrbk
42 if (( last_a < $(date +%s) - 60*60 )); then
43 if ! ssh $old btrbk-run -pvt $new; then
44 echo "$0: error: failed btrbk-run"
45 exit 1
46 fi
47 fi
48
49 switch-mail-host $old $new