ITPub博客

首页 > Linux操作系统 > Linux操作系统 > DB中备份每一张table script

DB中备份每一张table script

原创 Linux操作系统 作者:markzy5201190 时间:2012-06-11 11:39:34 0 删除 编辑
#!/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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2012-04-05

  • 博文量
    82
  • 访问量
    454243