CentOS下使用Automysqlbackup工具自動備份MySQLLinux下使用crontab來執行定時任務計劃

來源:互聯網
上載者:User

本來看到舊版的server administration可以自動備份的,想著workbench應該也可以……沒想到竟然不行,而且mysql只是考慮將這個功能加到community plugin中。憂桑……還好有人寫了個AutoMySQLBackup共用在了sourceforge上。試試看怎麼樣吧。

人家在sourceforge上的介紹:

Description

AutoMySQLBackup with a basic configuration will create Daily, Weekly and Monthly backups of one or more of your MySQL databases from one or more of your MySQL servers.

Other Features include:
- Email notification of backups (會有email通知)
- Backup Compression and Encryption (使用壓縮和加密)
- Configurable backup rotation (儲存的備份檔案時間)
- Incremental database backups (增量型備份)

 

1.下載AutoMySQLBackup

http://sourceforge.net/projects/automysqlbackup/

2.解壓縮

把下載的automysqlbackup-v3.0_rc6.tar.gz檔案拷貝到/usr/tmp下面

在/usr/local下面建立一個automysqlbackup檔案夾,並進入這個檔案夾

cd /usr/local

mkdir automysqlbackup

cd automysqlbackup

然後terminal解壓縮一下。

tar -xzvf /usr/tmp/automysqlbackup-v3.0_rc6.tar.gz

3. 安裝

安裝一下,命令為

./install.sh

中途會有兩個詢問設定檔安裝目錄的地方,之間斷行符號就好

安裝完以後會有提示資訊,告訴你如果不是目前使用者下使用這個命令要怎麼辦:

我們先到/etc/automysqlbackup下面看看有什麼東西好了

目測有四個檔案……

4.修改設定檔

我們要用到的是automysqlbackup.conf檔案:

檔案裡有一些基本的配置資訊,比如串連mysql server的使用者名稱、密碼、IP地址神馬的。

# Username to access the MySQL server e.g. dbuserCONFIG_mysql_dump_username='root'# Password to access the MySQL server e.g. passwordCONFIG_mysql_dump_password='1234'# Host name (or IP address) of MySQL server e.g localhostCONFIG_mysql_dump_host='localhost'

繼續,有個重要的配置,就是backup存放的地方咯!

# Backup directory location e.g /backupsCONFIG_backup_dir='/var/backup/db'

往下看,還有你要配置的database的名稱,當然可以精確到表名,也可以只指定到database的名稱。或者乾脆直接留空,不過留空的話會預設備份所有的資料庫……這樣磁碟可能會爆炸吧……

# Databases to backup# List of databases for Daily/Weekly Backup e.g. ( 'DB1' 'DB2' 'DB3' ... )# set to (), i.e. empty, if you want to backup all databasesCONFIG_db_names=()# You can use#declare -a MDBNAMES=( "${DBNAMES[@]}" 'added entry1' 'added entry2' ... )# INSTEAD to copy the contents of $DBNAMES and add further entries (optional).# List of databases for Monthly Backups.# set to (), i.e. empty, if you want to backup all databasesCONFIG_db_month_names=()# List of DBNAMES to EXLUCDE if DBNAMES is empty, i.e. ().CONFIG_db_exclude=( 'information_schema' 'wiqun' )

另外,還有配置weekly、monthly、daily之類的時間間隔的設定

# Rotation Settings# Which day do you want monthly backups? (01 to 31)# If the chosen day is greater than the last day of the month, it will be done# on the last day of the month.# Set to 0 to disable monthly backups.CONFIG_do_monthly="22"# Which day do you want weekly backups? (1 to 7 where 1 is Monday)# Set to 0 to disable weekly backups.CONFIG_do_weekly="7"# Set rotation of daily backups. VALUE*24hours# If you want to keep only today's backups, you could choose 1, i.e. everything older than 24hours will be removed.CONFIG_rotation_daily=7# Set rotation for weekly backups. VALUE*24hoursCONFIG_rotation_weekly=35# Set rotation for monthly backups. VALUE*24hoursCONFIG_rotation_monthly=150

前兩個都比較好理解,就是每個月或者每一周的什麼時候進行自動備份,如果不想使用每周備份或者每月備份的話,相應的地方設定0即可。那麼後面的rotation又是什麼意思呢?其實就是日誌儲存的期限啦。

比如說CONFIG_rotation_weekly=35的意思就是說按周儲存的備份最多保留35天。

再繼續,可以配置發送郵件的一些配置,比如郵件地址啦、還有附件的內容啦。

