#!/bin/bash ########################################### # ## # CRON SCRIPT FOR SYNC BETWEEN SERVERS ## # ## ########################################### function st_self_ts() { if [ -s "$CAP_STATUS_FILE" ] ; then LINE="$(sed -ne "${1}p" "$CAP_STATUS_FILE")" fi echo $LINE } function st_self_fix() { [ "$1" ] || exit 1 LINE="$1" shift [ "$1" ] || exit 1 VALUE="$1" sed -e "$LINE s/.*/$VALUE/" $CAP_STATUS_FILE } CAPNAME="$1" VARS_LLXCFG="CAP_TIME_TOLERANCE CAP_LIMIT_CHALLENGE" eval $(llxcfg-showvars $VARS_LLXCFG) HOSTNAME=$(hostname -f) CAPBASEDIR="/var/lib/lliurex-cap/${CAPNAME}" CAP_STATUS_FILE="$CAPBASEDIR/status/${HOSTNAME}.cap" STATUS="$(st_self_ts 5)" NOW="$(date +%Y%m%d%H%M%S)" NEW_TS="$(st_self_ts 1)" DIFF="$(( ( NOW - NEW_TS ) / 10 ))" if [ "$STATUS" -eq 0 ] ; then # # SERVER IS WAITING. # if [ "$DIFF" -le "$CAP_TIME_TOLERANCE" ]; then exit 0 fi # # Search Leader to sync # # Join on one line all content of all server file status. By this line get update status for server. Next print this line and name file. Order by line to get server more updated. First server on this list is leader. Get status file leader, that the name is FQDN of LEADER. # IPLEADER="{find ${CAPBASEDIR}/status/ -type f -name "*.cap" | while read f; do cat "$f" | tr -d "\n"; echo ":$f" ; done ; }|sort -r -t : -k 1 | head -1 | cut -d : -f 2" if [ -n "$IPLEADER" -o "$HOSTNAME" != "$IPLEADER" ] ; then lliurex-cap sync-by-rsync $CAPNAME $IPLEADER fi # # If I'm the leader or aren't, i do nothing and change my status to standar # else i sync with leader and chage my status to standar. # By to two way server is sync ( I'm leader or i'm updated ) # t_self_fix "5" "1" else if [ "$DIFF" -le "$CAP_LIMIT_CHALLENGE" ]; then exit 0 fi # # Should be launch challenge # RANK="$(st_self_ts 3)" LUCK="$(st_self_ts 4)" TTS="$(( 9 - RANK + LUCK ))" sleep $TTS NOW="$(date +%Y%m%d%H%M%S)" NEW_TS="$(st_self_ts 1)" DIFF="$(( ( NOW - NEW_TS ) / 10 ))" if [ "$DIFF" -le "$CAP_LIMIT_CHALLENGE" ]; then exit 0 fi t_self_fix "1" "${NOW}0" t_self_fix "2" "${NEW_TS}" fi csync2 -C "${CAPNAME}status" -xr || true