#!/bin/bash
heure=$(date +%H)
debutcrenau="20"
fincrenau="06"

#echo $heure
if [[ $1 == "now" ]]
then
        echo "NOW active"
else
	if [ $heure -ge $fincrenau ]
	then
		if [ $heure -le $debutcrenau ]
		then
			echo "Ce n est pas le moment. Ouvert de $debutcrenau h a $fincrenau h"
			exit
		fi
	fi
fi


if [ ! -f "/etc/sauvegarde-gimini.conf" ]; then
        echo "File /etc/sauvegarde-gimini.conf does not exist. -> FTP ONLY"
        exit
fi


nums=$(su - system /home/system/disks/s/theox/command/txserial | sed 's/[0-9]*-//g')
nums=$(sed 's/^0*//g' <<< ${nums})
nums=$(printf "%05d" $nums)

echo " "
if [[ -z "$nums" ]]
then
        echo "Error: txserial vide"
        exit
fi

#BACKUP_ROOT=$(awk '/^snapshot_root/ {print $2}' /etc/rsnapshot.conf)
BACKUP_ROOT="/home"
echo "$BACKUP_ROOT"

for i in `cat /home/system/disks/s/theos/config/account.ini | grep -v "\[__uid__\]" | grep -v "\[system\]" |  grep "\[" | sed -e "s/\[//g; s/\]//g;"`
do
	if [[ $nums == "55005" ]] #serveur de test
        then
                if [[ $i == "casa" ]]
                then
                        rsync -e "ssh -p 50218 -i /home/system/.ssh/$(hostname)_rsa -o 'StrictHostKeyChecking=accept-new'" -av --chmod=F644 --perms --chown=d$nums:theox --owner --group --progress --exclude="*.exp" --exclude="*/docassoc.lib/" $BACKUP_ROOT/$i d$nums@wan-kosc.timci.com:/TXBK/$nums
                fi
        else
		#rsync -e "ssh -p 50218 -i /home/system/.ssh/$(hostname)_rsa -o 'StrictHostKeyChecking=accept-new'" -av --chmod=F644 --perms --chown=d$nums:theox --owner --group --progress --exclude="*.exp" --exclude="*/docassoc.lib/" --exclude="*/archive.etats/" $BACKUP_ROOT/$i d$nums@wan-kosc.timci.com:/TXBK/$nums
		rsync -e "ssh -p 50218 -i /home/system/.ssh/$(hostname)_rsa -o 'StrictHostKeyChecking=accept-new'" -av --chmod=F644 --perms --chown=d$nums:theox --owner --group --progress --exclude="*.exp" --exclude="*/docassoc.lib/" $BACKUP_ROOT/$i d$nums@wan-kosc.timci.com:/TXBK/$nums
	  fi
done

