完美的mysql備份指令碼

來源:互聯網
上載者:User

標籤:exec   http   html   方式   .sql   sql   oca   password   blog   

轉自:https://www.cnblogs.com/leffss/p/7832047.html

#!/bin/bash#全備方式,一般在從機上執行,適用於小中型mysql資料庫#刪除15天以前備份#fafu_li#時間:2015.08.10source /etc/profile        #載入系統內容變數source ~/.bash_profile    #載入使用者環境變數set -o nounset             #引用未初始設定變數時退出#set -o errexit            #執行shell命令遇到錯誤時退出user="root"password="123456"host="localhost"port="3306"#需備份的資料庫,數組db=("test")#備份時加鎖方式,#MyISAM為鎖表--lock-all-tables,#InnoDB為鎖行--single-transactionlock="--single-transaction"mysql_path="/usr/local/mysql"backup_path="${mysql_path}/backup"date=$(date +%Y-%m-%d_%H-%M-%S)day=15backup_log="${mysql_path}/backup.log"#建立備份目錄if [ ! -e $backup_path ];then    mkdir -p $backup_pathfi#刪除以前備份find $backup_path -type f -mtime +$day -exec rm -rf {} \; > /dev/null 2>&1echo "開始備份資料庫:${db[*]}"#備份並壓縮backup_sql(){    dbname=$1    backup_name="${dbname}_${date}.sql"    #-R備份預存程序,函數,觸發器    mysqldump -h $host -P $port -u $user -p$password $lock --default-character-set=utf8 --flush-logs -R $dbname > $backup_path/$backup_name        if [[ $? == 0 ]];then        cd $backup_path        tar zcpvf $backup_name.tar.gz $backup_name        size=$(du $backup_name.tar.gz -sh | awk ‘{print $1}‘)        rm -rf $backup_name        echo "$date 備份 $dbname($size) 成功 "    else        cd $backup_path        rm -rf $backup_name        echo "$date 備份 $dbname 失敗 "    fi}#迴圈備份length=${#db[@]}for (( i = 0; i < $length; i++ )); do        backup_sql ${db[$i]} >> $backup_log 2>&1doneecho "備份結束,結果查看 $backup_log"du $backup_path/*$date* -sh | awk ‘{print "檔案:" $2 ",大小:" $1}‘

完美的mysql備份指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.