#!/bin/sh
# This script adds new rsync job to rsync.conf (taking SRC / DST server and MOUNT POINTS etc. as script arguments
# and creates backup of old rsync.conf
# Once new rsync.conf is ready it does add a new cron job with an if condition to prevent multiple rsync spawns
# Use carefully any problems created by using this script are YOUR RESPONSIBILITY

# Author: Georgi Georgiev georgi.georgiev05@sap.com

get_src=$1
get_src_nfs=$2
put_src=$3
put_src_nfs=$4
put_server=$5;
name=$6;
desc=$7;
home='/home/c5280383';
exclude='.snapshot;.ssh';
conf='/opt/imal/etc/rsync.conf';
t_conf="$home/rsync.conf";

clean_conf='yes';

# Cron frequency minutes
MINUTES='8,28,48';
# cron run (def. every hour)
HOUR='*';

rsync_pl_path='/opt/imal/bin/rsync.pl';
tmp_f='/tmp/rsync.lock';
opt_rsync_l='/opt/imal/etc/rsync.lock';
# fill in with value if you will not pass the variable
setx='';
log_f='/root/rsync_timeout.log';
DATE=$(date +%d-%m-%y-%H);

gen_rsync_conf () {

# check if args passed to script
if [ "$get_src" ] && [ "$get_src_nfs" ] && [ "$put_src" ] && [ "$put_src_nfs" ] && \
[ "$put_server" ] && [ "$name" ] && [ "$desc" ]; then
# check if mounted get_src and get_src_nfs dir exists
nslookup $put_server >/dev/null 2>&1
##echo $?
##echo "mount |grep -i $(echo $get_src| cut -d ':' -f2) |wc -l"
if [[ "$?" == "0" ]] && [[ "$(mount |grep -i $(echo $get_src| cut -d ':' -f2) |wc -l)" == '1' ]];  then

echo '[ OK ] mounts are fine ... proceeding';
else
echo "[ FAILED ] Some of the $get_src / $get_src_nfs / $put_src / $put_src_nfs / $put_server / $name / $home are not mounted or present ,. Exiting ..."
##exit 1;
echo 'Once the new rsync.conf records are aded a cron job is added with time occurance predefined in script header';

fi

fqdn=$(nslookup $(hostname)|grep Name | awk ' { print $2 }');
h0=$(echo $get_src | cut -d ":" -f 1);
h1=$(echo $put_src | cut -d ":" -f 1);
##echo a
##echo $h
##echo "(nslookup $h|grep -i Name";
##fqdn1=$(nslookup $h0|grep -i Name|awk '{ print $2 }');
##fqdn2=$(nslookup $h1|grep -i Name|awk '{ print $2 }');
##fqdn1=$(nslookup $(echo $get_src| cut -d ':' -f1| awk '{ print $1 }'));
##fqdn2=$(host $(echo $put_src| cut -d ':' -f1| awk '{ print $1 }'));
##echo $fqdn1;
##exit 1;
dest_src=$(nslookup $put_src|grep Name | awk ' { print $2 }');
echo "[ OK ] Creating $conf backup in ~/$conf-`date +%d-%m-%y`.bak";
echo "cp -rpf $conf $home/$conf-`date +%d-%m-%y`.bak"
cp -rpf $conf $home/$conf-`date +%d-%m-%y`.bak
echo "[ OK ] Generating $t_conf";
if [ "$clean_conf" == 'yes' ]; then
cat /dev/null  > $t_conf
fi
cat $conf >$t_conf
echo "#Added to rsync.conf - $(date +%d-%m-%y)" >>$t_conf
echo "[$name] $description" >> $t_conf
echo "bwlimit|128000" >> $t_conf
echo "get_server|$fqdn" >> $t_conf
echo "get_nfs|$get_src" >> $t_conf
echo "get_dir|$get_src_nfs" >> $t_conf
echo "put_server|$put_server" >> $t_conf
echo "put_nfs|$put_src" >> $t_conf
echo "put_dir|$put_src_nfs" >> $t_conf
echo "exclude|$exclude" >> $t_conf
##echo "exclude|.snapshot" >> $t_conf
echo "[ OK ] Completed";
echo "Please always review $conf to make sure no problems with it";
echo "!!! To override with new config run (ON YOUR OWN RESPONSIBILITY) !!!:";
echo "cp -rpf $t_conf $t_conf";
CONF_ADDED='1';
else

echo -e "Syntax: SRC_DIR SRC_DIR_NFS_VOL DST_SRC DST_SRC_NFS PUT_SERVER NAME CRON_TITLE_NAME DESCRIPTION\n"
echo -e 'i.e.:  src_server:/vol_name1 /repo/src/dir/to/sync dst_server:/volume_name2 /repo/dst/dir/to/sync hec03v027987.stl.hec.sap.biz rsync.pl_CRON_argument_name Desription'
echo 'Sample use';
echo './gen_rsync_conf.sh hec07fi0319:/vol1_s4hana_repomaster /repo/repo/s4hana hec07fi0319:/vol1_s4hana_repomaster /repo/repo/s4hana/data hec03v027987.stl.hec.sap.biz set_SYDDBA description';
echo -e "\nTo add excludes edit the script \$exclude option and add them";


fi


}

add_to_cron () {
if [ -z $setx ]; then
setx="$name";
fi
echo $setx;

if [ -z $setx ]; then
echo 'Add $setx in script or pass on the job name as argument:';
echo ' ./add_rsync_job_cron.sh Job_Name_Value';
echo 'Note the Job_Name_Value should be present in /opt/imal/etc/rsync.conf';
fi

if [[ $CONF_ADDED -eq '1' ]]; then
(crontab -l 2>/dev/null; echo "#BELOW added on: $DATE";) | crontab -
echo "Adding cron:";

echo "$MINUTES $HOUR * * * if [ ! -f $tmp_f.$setx ]; then touch $tmp_f.$setx; echo \"Started: \$(date)\" >> $log_f; (time timeout 86400 /usr/bin/perl $rsync_pl_path $setx) 2>&1 | tee -a $log_f; echo \"Clearing $opt_rsync_l.$setx\" >> $log_f;  rm -f $opt_rsync_l.$setx; echo \"Finished: \$(date)\" >> $log_f >/dev/null 2>&1; rm -f $tmp_f.$setx; fi";
(crontab -l 2>/dev/null; echo "$MINUTES $HOUR * * * if [ ! -f $tmp_f.$setx ]; then touch $tmp_f.$setx; echo \"Started: \$(date)\" >> $log_f; (time timeout 86400 /usr/bin/perl $rsync_pl_path $setx) 2>&1 | tee -a $log_f; echo \"Clearing $opt_rsync_l.$setx\" >> $log_f;  rm -f $opt_rsync_l.$setx; echo \"Finished: \$(date)\" >> $log_f >/dev/null 2>&1; rm -f $tmp_f.$setx; fi";) | crontab -
fi

echo "Run 1st time and Test Newly added $setx perl job with:";

echo "/usr/bin/perl $rsync_pl_path $setx --progress --verbose";
}

main () {
gen_rsync_conf;
add_to_cron;
}

main;
