DB中備份每一張table script

markzy5201190發表於2012-06-11
#!/bin/bash
##Descript. backup per table in the testDB
##time:2012-06-05
##Name:testDBfullPerTable.sh

#define DB username and password
myDB=testDB
#define testDB per table backupdir
tabbakdir=/data/testDBbackup/testDBfullPertable
#define scriptdir
scriptdir=`pwd`
#define email subject files
eMailfile=${scriptdir}/email.txt
#Empty email.txt
echo > ${eMailfile}
#define email address
#define exit if-statement condition
exittab="user_log_2012_01_01"

#Backup starting
echo "****************backup per tables of testDB*******************">>${eMailfile}
echo $(date +"%y-%m-%d %H:%M:%S")>>${eMailfile}
tables=`mysql -h a.b.c.d -P port -u xyz -p'abcdek' -Bse "use testDB;show tables;" | grep -I user_log_`
for table_name in ${tables}
do
#define backupfile format
tabdumpfile=${table_name}.$(date +%y%m%d).tar.gz
if [ ${exittab} == ${table_name} ];then
echo "table is backup before [${table_name}] times" >> ${eMailfile}
cat ${eMailfile} | mail -s "tables were backuped before [${table_name}]" ${email}
exit 1
else
if [ -f ${tabdumpfile} ] 
then
echo "[${tabdumpfile}] The backup file is exists,Can't backup again!">>${eMailfile}
cat ${eMailfile} | mail -s "backup repeat pls check it out"  ${email}
else
/usr/bin/mysqldump -h a.b.c.d -P port -u xyz -p'abcdek'  ${myDB} ${table_name} > ${tabbakdir}/${table_name}.sql
#tar backup file
        if [[ $? == 0 ]];then
cd ${tabbakdir}
tar -zcf ${table_name}.$(date +%y%m%d).tar.gz ${table_name}.sql
echo "BackupFileName:${tabdumpfile}">>${eMailfile}
echo "testDB per table backup success!">>${eMailfile}
cat ${eMailfile} | mail -s "${table_name} backuped!" ${email}
rm -rf ${table_name}.sql
      fi
fi
fi
done
echo "**************Backup per tables of testDB end!***************">>${eMailfile}
cat ${eMailfile} | mail -s "testDB per tables backup" ${email}


備註: var="${var}
xxxxxx"

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26855487/viewspace-732438/,如需轉載,請註明出處,否則將追究法律責任。

相關文章