#!/bin/bash
   
LOCKFILE="/run/lock/rsync_airtune_backup"
   
# when using docker, specify the path: <directory where docker is deployed>/volumes/eltex-airtune/etc/eltex-airtune/
AIRTUNE_CONF="/etc/eltex-airtune/"
AIRTUNE_BACKUP="/tmp/eltex-airtune/backup/"
AIRTUNE_REPORTS="/var/lib/eltex-airtune/reports/"
 
# IP address backup server
HOST=<ip_адрес_другого_сервера>
# Check if we're root
if [ `whoami` != "root" ]
    then
    echo "This script should be run by root."
    exit 1
fi
 
# Check and create lock file
if ! lockfile-create --use-pid -r 0 $LOCKFILE &> /dev/null ; then
    echo "Backup is already running"
    exit 0
fi
 
FOLDER="/tmp/rsync"
if [ ! -d "$FOLDER" ]
then
    mkdir $FOLDER
fi
 
# Check - if we're master - try to perform backup to slave
SRVMODE=`cat /tmp/keep.mode`
if [ "$SRVMODE" == "MASTER" ]
    then
    rsync -urlogtp --delete-after --password-file=/etc/rsync_client.secrets $AIRTUNE_CONF backup@$HOST::airtune-conf > /tmp/rsync/rsync_airtune_conf.log 2>&1
    echo $? >> /tmp/rsync/rsync_airtune_conf_result.log
    rsync -urlogtp --delete-after --password-file=/etc/rsync_client.secrets $AIRTUNE_BACKUP backup@$HOST::airtune-backup > /tmp/rsync/rsync_airtune_backup.log 2>&1
    echo $? >> /tmp/rsync/rsync_airtune_backup_result.log
    rsync -urlogtp --delete-after --password-file=/etc/rsync_client.secrets $AIRTUNE_REPORTS backup@$HOST::airtune-reports > /tmp/rsync/rsync_airtune_reports.log 2>&1
    echo $? >> /tmp/rsync/rsync_airtune_reports_result.log
else
 
    echo "Not master. No action will be performed."
fi
 
lockfile-remove $LOCKFILE
