我的博客已迁移到xdoujiang.com请去那边和我交流 一、脚本说明 1、需要备份2个库(mysqltest1 mysqltest) 2、开启4个进程 3、可以选择进行备份不压缩和备份压缩 4、可以对不需要的数据库剔除 5、backup用户在数据库里的权限 grant select,reload,super,lock tables,create view,show view on *.* to 'backup'@'localhost' identified by 'backup'; flush privileges; 6、计划任务 00 00 * * * cd /opt/dbbak && bash mysqlback.sh mysqlbackupconf >> /opt/dbbak/dbbackup.log 2>&1 7、会删除三天前的binlog 8、mysql版本5.5的 9、debian7.4 (3.2.54-2 x86_64) 二、具体脚本如下 cat mysqlback.sh #!/bin/bash #-------------------------------------------------- #Author:jimmygong #Email:jimmygong@taomee.com #FileName:mysqlback.sh #Function: #Version:1.0 #Created:2015-06-29 #-------------------------------------------------- if [[ $# -ne 1 ]] then echo -e "Usage:$0 mysqlbackupconf" exit 1 else config=$1 if [[ ! -f $config ]] then echo -e "Usage:$0 mysqlbackupconf" exit 1 fi fi source $config dbname="" currdate=$(date +%Y%m%d) haveinnodb=0 localip=$(ifconfig eth0|awk '/inet addr:/'|awk -F: '{print $2}'|awk -F" " '{print $1}') function purgebinlog () { purdate=`date "+%F %T" --date='3 day ago'` pur="purge master logs before '$purdate'" echo "$pur"|mysql -u"$dbuser" -p"$dbpass" } function redirectlog () { logfile=$logdir/${currdate}_${localip}_log mkdir -p $logdir exec 1>$logfile exec 2>$logfile } function checkdestdir () { destdir=$destdir/$currdate if [[ ! -d $destdir ]] then mkdir -p $destdir fi } function runcommand () { comm=$1 mysql -u"$dbuser" -p"$dbpass" -sNe "$comm" } function generateschema () { i=0 result=`runcommand 'show databases'` for db in $result do rv=`echo $exclude|grep -w -i $db` if [[ -n "$rv" ]] then continue; fi dbname[i]=$db let i++ done } function guessengine () { innodb=`runcommand 'show engines'|grep -i innodb|grep -i yes` if [[ ! -z "$innodb" ]] then haveinnodb=1 fi } function dobackupsql () { db=$1 destname=$destdir/$db.sql if [[ $haveinnodb -eq 1 ]] then dumpcomm="mysqldump --opt -u$dbuser -p$dbpass --single-transaction --databases" else dumpcomm="mysqldump --opt -u$dbuser -p$dbpass --databases" fi $dumpcomm $db > $destname } function dobackupgzip () { db=$1 destname=$destdir/$db.sql.gz if [[ $haveinnodb -eq 1 ]] then dumpcomm="mysqldump --opt -u$dbuser -p$dbpass --single-transaction --databases" else dumpcomm="mysqldump --opt -u$dbuser -p$dbpass --databases" fi $dumpcomm $db|gzip > $destname } function backup () { actioncommand=$1 if [[ $commpress -eq 1 ]] then actioncommand="dobackupgzip" else actioncommand="dobackupsql" fi echo -en "`date`\tBACKUP\t$db\n" $actioncommand $db } function backupalldb () { count=0 for db in ${dbname[@]} do backup $db & let count+=1 [[ $((count%$processnum)) -eq 0 ]] && wait done wait echo "all backup done" } purgebinlog redirectlog checkdestdir generateschema guessengine runcommand "flush logs" backupalldb exit 0 三、具体配置信息 cat mysqlbackupconf dbuser="backup" dbpass="backup" exclude='mysql information_schema performance_schema' destdir=/opt/dbbak logdir=/opt/dbbak/log commpress=1 processnum=4 四、备份后结果 1、root@10.131.172.202:~# ll /opt/dbbak/20150629/ -rw-r--r-- 1 root root 4443602410 Jun 29 19:18 mysqltest1.sql.gz -rw-r--r-- 1 root root 4443601959 Jun 29 19:19 mysqltest.sql.gz 2、root@10.131.172.202:~# ll /opt/dbbak/log/ -rw-r--r-- 1 root root 109 Jun 29 19:19 20150629_10.131.172.202_log
mysql备份单实例(一)shell
原创文章标签 mysql 备份 实例 shell 文章分类 MySQL 数据库
©著作权归作者所有:来自51CTO博客作者xdoujiang的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
一键安装Mysql单实例
编译安装.
mysql -
利用Xtrabackup进行单库备份、单表恢复实例
利用Xtrabackup进行单库备份、单表恢复实例
Xtrabackup 数据备份 数据恢复 表空间传输 -
MySQL备份脚本shell
#!/bin/bashdate=`date +%Y%m%d`backDb=$date.sqlbackFile
sql shell 备份 linux php