mysql備份單一實例(一)shell

來源:互聯網
上載者:User

標籤:mysql 備份 執行個體 shell

一、指令碼說明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>&17、會刪除三天前的binlog8、mysql版本5.5的9、debian7.4 (3.2.54-2 x86_64)二、具體指令碼如下cat mysqlback.sh#!/bin/bash#--------------------------------------------------#Author:jimmygong#Email:[email protected]#FileName:mysqlback.sh#Function: #Version:1.0 #Created:2015-06-29#--------------------------------------------------if [[ $# -ne 1 ]]then    echo -e "Usage:$0 mysqlbackupconf"    exit 1else    config=$1    if [[ ! -f $config ]]    then        echo -e "Usage:$0 mysqlbackupconf"        exit 1    fifisource $configdbname=""currdate=$(date +%Y%m%d)haveinnodb=0localip=$(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"}purgebinlogredirectlogcheckdestdirgenerateschemaguessengineruncommand "flush logs"backupalldbexit 0三、具體配置資訊cat mysqlbackupconfdbuser="backup"dbpass="backup"exclude=‘mysql information_schema performance_schema‘destdir=/opt/dbbaklogdir=/opt/dbbak/logcommpress=1processnum=4四、備份後結果1、[email protected]:~# 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.gz2、[email protected]:~# ll /opt/dbbak/log/-rw-r--r-- 1 root root 109 Jun 29 19:19 20150629_10.131.172.202_log

本文出自 “7928217” 部落格,請務必保留此出處http://7938217.blog.51cto.com/7928217/1669168

mysql備份單一實例(一)shell

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.