# What would you like to be mailed to you?# - log   : send only log file# - files : send log file and sql files as attachments (see docs)# - stdout : will simply output the log to the screen if run manually.# - quiet : Only send logs if an error occurs to the MAILADDR.CONFIG_mailcontent='files'# Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])CONFIG_mail_maxattsize=4000# Allow packing of files with tar and splitting it in pieces of CONFIG_mail_maxattsize.CONFIG_mail_splitandtar='yes'# Use uuencode instead of mutt. WARNING: Not all email clients work well with uuencoded attachments.#CONFIG_mail_use_uuencoded_attachments='no'# Email Address to send mail to? (user@domain.com)CONFIG_mail_address='elarwei@gmail.com'

5. 運行Automysqlbackup

要運行automysqlbackup之前呢,不要忘了先建立我們存放備份資料庫檔案的地方,即在設定檔中設定的“/var/backup/db”,現在我們來建立這個目錄。

mkdir /var/backup

mkdir /var/backup/db

好了,現在我們可以run一下automysqlbackup了。命令是:

automysqlbackup /etc/automyslqbackup/automysqlbackup.conf

噔噔,怎麼報錯了呢?

Parsed config file "/etc/automysqlbackup/automysqlbackup.conf"# Checking for permissions to write to folders:base folder /var/backup ... exists ... ok.backup folder /var/backup/db ... exists ... writable? yes. Proceeding./usr/local/bin/automysqlbackup: line 139: mutt: command not found

好吧,有事問Google。

sourceForge上果然也有人遇到過這個問題……,解答是

Please set CONFIG_mailcontent='stdout' in your config file and try again from your console.
The output should directly be displayed.
It seems some error occured before the check for config variables could determine
whether there is a need for checking if mutt (mail program) is installed and can be used.
Therefore the error handler tried to mail the errors using mutt (your configuration choice), which isn't there.

大 意就是說在設定檔裡面使用了發送email來報告error這個東東,但是你的機子上卻沒安裝mutt這個程式,所以在檢測配置的時候就發生了錯誤。解 決的方法是把報錯改成“stdout”,不過如果改成“stdout”不就失去了我們發送報告到email 的本意了嗎……so,我們安裝一下mutt這個程式好了。

(PS:如果不使用發送郵件則不需要這個步驟)

5.1 安裝mutt

命令是

yum install mutt

安裝完了以後我們再重新run一下automysqlbackup。

這次沒有報錯了。我們到/var/backup/db下面看一下有沒有什麼變化

cd /var/backup/db

ls

出現了幾個檔案夾哦

6. 設定Cron Job

Cron 是一個可以讓系統定時運行後台指令碼的工具。

6.1 編寫可執行指令碼

我們在/usr/local/bin目錄下編寫一個要定時執行的指令碼

#!/bin/sh/usr/local/bin/automysqlbackup /etc/automysqlbackup/automysqlbackup.conf

儲存為backupscript.sh

然後改變其模式為root可以執行、讀寫的方式

chmod 744 /usr/local/bin/backupscript.sh

6.2 將可執行指令碼加入定時任務中

先使用命令

crontab -l

看一下現在cron中的定時程式有哪些。

如果提示“no crontab for root”的話,說明root使用者下現在還沒有定時任務。我們可以建立一個檔案,用來配置root使用者下的定時任務。

vi root-conf

然後在vi中輸入

0-59/10 * * * * /usr/local/bin/backupscript.sh

意思是每十分鐘執行一下備份指令碼程式(比較誇張啦,為了能在短時間內看到比較多備份的結果,一般都是設定某一天的一個時刻來執行)然後儲存退出。

接著,使用命令

crontab -u root root-conf

來將剛才編輯的檔案作為root下的定時工作清單檔案。這樣再鍵入crontab -l命令時,就可以查看到我們剛才設定的定時任務了。另外,使用crontab -e命令可以編輯我們的定時任務。具體的設定時間的方法可以看Kaixuan的這篇文章《Linux下使用crontab來執行定時任務計劃》。

 

7.查看備份結果

我們進入備份的目錄看一下有什麼變化

cd /var/backup/db/daily

ls

可以看到兩個目錄

 

 進入mysql

14:52的時候daily目錄內的備份如:

17:05的時候daily目錄內的備份如:

查看郵件裡面,也可以看到發送的備份email,如下

不過gmail自動把這些郵件當成垃圾郵件了,所以如果郵箱裡看不到的話找找垃圾郵件裡面……

Reference

How to do Painless MySQL Server Backups with AutoMySQLBackup

 

其他的備份工具還有

XtraBackup

相關文章

聯繫我們

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