This script allows to synchronise the databases backed-up on PiDatabase with TRD Machine so as to always have a copy of TRD system on an independent device (TRD Machine).
This script sends warning messages via MQTT and Pushover in case the synchronisation has not ended successfully
#!/bin/bash
#
#synchronisation script between PiDatabase machine containing Databases Backup and TRD Machine to maintain an always up-to data configuration
# (Databases and PHP code)
PiDatabase='192.168.86.160'
BROKER='bubuoat.mine.nu'
TOPIC='rsync'
SOURCE_DATABASE_BACKUP='/home/database_backup'
DATABASE_BACKUP_TRD_machine='/home/database_backup/'
TOKEN='aeuca87bjjuqknkwswm9bpvy1ygngo'
USER_KEY='u6ysovfgq1nhysszxzh91qnwadch2y'
MAX_LOG_SIZE=100000
LOG_FILE='/home/pi/php_sync.log'
while true
do
# check whether the log file size is exceeding the limit defined by MAX_LOG_SIZE above
if [[ -s $LOG_FILE ]]; then
log_size=$(wc -c $LOG_FILE | awk '{print $1}')
if [ $log_size -gt $MAX_LOG_SIZE ]
then
# remove the file
rm $LOG_FILE
fi
else
echo "No such file!!"
fi
# Synchronise the databases backup with TRD Machine
SHARE='DATABASE_BACKUP'
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
echo "" & date >> $LOG_FILE
echo "$SHARE to TRD_machine" >> $LOG_FILE
ping -c 1 $TRD_machine > /dev/null # no output on screen
if [ $? -eq 0 ] # test the return value of the last executed command
then
echo "TRD_machine is reachable" >> $LOG_FILE
mosquitto_pub -p 1883 -h $BROKER -t $TOPIC -m "TRD_machine is reachable"
sudo rsync -avzh -i --stats $SOURCE_DATABASE_BACKUP $DATABASE_BACKUP_TRD_machine > /dev/null
case $? in
0)
echo "Success" >> $LOG_FILE
;;
1|2|3|4|5|6|10|11|12|13|14|20|21|22|23|24|25|30|35)
echo "*** Synchronisation encountered problems. Share: $SHARE ***" >> $LOG_FILE
mosquitto_pub -p 1883 -h $BROKER -t $TOPIC -m "Error on $SHARE share!"
curl -s \
--form-string "token=aeuca87bjjuqknkwswm9bpvy1ygngo" \
--form-string "user=u6ysovfgq1nhysszxzh91qnwadch2y" \
--form-string "title=Sync issue on PiDatabase"\
--form-string "message=Problem with Share $SHARE" \
https://api.pushover.net/1/messages.json
;;
*)
echo "Unknown return code $? from rsync! You should have a look at it." >> $LOG_FILE
mosquitto_pub -p 1883 -h $BROKER -t $TOPIC -m "Unexpected rsync error code!!"
esac
else
echo "PiPlex2 not reachable. Sync has been skipped" >> $LOG_FILE
mosquitto_pub -p 1883 -h $BROKER -t $TOPIC -m "PiPlex2 not reachable"
fi
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
echo "" & date >> $LOG_FILE
echo "Synchronisation completed " >> $LOG_FILE
echo "################################################" >> $LOG_FILE
echo "" >> $LOG_FILE
sleep 300
done
exit