dump备份mysql表
Auth: Jin
Date: 20140403
Content:
1 #!/bin/bash - 2 ### auth: Jin 3 ### date: 20140403 4 ### Desc: 备份输入库里的所有表,清理指定天前的备份文件 5 ### Usage: ./dump_mysqltable.sh 6 7 BACKDIR=/data/BackupPC/database 8 BACKUPLOG=${BACKDIR}/dump_mysqltable.log 9 TODAY=`date +%F` 10 DELDAY=`date -d "7 day ago" +%F` 11 DUMPBIN=/usr/local/mysql/bin/mysqldump 12 MYSQLBIN=/usr/local/mysql/bin/mysql 13 IP=192.168.201.3 14 PORT=3311 15 USER=root 16 PASSWD=password 17 18 function log () 19 { 20 echo " " >> $BACKUPLOG 21 echo "###########################" >> $BACKUPLOG 22 echo "`date +%F/%T` $1" >> $BACKUPLOG 23 echo "###########################" >> $BACKUPLOG 24 } 25 26 if [ $# -ne 1 ];then 27 printf 'Please give %s\n' databasename 28 exit 1; 29 else 30 DB=$1 31 DBDIR=${BACKDIR}/${DB} 32 BAKDIR=${DBDIR}/${TODAY} 33 DELDIR=${DBDIR}/${DELDAY} 34 mkdir -p ${BAKDIR} 35 fi 36 37 log START 38 TABS=`${MYSQLBIN} -h${IP} -P${PORT} -u${USER} -p${PASSWD} ${DB} -e"show tables\G"|grep -v row|awk '{print $2}'` 39 for TAB in ${TABS};do 40 ${DUMPBIN} -h${IP} -P${PORT} -u${USER} -p${PASSWD} ${DB} $TAB --opt |gzip -9 > ${BAKDIR}/db-${TODAY}.community.${TAB}.sql.gz 41 if [ $? -eq 0 ];then 42 echo "$TAB backup OK" >> $BACKUPLOG 43 else 44 echo "$TAB backup Fail" >> $BACKUPLOG 45 fi 46 done 47 if [ -d ${DELDIR} ];then 48 rm -rf ${DELDIR} 49 if [ -d ${DELDIR} ]; then 50 echo "${DELDIR} delete Fail" >> $BACKUPLOG 51 else 52 echo "${DELDIR} delete OK" >> $BACKUPLOG 53 fi 54 else 55 echo "${DELDIR} is not exist" >> $BACKUPLOG 56 fi 57 log END