用shell指令碼自動化備份資料庫

來源:互聯網
上載者:User

標籤:楊文   伺服器   master   server   資料庫   關鍵字   

防偽碼:防止誤刪系統資料庫,定時備份你我更安心!              

      使用shell指令碼自動化備份資料庫

一、簡介

系統資料庫(以MySQL5.5為例)

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/87/48/wKiom1faI52jq-ihAAAGaW9Asms259.png-wh_500x0-wm_3-wmp_4-s_437084086.png" title="9FD`TY5O~GEBBOZYL_T$H84.png " alt="wKiom1faI52jq-ihAAAGaW9Asms259.png-wh_50" />

1、information_schema:資訊資料庫,其中儲存著關於MySQL伺服器所維護的所有其他資料庫的資訊。在information_schema中,有數個唯讀表。它們實際上是視圖,而不是基本表,因此,你將無法看到與之相關的任何檔案。

2、mysql:MySQL的的核心資料庫,類似於SQLServer中的master表,主要負責儲存資料庫的使用者、使用權限設定、關鍵字、mysql自己需要使用的控制和管理資訊等。不可以刪除,如果對mysql不是很瞭解,也不要輕易修改這個資料庫裡面的表資訊。

3、performance_schema:儲存引擎,主要用於收集資料庫伺服器績效參數。

4、test:安裝時候建立的一個測試資料庫,和它的名字一樣,是一個完全的空資料庫,沒有任何錶,可以刪除。

二、編寫指令碼,自動化備份資料庫(以test為例)

1、開啟服務

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/87/49/wKiom1faJlmxHq_JAAAEdxGTGXE978.png-wh_500x0-wm_3-wmp_4-s_3676472631.png" title="W[F}RP7]$7GCWRH%A~[DNLW.png" alt="wKiom1faJlmxHq_JAAAEdxGTGXE978.png-wh_50" />

2、使用者授權

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/87/46/wKioL1faJnzSHXS2AAAFCblPjtk396.png-wh_500x0-wm_3-wmp_4-s_567182841.png" title="(H~5UE{K7N$7{WXQ_Z68)D4.png" alt="wKioL1faJnzSHXS2AAAFCblPjtk396.png-wh_50" />

3、編寫指令碼(vim yy.sh)

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/87/46/wKioL1faJqPQ-vYkAAAR9ZD7kTE236.png-wh_500x0-wm_3-wmp_4-s_1657854436.png" title="79(OA_FSNXO$P](4]KWQV6T.png" alt="wKioL1faJqPQ-vYkAAAR9ZD7kTE236.png-wh_50" />

指令碼如下:

#!/bin/bash

#auto backup

back=/data/backup/`date +%Y%m%d`

mysqldb=test

mysqlpw=123456

mysqluser=backup

mysqlcmd=$(which mysqldump)


if [ $UID -ne 0 ];then

         echo "bixushiyongroot"

         exit

fi

if [ ! -d $back ];then

   mkdir -p   $back

else

   echo  "this  $back  is exit............."

fi


$mysqlcmd  -u$mysqluser  -p$mysqlpw  -d $mysqldb >$back/$mysqldb.sql


if [ $?  -eq 0 ];then

               echo  "The backup successful"

else

               echo    "Backup failure"

fi



4、設定執行許可權並執行指令碼

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/87/49/wKiom1faJtDSm2dfAAAUIOCOzVs977.png-wh_500x0-wm_3-wmp_4-s_3280256739.png" title="94I7J~EZF]$RE9YVP{897C0.png" alt="wKiom1faJtDSm2dfAAAUIOCOzVs977.png-wh_50" />

三、根據工作需要,設定計劃任務(crontab -e)


本文出自 “一盞燭光” 部落格,謝絕轉載!

用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.