forked from wiedehopf/readsb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicate_state.sh
executable file
·68 lines (48 loc) · 2.15 KB
/
replicate_state.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
set -e
# ssh access required for both the source and target box
# this script will transfer the traces for last 24h and current aircraft positions
# it reads the state from the source and will replace the target state except for aircraft that exist on the target but not on the source
# source
SHOST=root@box1
SDIR=/run/readsb
# target
THOST=root@box2
TDIR=/run/readsb
echo "$(date -u --rfc-3339=s) starting transfer from $SHOST to $THOST, 10 seconds to cancel with Ctrl-C"
sleep 10
SSHDIR="$HOME/.vee0za6ugohru6Id0ziK3ahv1ietahva"
rm -rf "$SSHDIR"
mkdir -p "$SSHDIR"
chmod 700 "$SSHDIR"
SSHPERSIST="-o ControlMaster=auto -o ControlPersist=30s"
SSHCOMMON="-o StrictHostKeyChecking=no"
P1="-S $SSHDIR/1cm-%r@%h:%p"
P2="-S $SSHDIR/2cm-%r@%h:%p"
SCMD="ssh $SHOST $SSHCOMMON $SSHPERSIST $P1"
TCMD="ssh $THOST $SSHCOMMON $SSHPERSIST $P2"
if ! $SCMD "cd $SDIR/getState" || ! $TCMD "cd $TDIR/getState"; then
echo "---------------------------------------------------------"
echo "ERROR: readsb version too old, use replicate_state_old.sh or update readsb to version >= v3.14.1618"
echo "---------------------------------------------------------"
exit 1
fi
GDIR="$SDIR/getState"
RDIR="$TDIR/replaceState"
TTDIR="$TDIR/replaceTmp"
$TCMD "mkdir -p $TTDIR && mkdir -p $RDIR && chmod a+w $RDIR"
suffix="zstl"
for num in $(seq 0 255); do
blob="$(printf "%02x\n" "$num")"
TRIGGER="$GDIR/writeState"
BLOB="blob_${blob}.${suffix}"
$SCMD "echo $blob > $TRIGGER; while [[ -f $TRIGGER ]]; do sleep 0.01; done;"
$SCMD "tar -C $GDIR -c -f - $BLOB && rm -f $GDIR/$BLOB" | $TCMD "tar -C $TTDIR --overwrite -x -f - && chmod a+w $TTDIR/$BLOB && mv -f $TTDIR/$BLOB $RDIR/$BLOB;"
echo "$(date -u --rfc-3339=s) transferring $BLOB"
done
wait # wait for last transfer
echo "$(date -u --rfc-3339=s) transfer done, waiting for completion of state load on the target side"
$TCMD "while ls $RDIR | grep -qs -v -e tmp; do sleep 1; done"
$TCMD "if ls $RDIR | grep -qs ${suffix}; then echo transfer or state loading incomplete, check target readsb log; else echo $(date -u --rfc-3339=s) state loading completed on target; fi"
$TCMD "rm -rf $RDIR $TTDIR"
rm -rf "$SSHDIR"