ZRM(Zmanda For MySQL) 是一个功能丰富的针对于MySQL数据库实施数据备份与恢复的开源工具,其中针对于MySQL数据库社区版的ZRM提供免费下载使用。
下面就来具体介绍ZRM的使用方法和一些注意事项。
工作环境:Fedora 7 + MySQL 5.0.45 + ZRM 2.1.1
安装ZRM: rpm -ivh MySQL-zrm-2.1.1-1.noarch.rpm
rpm -ivh perl-XML-Parser-2.36-1.el5.rf.i386.rpm (ZRM需要的perl模块)
(NOTE: 相关安装文件可以在 http://zmanda.com/download-zrm.php 的提示连接中下载。在安装zrm所需的两个模块perl-DBI和perl-XML-parser的时候折腾了半天,之前想通过CPAN去安装,但是后来发现都安装好了后,rpm去安装zrm的时候始终找不到perl-XML-parser这个模块,最后折腾半天还是安装上面连接中提示的地方下了一个rpm版的安装包,之后就没有问题了。)
使用前,我们先来整体了解下这个软件的整体结构,rpm安装好的zrm文件夹分布情况如下:
/etc/mysql-zrm目录下默认有4个文件mysql-zrm.conf (zrm配置文件)、mysql-zrm-reporter.conf 、mysql-zrm-release 、RSS.header。随着备份工作的展开这个目录下会自动生成一些备份文件夹,由参数-backup-set来定义(BackupSet1为默认文件夹的名字),每个文件夹下的last_backup文件中都会记录最近一次备份位置。
/var/lib/mysql-zrm/目录中是每次备份的具体数据对应文件的存放位置。
2. Backup
a.) 全备
逻辑备份: shell> mysql-zrm-backup -backup-set test -backup-level 0 -backup-mode logical -database test -tables employee
backup:INFO: ZRM for MySQL Community Edition - version 2.1.1
test:backup:INFO: START OF BACKUP
test:backup:INFO: PHASE START: Initialization
test:backup:INFO: The quick backup-type is supported only for snapshot backups. Setting backup-type to ‘regular’
test:backup:INFO: backup-set=test
test:backup:INFO: backup-date=20100319180120
test:backup:INFO: mysql-server-os=Linux/Unix
test:backup:INFO: backup-type=regular
test:backup:INFO: host=localhost
test:backup:INFO: backup-date-epoch=1268992880
test:backup:INFO: retention-policy=1D
test:backup:INFO: mysql-zrm-version=ZRM for MySQL Community Edition - version 2.1.1
test:backup:INFO: mysql-version=5.0.45-log
test:backup:INFO: backup-directory=/var/lib/mysql-zrm/test/20100319180120
test:backup:INFO: backup-level=0
test:backup:INFO: backup-mode=logical
test:backup:INFO: PHASE END: Initialization
test:backup:INFO: PHASE START: Running pre backup plugin
test:backup:INFO: PHASE END: Running pre backup plugin
test:backup:INFO: PHASE START: Flushing logs
test:backup:INFO: PHASE END: Flushing logs
test:backup:INFO: PHASE START: Creating logical backup
test:backup:INFO: logical-tables=employee
logical-databases=test
test:backup:INFO: PHASE END: Creating logical backup
test:backup:INFO: PHASE START: Calculating backup size & checksums
test:backup:INFO: next-binlog=mysql-bin.000005
test:backup:INFO: last-backup=/var/lib/mysql-zrm/test/20100319100944
test:backup:INFO: backup-size=0.01 MB
test:backup:INFO: PHASE END: Calculating backup size & checksums
test:backup:INFO: read-locks-time=00:00:00
test:backup:INFO: flush-logs-time=00:00:00
test:backup:INFO: backup-time=00:00:00
test:backup:INFO: backup-status=Backup succeeded
test:backup:INFO: Backup succeeded
test:backup:INFO: PHASE START: Running post backup plugin
test:backup:INFO: PHASE END: Running post backup plugin
test:backup:INFO: PHASE START: Mailing backup report
test:backup:INFO: PHASE END: Mailing backup report
test:backup:INFO: PHASE START: Cleanup
test:backup:INFO: PHASE END: Cleanup
test:backup:INFO: END OF BACKUP
You have new mail in /var/spool/mail/root
以上为执行后的输出信息,我们可以看到比较详细,有备份的路径以及执行情况,系统以及数据库的信息等。
shell> ls /var/lib/mysql-zrm/test/ (查看备份数据)
20100319180120 (NOTE: 生成一个以日期为文件夹名的目录。)
shell> ls /var/lib/mysql20100319180120/
backup.sql index zrm_checksum
(NOTE: backup.sql— 数据库dump文件; index— 内容与上面的输出信息相同;zrm_checksum— 记录了本次操作的备份路径以及dump文件的校验码。)
物理备份: shell> mysql-zrm-backup -backup-set test -backup-level 0 -backup-mode raw -database test -tables employee
shell> ls /var/lib/mysql-zrm/test/20100319182600/
index test zrm_checksum
shell> ls /var/lib/mysql-zrm/test/20100319182600/test
employee.frm employee.MYD employee.MYI
(NOTE: 前后的两次差别,在于物理备份后,会产生一个对应的备份目录,里面是所备份表的相关物理文件。)
b.) 增量备份
shell> mysql-zrm-backup -backup-set test -backup-level 1 -backup-mode logical -database test -tables employee
shell> /var/lib/mysql-zrm/test/20100319183735
index mysql-bin.000006 zrm_checksum
(NOTE: 增量与全备比较之后,我们会发现增备备份的是二进制文件。所以,我们也学会了如何自己去做增量备份。)
3. Restore
mysql> drop table employee;
shell> mysql-zrm-restore -source-directory /var/lib/mysql-zrm/test/20100319180120 -databases test -user root -password eikenuat -host 192.168.11.172
restore:INFO: ZRM for MySQL Community Edition - version 2.1.1
BackupSet1:restore:INFO: Restored database(s) from logical backup: test
BackupSet1:restore:INFO: Restore done in 0 seconds.
(NOTE: 上面没有错误信息,恢复成功,查看test库,发现已经employee表已经被恢复。)
4. 自动定时备份 (NOTE: 进行执行计划前,我们需要提前在配置文件mysql-zrm.conf中配置好所有的选项。)
shell> mysql-zrm-scheduler -add -interval daily -start 02:30 —- 每天凌晨2点半进行备份工作
schedule:INFO: ZRM for MySQL Community Edition - version 2.1.1
Logging to /var/log/mysql-zrm/mysql-zrm-scheduler.log
DONE
shell> mysql-zrm-scheduler -query —- 查看备份计划任务
schedule:INFO: ZRM for MySQL Community Edition - version 2.1.1
Logging to /var/log/mysql-zrm/mysql-zrm-scheduler.log
35 13 * * * /usr/bin/zrm-pre-scheduler -action backup -backup-set BackupSet1 -backup-level 1 -interval daily
shell> mysql-zrm-scheduler -delete -interval daily -start 02:30 —- 删除计划任务
schedule:INFO: ZRM for MySQL Community Edition - version 2.1.1
Logging to /var/log/mysql-zrm/mysql-zrm-scheduler.log
1 crontab entry deleted
DONE
以上就是使用备份与恢复的一些基本操作。在ZRM的命令中有个有个mysql-zrm命令这个命令可以间接执行大部分的操作,比如,上面的命令还可以这样执行> mysql-zrm -action backup | restore … …
之前一直在用Xtrabackup这个备份工具,备份也还可以,就是当你要去回复的时候,就得自己动手把之前的备份文件放到要回复数据库的相应位置上去,稍微让我们感觉到一丝的不爽。当这周开始尝试使用ZRM后,觉得所有操作都是那么的和谐,一切都是那么轻松简单,就像10年前我们初次体验windows一样,敲敲点点的那个爽呀。ZRM也提供给我们远程备份异地数据库的实现,需要在远端的server上安装socket copy plugin插件。其实,生产当中会有一个专门提供数据备份的server,上面安装好ZRM,然后在各个数据库sever上安装那个socket_copy插件,供ZRM连接使用。
不错吧,大家也试试吧。—- HAPPY WEEKEND & GOOD LUCK !