CentOS下使用Automysqlbackup工具自動備份MySQL


本來看到舊版的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. dbuser
CONFIG_mysql_dump_username='root'

# Password to access the MySQL server e.g. password
CONFIG_mysql_dump_password='1234'

# Host name (or IP address) of MySQL server e.g localhost
CONFIG_mysql_dump_host='localhost'

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

# Backup directory location e.g /backups
CONFIG_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 databases
CONFIG_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 databases
CONFIG_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*24hours
CONFIG_rotation_weekly=35

# Set rotation for monthly backups. VALUE*24hours
CONFIG_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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM