#!/bin/bash
config="/etc/astercc.conf"  # if this run for crontab please assign absolute path for config
if [ "X$1" != "X" ];then
	config="$1"
fi

mainpath="/tmp/backup"

astercc10='yes'

if [ ! -e ${config} ];then
	echo "can not find config file "${config}
	astercc10='no';
	versionrev=0
fi

if [ "${astercc10}" == "yes" ];then
	### Get Astercc version rev ###
	dbname=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep dbname[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`
	dbhost=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep dbhost[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`
	dbuser=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep username[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`
	dbpasswd=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep password[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`
	prefix=`sed -n '/\[database]/,/\[.*\]/p' ${config} |grep prefix[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`

	if [ "${dbpasswd}X" != "X" ];then
		version=`mysql -h${dbhost} -u${dbuser} -p${dbpasswd} -e "use ${dbname}; select rev from ${prefix}upgradelogs where versiontype='system'  order by rev desc limit 1;"`
	else
		version=`mysql -h${dbhost} -u${dbuser}  -e "use ${dbname}; select rev from ${prefix}upgradelogs where versiontype='system'  order by rev desc limit 1;"`	
	fi
		
	if [ "X${version}" == "X" ];then  
		versionrev=0	
	else	
		versionrev=`echo ${version} | cut -f 2 -d " "`
	fi
fi

### time ###
curdate=`date +"%Y-%m-%d-%H-%M"`

curmonth=`date +"%m"`
if [ `expr length $curmonth` == 2 ] && [[ "$curmonth" =~ ^0 ]];then	
	curmonth=`echo "$curmonth" | sed -e 's/0\([0-9]*\)/\1/g'`
fi

curweek=`date +"%w"`

curday=`date +"%d"`
if [ `expr length $curday` == 2 ] && [[ "$curday" =~ ^0 ]];then	
	curday=`echo "$curday" | sed -e 's/0\([0-9]*\)/\1/g'`
fi

curhour=`date +"%H"`
if [ `expr length $curhour` == 2 ] && [[ "$curhour" =~ ^0 ]];then	
	curhour=`echo "$curhour" | sed -e 's/0\([0-9]*\)/\1/g'`
fi

curminute=`date +"%M"`
if [ `expr length $curminute` == 2 ] && [[ "$curminute" =~ ^0 ]];then	
	curminute=`echo "$curminute" | sed -e 's/0\([0-9]*\)/\1/g'`
fi

### check  backupplan.conf ###
planconf='/tmp/backup/backupplan.conf'
if [ "X$1" != "X" ];then
	planconf="$1"
fi

if [ ! -e ${planconf} ];then
	echo "can not find backupplan.conf file "${planconf}
	exit
fi


### ls backplan ###
cat ${planconf} | while read LINE
do
	if [[ "$LINE" =~ ^[backplan:[1-9]+] ]];then
		plantag=`echo "$LINE" | sed -e 's/\[backplan:\([0-9]*\)\]/\1/g'`
		
		### Get backupplan time ###
		backuptime=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep backuptime[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`

	
		planminute=`echo $backuptime | cut -f 1 -d " "`
		planhour=`echo $backuptime | cut -f 2 -d " "`
		planday=`echo $backuptime | cut -f 3 -d " "`
		planweek=`echo $backuptime | cut -f 4 -d " "`
		planmonth=`echo $backuptime | cut -f 5 -d " "`
		
		#Get backupplan status
		planstatus=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep planstatus[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
		
		
		### Check backup time ###
		#echo "${planminute} - ${curminute} || ${planhour} - ${curhour} || ${planday} - ${curday} || ${planweek} - ${curweek} || ${planmonth} - ${curmonth} || $planstatus"
		if [ "${planminute}" == "a" -o "${planminute}" == "${curminute}" ] && [ "${planhour}" == "a" -o "${planhour}" == "${curhour}" ] && [ "${planday}" == "a" -o "${planday}" == "${curday}" ] && [ "${planweek}" == "a" -o "${planweek}" == "${curweek}" ] && [ "${planmonth}" == "a" -o "${planmonth}" == "${curmonth}" ] && [ "$planstatus" == "enable" ];then
			
			### create save dir ###
			if [ ! -d "/tmp/backup/" ];then
				mkdir -p "/tmp/backup/"
				chmod 777 "/tmp/backup/" -R
			fi			
			
			planname=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep planname[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
			backuppath="/tmp/backup/${planname}.${versionrev}.${curdate}/";
			
			if [ ! -d ${backuppath} ];then
				mkdir -p ${backuppath}
				chmod 777 ${backuppath} -R
			fi
			
			### backup database ###
			dbconf=${backuppath}database.conf
			echo "[database]" >> ${dbconf}
			sed -n '/\[database:'${plantag}']/,/\[.*\]/p' ${planconf} | while read dbline
			do
				if [ "${dbline}" != "[database:"${plantag}"]" ] && [ "${dbline}X" != "X" ] && [ "${dbline}" != "[filelist:"${plantag}"]" ];then		
					curdbname=`echo $dbline | cut -f 1 -d ","`
					curdbhost=`echo $dbline | cut -f 2 -d ","`
					curdbuser=`echo $dbline | cut -f 3 -d ","`
					curdbpw=`echo $dbline | cut -f 4 -d ","`
					sqldst=${backuppath}${curdbname}.sql
					
					if [ "${curdbname}X" != "X" ] && [ "${curdbuser}X" != "X" ] && [ "${curdbpw}X" == "X" ];then
						#echo "$curdbuser $curdbname";
						mysqldump --opt -Q -h$curdbhost -u$curdbuser $curdbname > ${sqldst}
					elif [ "${curdbname}X" != "X" ] && [ "${curdbuser}X" != "X" ] && [ "${curdbpw}X" != "X" ];then
						#echo "-u$curdbuser -p$curdbpw $curdbname";
						mysqldump --opt -Q -h$curdbhost -u$curdbuser -p$curdbpw $curdbname > ${sqldst}
					fi

					echo "${curdbname}dbname=${curdbname}" >> ${dbconf}
					echo "${curdbname}dbhost=${curdbhost}" >> ${dbconf}
					echo "${curdbname}dbuser=${curdbuser}" >> ${dbconf}
					echo "${curdbname}dbpw=${curdbpw}" >> ${dbconf}
					if [ -e ${sqldst} ];then
						cd ${backuppath}
						if [ "${curdbname}" == "${dbname}" ];then
							tar -Pczf  accdb_${curdbname}.tar.gz  ${curdbname}.sql
						else
							tar -Pczf  adb_${curdbname}.tar.gz  ${curdbname}.sql
						fi
						rm -f ${sqldst}
					fi
					
					unset sqldst
				fi
			done
			
			### backup file ###
			sed -n '/\[filelist:'${plantag}']/,/\[.*\]/p' ${planconf} | while read fileline
			do
				let nextplantag=${plantag}+1
				if [ "${fileline}" != "[filelist:"${plantag}"]" ] && [ "${fileline}X" != "X" ] && [ "${fileline}" != "[backplan:"${nextplantag}"]" ];then
					if [ -d ${fileline} -o -e ${fileline} ];then
						tarname=${fileline////\_}
						#tar.gz
						tar -Pczf  ${backuppath}${tarname}.tar.gz  ${fileline}
					fi
				fi
			done
			
			chmod 777 ${backuppath} -R
			
			### ftp backup ###
			ftpstatus=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep ftpstatus[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
			if [ "${ftpstatus}" == "yes" ];then
				ftphost=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep ftphost[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
				ftpuser=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep ftpuser[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
				ftpport=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep ftpport[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
				ftppasswd=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep ftppasswd[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
				ftppath=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep ftppath[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
				
#put file to ftp
if [ "X${ftphost}" != "X" ];then
/usr/bin/ftp -n -i  ${ftphost} ${ftpport} <<CONTENT
user ${ftpuser} ${ftppasswd}
binary
mkdir ${ftppath}
mkdir ${ftppath}/${planname}.${versionrev}.${curdate}
cd ${ftppath}/${planname}.${versionrev}.${curdate}
lcd "${backuppath}"
mput *.tar.gz
dir * "curremotedbfile"
cd ..
lcd ..
dir * "${plantag}-curremotedbfile"
bye
CONTENT
fi
			backuphold=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep backuphold[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
			roldbkdate=`date -d -${backuphold}day "+%Y%m%d"`
			#echo $roldbkdate
			while read RFILE
			do
				curdatetime=`echo $RFILE | cut -f 3 -d .`
				curversion=`echo $RFILE | cut -f 2 -d .`
				curdirdate=`echo $curdatetime |sed -e 's/-[0-9][0-9]-[0-9][0-9]$//'`
				curdirdate=`echo $curdirdate |sed -e 's/-//g'`
				#echo $curdirdate
				if [ $curdirdate != '' ];then
					if [ $curdirdate -le $roldbkdate ];then
#delete remote old file to ftp
echo "delete remote old file:${planname}.${curversion}.${curdatetime}"
if [ "X${ftphost}" != "X" ];then
/usr/bin/ftp -n -i  ${ftphost} ${ftpport} <<CONTENT
user ${ftpuser} ${ftppasswd}
binary
cd ${ftppath}
mdelete ${planname}.${curversion}.${curdatetime}/*
rmdir ${planname}.${curversion}.${curdatetime}
bye
CONTENT
fi
					fi
				fi
			done < "$mainpath/1-curremotedbfile"


			fi
			
			### delete backup ###
			backuphold=`sed -n '/\[backplan:'${plantag}']/,/\[.*\]/p' ${planconf} |grep backuphold[\ ]*= |grep -v "^\ *#" |cut -d= -f2 |tr -d ""`
			if [ "${backuphold}" == "0" ];then
				curcount=`ls /tmp/backup/ |wc -l`
				if [ $curcount -gt 0 ];then
					rm -rf /tmp/backup/${planname}.*
				fi
			else
				#n=`echo ${backuphold} |tr -d [1-9]`
				#if [ "X${n}" == "X" -a "X${backuphold}" != "X" ];then
                if [ "X${backuphold}" != "X" ];then
					oldbkdate=`date -d -${backuphold}day "+%Y-%m-%d"`
					rm -rf /tmp/backup/${planname}.*.${oldbkdate}*
				fi
			fi
			
			#cur backupplan done
		fi
	fi
done
#echo "GAME OVER"
exit
