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

posted @ 2013-03-22 17:12  elar  阅读(5284)  评论(2编辑  收藏  举报