#!/bin/bash
#auther:don
# version: 1.0.0
pro_dir="/home/www"
pro_name_nangua="nangua"
pro_name_qingyun="qingyun"
pro_name_nangua_new="nangua_new"
pro_name_nangua_admin="nangua_admin"
bak_dir="/home/www/backup"
DATE=$(date +'%Y%m%d%H%M%S')
bak_dir_ip="35.203.135.112"
LogFile=$bak_dir/${DATE}.log
Rsync_bin="/usr/bin/rsync"
Rsync_password="/etc/rsync.password"
Rsync_user="rsync_backup"
function font
{
while (($#!=0))
do
case $1 in
-b ) echo -ne " ";;
-t ) echo -ne " ";;
-n ) echo -ne "\n";;
-black ) echo -ne "\033[30m";;
-red ) echo -ne "\033[31m";;
-green ) echo -ne "\033[32m";;
-yellow ) echo -ne "\033[33m";;
-blue ) echo -ne "\033[34m";;
-purple ) echo -ne "\033[35m";;
-cyan ) echo -ne "\033[36m";;
-gray ) echo -ne "\033[37m";;
-reset ) echo -ne "\033[0m";;
-h|-help|--help ) echo "Usage: font -color1 message1 -color2 message2 ...";
echo "eg: font -red [ -blue message1 message2 -red ]";;
* ) echo -ne "$1";;
esac
shift
done
}
function bak_code_nangua() {
test ! -d ${bak_dir} && mkdir -p ${bak_dir}
cd ${pro_dir}
tar -czf ${bak_dir}/${pro_name_nangua}_${DATE}.tar.gz ${pro_name_nangua} --exclude=${pro_name_nangua}/runtime/*
echo "${bak_dir}/${pro_name_nangua}_${DATE}.tar.gz"
cd ${bak_dir} && find ${bak_dir} -mtime +5 |grep ".gz"|xargs rm -f
}
function bak_code_qingyun(){
test ! -d ${bak_dir} && mkdir -p ${bak_dir}
cd ${pro_dir}
tar -czf ${bak_dir}/${pro_name_qingyun}_${DATE}.tar.gz ${pro_name_qingyun} --exclude=${pro_name_qingyun}/runtime/*
echo "${bak_dir}/${pro_name_qingyun}_${DATE}.tar.gz"
cd ${bak_dir} && find ${bak_dir} -mtime +5 |grep ".gz"|xargs rm -f
}
function bak_code_nangua_new(){
test ! -d ${bak_dir} && mkdir -p ${bak_dir}
cd ${pro_dir}
tar -czf ${bak_dir}/${pro_name_nangua_new}_${DATE}.tar.gz ${pro_name_nangua_new} --exclude=${pro_name_nangua_new}/runtime/*
echo "${bak_dir}/${pro_name_nangua_new}_${DATE}.tar.gz"
cd ${bak_dir} && find ${bak_dir} -mtime +5 |grep ".gz"|xargs rm -f
}
function bak_code_nangua_admin(){
test ! -d ${bak_dir} && mkdir -p ${bak_dir}
cd ${pro_dir}
tar -czf ${bak_dir}/${pro_name_nangua_admin}_${DATE}.tar.gz ${pro_name_nangua_admin} --exclude=${pro_name_nangua_admin}/runtime/*
echo "${bak_dir}/${pro_name_nangua_new}_${DATE}.tar.gz"
cd ${bak_dir} && find ${bak_dir} -mtime +5 |grep ".gz"|xargs rm -f
}
function check_ip() {
if [[ $bak_dir_ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
FIELD1=$(echo $bak_dir_ip |cut -d. -f1)
FIELD2=$(echo $bak_dir_ip |cut -d. -f2)
FIELD3=$(echo $bak_dir_ip |cut -d. -f3)
FIELD4=$(echo $bak_dir_ip |cut -d. -f4)
elif [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then
echo "IP $IP available."
else
echo "IP $IP not available!"
fi
}
function bak_rsync(){
find ${bak_dir}/* |xargs -n1 | xargs md5sum >> file.md5.local
echo "start rsync,$(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
${Rsync_bin} -avlzP --delete ${bak_dir} ${Rsync_user}@35.203.135.112::qybackup --password-file=${Rsync_password} >> $LogFile
if [ $? -eq 0 ];then
font -purple "Rsync_Status="SUCCESS"" -reset -n >> $LogFile
echo "rsync end,$(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
else
font -purple "Rsync_Status="FAILED"" -reset -n >> $LogFile
echo "rsync end,$(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
fi
}
function main() {
while true
do
check_ip
bak_code_nangua
bak_code_qingyun
bak_code_nangua_new
bak_code_nangua_admin
bak_rsync
break
done
}
main