CentOS5.4下自動備份MySQL資料庫

來源:互聯網
上載者:User

利用系統crontab來定時執行備份檔案,按日期對備份結果進行儲存,達到備份的目的。

1、建立儲存備份檔案的路徑

#mkdir /opt/databack/mysql

2、建立備份指令檔

#vi /usr/sbin/mysqlback.sh

輸入

date=` date +%Y%m%d `

tar zcvf /opt/databack/mysql/mysql$date.tar.gz /var/lib/mysql

或者寫成

date=` date +%Y%m%d `

mysqldump –all-databases -u root -p密碼 > /opt/databack/mysql/mysql$rq.sql

/var/lib/mysql是你資料庫檔案的目錄,部分使用者是/usr/local/mysql/data,每個人可能不同

/opt/databack/mysql/表示儲存備份檔案的目錄,這個每個人也可以根據自己的要求來做。

3、修改檔案屬性,使其可執行

# chmod +x /usr/sbin/mysqlbak.sh


4、修改/etc/crontab

#vi /etc/crontab

在下面添加

01 1 * * * root /usr/sbin/mysqlbak.sh

表示每天淩晨1點鐘執行備份

5、重新啟動crond

# /etc/rc.d/init.d/crond restart

完成。

這樣每天你在/opt/databack/mysql/可以看到這樣的檔案

mysql20100301.tar.gz   或者是mysql20100301.sql

相關文章

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.