# ERP backup script

#!/bin/bash
# This is an ERP backup script
# By LongMinyong
# Last update on 2018-08-07
# Verion 1.0

#######color code########
RED="31m"      # Error message
GREEN="32m"    # Success message
YELLOW="33m"   # Warning message
BLUE="36m"     # Info message

colorEcho(){
    COLOR=$1
    echo -e "\033[${COLOR}${@:2}\033[0m"
}

date=$(date +%Y%m%d)

echo "Press Ctrl+C to stop"
read -p "Do you want to backup and kill the erp process?(y or n): " ans
ls bkup/$date &> /dev/null


if [ ! $? = 0 ] && [ $ans = "y" ]

then

	colorEcho ${BLUE} "kill erp ..."
	erps=$(pidof erp)
	kill $erps
	mkdir -p bkup/$date
	colorEcho ${BLUE} "Backuping,please wait ..."
	cp -r gopath/src/erp bkup/$date/ &> /dev/null
	cp -r gopath/src/wb bkup/$date/ &> /dev/null
	cp -r gopath/src/erpdata/db bkup/$date/ &> /dev/null
	colorEcho ${BLUE} "rsyncing..."
	rsync -az gopath/src/erpdata/static filebkup/
	ls -al bkup/$date/
	ls -al filebkup/
	colorEcho ${GREEN} "Backup is success!"

elif [ ! $? = 0 ] && [ $ans = "n" ]

then

	colorEcho ${YELLOW} "Backup cancelled!"

else

	colorEcho ${RED} "Backup is error!"
	colorEcho ${RED} "$date may already exist"
	colorEcho ${RED} "Your answer is $ans, please enter (y or n)"

fi