#!/bin/bash
Date="ph`date +%Y%m%d%H%M`"
backupdir="/usr/backup/" #backup mysql data direction
mysqldir="/var/lib/mysql" #mysql data direction
backupDB=`find /var/lib/mysql/ -type d | cut -d '/' -f5` #DB name which you want to backup
Num=8 #keep backup data amount
#BinLog=`ls $mysqldir | grep bin| grep -v 'index'|tail -2`
BinLog=`find /var/lib/mysql/ -name '*bin.0*' | cut -d '/' -f5 | grep -v 'relay' | sort | tail -n2`
################# keep the data amount ####################################
if [ `ls $backupdir |sort|wc -l` -gt $Num ];then
while [ `ls $backupdir |sort|wc -l` -gt $Num ]
do
echo "deling"
Dir=`ls $backupdir|sort -r| tail -1`
rm -rf $backupdir$Dir
done
fi
################# backup the DB ######################################
echo "backup db data ................."
mkdir -p $backupdir$Date
cd $mysqldir
cp -rf $BinLog $backupDB $backupdir$Date
################ tar the DB backup files ######################################
cd $backupdir
tar zcf $Date.tar.gz $backupdir$Date