mysqlDatabase Backup指令碼

來源:互聯網
上載者:User

標籤:備份   mysql   mysql備份   mysqldump   

備份mysql資料庫指令碼。

手動建立目錄/var/backups/mysqldata   也可以自訂,只需修改變數backup_dir的值即可。

#!/bin/bash#mysql備份指令碼database=test001mysql_user=rootmysql_password=123.combackup_dir=/var/backups/mysqldataname=`date "+%Y_%m_%d"` for i in $database  do   [ -d ${backup_dir}/${i} ] || mkdir ${backup_dir}/${i}/   mysqldump -u$mysql_user -p$mysql_password --database $i > ${backup_dir}/${i}/${i}_${name}.sql   file_n=`ls ${backup_dir}/${i}/ | wc -l`   rm_file=`ls -l ${backup_dir}/${i}/ | head -2 | tail -1 | awk ‘{print $9}‘`if [ $file_n -gt 3 ] then  rm -f ${backup_dir}/${i}/$rm_filefi  done


#!/bin/bash

#mysql備份指令碼

database=test001           #定義要備份的資料庫,以空格隔開

mysql_user=root            #定義備份資料庫使用的使用者

mysql_password=123.com        #定義使用者密碼

backup_dir=/var/backups/mysqldata  #定義備份到的目錄

name=`date "+%Y_%m_%d"`        #定義備份名稱尾碼,這裡以時間為尾碼


 for i in $database        

  do

   [ -d ${backup_dir}/${i} ] || mkdir ${backup_dir}/${i}/    

#判斷是否有資料庫同名目錄,若沒有則建立一個

   mysqldump -u$mysql_user -p$mysql_password --database $i > ${backup_dir}/${i}/${i}_${name}.sql

#使用mysqldump對資料庫進行備份

   file_n=`ls ${backup_dir}/${i}/ | wc -l`

#查看一個備份目錄下檔案個數

   rm_file=`ls -l ${backup_dir}/${i}/ | head -2 | tail -1 | awk ‘{print $9}‘`

#查看備份目錄下第一個檔案名稱,我這裡查看的是第二行,因為使用ls -l的話,第一行顯示的目錄詳情

if [ $file_n -gt 30 ]

#判斷如果一個備份目錄中的備份檔案數是否大於30個

 then

  rm -f ${backup_dir}/${i}/$rm_file

#如果檔案數大於30個的話,則刪除第一個檔案,可以理解為最多保留三十天的備份檔案

fi

  done


在crontab中寫入計劃任務,每天執行一次指令碼即可。

本文出自 “西門吹牛” 部落格,請務必保留此出處http://taixin.blog.51cto.com/11725086/1828377

mysqlDatabase Backup指令碼

聯繫我們

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