安装ZRM:
[root@master ~]# yum -y install perl-DBI perl-DBD-MySQL perl-XML-Parser [root@master ~]# yum localinstall MySQL-zrm-3.0-1.noarch.rpm [root@master ~]# ls /etc/mysql-zrm/ mysql-zrm.conf mysql-zrm-release mysql-zrm-reporter.conf RSS.header [root@master mysql-zrm]# ln -s /usr/local/mysql/bin/* /usr/bin/
创建备份用户并授权
mysql> grant select, insert, update, create, drop, reload, shutdown, alter, super, lock tables, replication client on *.* to 'backup-user'@'localhost' identified by 'redhat'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
创建备份目录
[root@master ~]# mkdir /backup/zrm -pv [root@master mysql-zrm]# mkdir full
修改配置文件:
[root@master full]# egrep -v '^#|^$' mysql-zrm.conf backup-level=0 backup-mode=raw lvm-snapshot=100M snapshot-plugin="/usr/share/mysql-zrm/plugins/lvm-snapshot.pl" backup-type=regular destination=/backup/zrm retention-policy=1W compress=1 compress-plugin=/usr/bin/gzip all-databases=1 user="backup-user" password="redhat" host="localhost" socket=/tmp/mysql.sock mysql-binlog-path="/mydata/data" mailto="[email protected]"
手工执行备份:
[root@master mysql-zrm]# mysql-zrm-scheduler --now --backup-set full schedule:INFO: ZRM for MySQL Community Edition - version 3.0 Logging to /var/log/mysql-zrm/mysql-zrm-scheduler.log backup:INFO: ZRM for MySQL Community Edition - version 3.0 full:backup:INFO: START OF BACKUP full:backup:INFO: PHASE START: Initialization full:backup:WARNING: The lvm-snapshot option is deprecated. Use snapshot-size instead full:backup:INFO: backup-set=full full:backup:INFO: backup-date=20130827000610 full:backup:INFO: mysql-server-os=Linux/Unix full:backup:INFO: backup-type=regular full:backup:INFO: host=localhost full:backup:INFO: backup-date-epoch=1377533170 full:backup:INFO: retention-policy=1W full:backup:INFO: mysql-zrm-version=ZRM for MySQL Community Edition - version 3.0 full:backup:INFO: mysql-version=5.5.28-log full:backup:INFO: backup-directory=/backup/zrm/full/20130827000610 full:backup:INFO: backup-level=0 full:backup:INFO: backup-mode=raw full:backup:INFO: PHASE END: Initialization full:backup:INFO: PHASE START: Running pre backup plugin full:backup:INFO: PHASE END: Running pre backup plugin full:backup:INFO: PHASE START: Flushing logs full:backup:INFO: PHASE END: Flushing logs full:backup:INFO: PHASE START: Creating snapshot based backup full:backup:INFO: File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000019 107 full:backup:INFO: innodb-data=/mydata/data/ibdata1; full:backup:INFO: innodb-logs=/mydata/data/./ib_logfile* full:backup:INFO: raw-databases-snapshot=test shop mysql performance_schema bbs full:backup:INFO: PHASE END: Creating snapshot based backup full:backup:INFO: PHASE START: Calculating backup size & checksums full:backup:INFO: next-binlog=mysql-bin.000019 full:backup:INFO: backup-size=35.41 MB full:backup:INFO: PHASE END: Calculating backup size & checksums full:backup:INFO: PHASE START: Compression/Encryption full:backup:INFO: compress=/usr/bin/gzip full:backup:INFO: backup-size-compressed=1.60 MB full:backup:INFO: PHASE END: Compression/Encryption full:backup:INFO: read-locks-time=00:00:02 full:backup:INFO: flush-logs-time=00:00:01 full:backup:INFO: compress-encrypt-time=00:00:20 full:backup:INFO: backup-time=00:00:15 full:backup:INFO: backup-status=Backup succeeded full:backup:INFO: Backup succeeded full:backup:INFO: PHASE START: Running post backup plugin full:backup:INFO: PHASE END: Running post backup plugin full:backup:INFO: PHASE START: Mailing backup report full:backup:INFO: PHASE END: Mailing backup report full:backup:INFO: PHASE START: Cleanup full:backup:INFO: PHASE END: Cleanup full:backup:INFO: END OF BACKUP /usr/bin/mysql-zrm started successfully
备份过程使用了逻辑卷快照功能
root@master ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/mydata-mydata ext4 5.0G 182M 4.5G 4% /mydata /dev/mapper/mydata-zrmQGzbaZ6jWo ext4 5.0G 182M 4.5G 4% /backup/zrm/full/20130827000610/ZRM_MOUNTS/zrmQGzbaZ6jWo [root@master mysql-zrm]# ll -h full/ total 4.0K -rw-r--r-- 1 root root 32 Aug 27 00:06 last_backup
指向备份集所在的目录
[root@master mysql-zrm]# cat full/last_backup /backup/zrm/full/20130827000610 [root@master mysql-zrm]# ll -h /backup/zrm/full/20130827000610/ total 1.7M #启用了压缩功能的缘故 -rw-r--r-- 1 root root 1.6M Aug 27 00:06 backup-data -rw-r--r-- 1 root root 680 Aug 27 00:06 index -rw-r--r-- 1 root root 126 Aug 27 00:06 zrm_checksum
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bbs | | mysql | | performance_schema | | shop | | test | +--------------------+ 6 rows in set (0.00 sec)
插入一条记录
mysql> insert into shop.ecs_users(user_name,last_time) values('week0',now()); Query OK, 1 row affected, 7 warnings (0.00 sec) mysql> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | | 6 | week0 | 2013-08-27 00:15:21 | +---------+-----------+---------------------+ 5 rows in set (0.00 sec) mysql> drop database shop; Query OK, 88 rows affected (0.13 sec)
还原(备份后发生的数据改变需要用二进制日志恢复):
[root@master mysql-zrm]# mysql-zrm-reporter --show restore-info --where backup-set=full REPORT TYPE : restore-info backup_set backup_date backup_level backup_directory backup_status comment ----------------------------------------------------------------------------------------------------------------------------------------------------- full Tue 27 Aug 2013 12:06:10 0 /backup/zrm/full/20130827000610 Backup succeeded ---- AM CST
校验备份集
[root@master mysql-zrm]# mysql-zrm --action verify-backup --backup-set full verify-backup:INFO: ZRM for MySQL Community Edition - version 3.0 full:verify-backup:INFO: Verification successful
还原
root@master mysql-zrm]# mysql-zrm --action restore --backup-set full --source-directory /backup/zrm/full/20130827000610 restore:INFO: ZRM for MySQL Community Edition - version 3.0 dailyrun:restore:WARNING: The lvm-snapshot option is deprecated. Use snapshot-size instead dailyrun:restore:INFO: Restored innodb log '/mydata/data/ib_logfile0' dailyrun:restore:INFO: Restored innodb log '/mydata/data/ib_logfile1' dailyrun:restore:INFO: Restored innodb data file '/mydata/data/ibdata1' dailyrun:restore:INFO: Restored database from raw backup: test dailyrun:restore:INFO: Restored database from raw backup: shop dailyrun:restore:INFO: Restored database from raw backup: mysql dailyrun:restore:INFO: Restored database from raw backup: performance_schema dailyrun:restore:INFO: Restored database from raw backup: bbs dailyrun:restore:INFO: Restore done in 31 seconds. MySQL server has been shutdown. Please restart after verification. [root@master ~]# service mysqld start Starting MySQL... SUCCESS! mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bbs | | mysql | | performance_schema | | shop | | test | +--------------------+ 6 rows in set (0.00 sec) mysql> use shop Database changed mysql> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | +---------+-----------+---------------------+ 4 rows in set (0.00 sec) 记录中不含week0
增量备份
插入一条记录
mysql> insert into shop.ecs_users(user_name,last_time) values('week0',now()); Query OK, 1 row affected, 7 warnings (0.03 sec) mysql> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | | 6 | week0 | 2013-08-27 01:11:39 | +---------+-----------+---------------------+ 5 rows in set (0.00 sec)
全备
[root@master mysql-zrm]# mkdir mybak-`date +%F` [root@master mysql-zrm]# cp mysql-zrm.conf mybak-`date +%F` [root@master mysql-zrm]# mysql-zrm-scheduler --now --backup-set mybak-`date +%F` schedule:INFO: ZRM for MySQL Community Edition - version 3.0 Logging to /var/log/mysql-zrm/mysql-zrm-scheduler.log backup:INFO: ZRM for MySQL Community Edition - version 3.0 mybak-2013-08-27:backup:INFO: START OF BACKUP mybak-2013-08-27:backup:INFO: PHASE START: Initialization mybak-2013-08-27:backup:WARNING: The lvm-snapshot option is deprecated. Use snapshot-size instead mybak-2013-08-27:backup:INFO: backup-set=mybak-2013-08-27 mybak-2013-08-27:backup:INFO: backup-date=20130827012922 mybak-2013-08-27:backup:INFO: mysql-server-os=Linux/Unix mybak-2013-08-27:backup:INFO: backup-type=regular mybak-2013-08-27:backup:INFO: host=localhost mybak-2013-08-27:backup:INFO: backup-date-epoch=1377538162 mybak-2013-08-27:backup:INFO: retention-policy=1W mybak-2013-08-27:backup:INFO: mysql-zrm-version=ZRM for MySQL Community Edition - version 3.0 mybak-2013-08-27:backup:INFO: mysql-version=5.5.28-log mybak-2013-08-27:backup:INFO: backup-directory=/backup/zrm/mybak-2013-08-27/20130827012922 mybak-2013-08-27:backup:INFO: backup-level=0 mybak-2013-08-27:backup:INFO: backup-mode=raw mybak-2013-08-27:backup:INFO: PHASE END: Initialization mybak-2013-08-27:backup:INFO: PHASE START: Running pre backup plugin mybak-2013-08-27:backup:INFO: PHASE END: Running pre backup plugin mybak-2013-08-27:backup:INFO: PHASE START: Flushing logs mybak-2013-08-27:backup:INFO: PHASE END: Flushing logs mybak-2013-08-27:backup:INFO: PHASE START: Creating snapshot based backup mybak-2013-08-27:backup:INFO: File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000026 107 mybak-2013-08-27:backup:INFO: innodb-data=/mydata/data/ibdata1; mybak-2013-08-27:backup:INFO: innodb-logs=/mydata/data/./ib_logfile* mybak-2013-08-27:backup:INFO: raw-databases-snapshot=test shop mysql performance_schema bbs mybak-2013-08-27:backup:INFO: PHASE END: Creating snapshot based backup mybak-2013-08-27:backup:INFO: PHASE START: Calculating backup size & checksums mybak-2013-08-27:backup:INFO: next-binlog=mysql-bin.000026 mybak-2013-08-27:backup:INFO: backup-size=35.41 MB mybak-2013-08-27:backup:INFO: PHASE END: Calculating backup size & checksums mybak-2013-08-27:backup:INFO: PHASE START: Compression/Encryption mybak-2013-08-27:backup:INFO: compress=/usr/bin/gzip mybak-2013-08-27:backup:INFO: backup-size-compressed=1.60 MB mybak-2013-08-27:backup:INFO: PHASE END: Compression/Encryption mybak-2013-08-27:backup:INFO: read-locks-time=00:00:01 mybak-2013-08-27:backup:INFO: flush-logs-time=00:00:01 mybak-2013-08-27:backup:INFO: compress-encrypt-time=00:00:20 mybak-2013-08-27:backup:INFO: backup-time=00:00:09 mybak-2013-08-27:backup:INFO: backup-status=Backup succeeded mybak-2013-08-27:backup:INFO: Backup succeeded mybak-2013-08-27:backup:INFO: PHASE START: Running post backup plugin mybak-2013-08-27:backup:INFO: PHASE END: Running post backup plugin mybak-2013-08-27:backup:INFO: PHASE START: Mailing backup report mybak-2013-08-27:backup:INFO: PHASE END: Mailing backup report mybak-2013-08-27:backup:INFO: PHASE START: Cleanup mybak-2013-08-27:backup:INFO: PHASE END: Cleanup mybak-2013-08-27:backup:INFO: END OF BACKUP /usr/bin/mysql-zrm started successfully [root@master ~]# du /backup/zrm/mybak-`date +%F`/* -sh 1.7M /backup/zrm/mybak-2013-08-27/20130827012922
执行增量备份:
[root@master mysql-zrm]# mysql-zrm-scheduler --now --backup-level 1 --backup-set mybak-`date +%F` schedule:INFO: ZRM for MySQL Community Edition - version 3.0 Logging to /var/log/mysql-zrm/mysql-zrm-scheduler.log backup:INFO: ZRM for MySQL Community Edition - version 3.0 mybak-2013-08-27:backup:INFO: START OF BACKUP mybak-2013-08-27:backup:INFO: PHASE START: Initialization mybak-2013-08-27:backup:WARNING: The lvm-snapshot option is deprecated. Use snapshot-size instead mybak-2013-08-27:backup:INFO: backup-set=mybak-2013-08-27 mybak-2013-08-27:backup:INFO: backup-date=20130827013928 mybak-2013-08-27:backup:INFO: mysql-server-os=Linux/Unix mybak-2013-08-27:backup:INFO: backup-type=regular mybak-2013-08-27:backup:INFO: host=localhost mybak-2013-08-27:backup:INFO: backup-date-epoch=1377538768 mybak-2013-08-27:backup:INFO: retention-policy=1W mybak-2013-08-27:backup:INFO: mysql-zrm-version=ZRM for MySQL Community Edition - version 3.0 mybak-2013-08-27:backup:INFO: mysql-version=5.5.28-log mybak-2013-08-27:backup:INFO: backup-directory=/backup/zrm/mybak-2013-08-27/20130827013928 mybak-2013-08-27:backup:INFO: backup-level=1 mybak-2013-08-27:backup:INFO: PHASE END: Initialization mybak-2013-08-27:backup:INFO: PHASE START: Running pre backup plugin mybak-2013-08-27:backup:INFO: PHASE END: Running pre backup plugin mybak-2013-08-27:backup:INFO: PHASE START: Flushing logs mybak-2013-08-27:backup:INFO: PHASE END: Flushing logs mybak-2013-08-27:backup:INFO: PHASE START: Creating incremental backup src = /mydata/data/mysql-bin.000026 k = /backup/zrm/mybak-2013-08-27/20130827013928/mysql-bin.000026 mybak-2013-08-27:backup:INFO: incremental=mysql-bin.[0-9]* mybak-2013-08-27:backup:INFO: PHASE END: Creating incremental backup mybak-2013-08-27:backup:INFO: PHASE START: Calculating backup size & checksums mybak-2013-08-27:backup:INFO: next-binlog=mysql-bin.000027 mybak-2013-08-27:backup:INFO: last-backup=/backup/zrm/mybak-2013-08-27/20130827012922 mybak-2013-08-27:backup:INFO: backup-size=0.00 MB mybak-2013-08-27:backup:INFO: PHASE END: Calculating backup size & checksums mybak-2013-08-27:backup:INFO: PHASE START: Compression/Encryption mybak-2013-08-27:backup:INFO: compress=/usr/bin/gzip mybak-2013-08-27:backup:INFO: backup-size-compressed=0.00 MB mybak-2013-08-27:backup:INFO: PHASE END: Compression/Encryption mybak-2013-08-27:backup:INFO: read-locks-time=00:00:00 mybak-2013-08-27:backup:INFO: flush-logs-time=00:00:01 mybak-2013-08-27:backup:INFO: compress-encrypt-time=00:00:00 mybak-2013-08-27:backup:INFO: backup-time=00:00:01 mybak-2013-08-27:backup:INFO: backup-status=Backup succeeded mybak-2013-08-27:backup:INFO: Backup succeeded mybak-2013-08-27:backup:INFO: PHASE START: Running post backup plugin mybak-2013-08-27:backup:INFO: PHASE END: Running post backup plugin mybak-2013-08-27:backup:INFO: PHASE START: Mailing backup report mybak-2013-08-27:backup:INFO: PHASE END: Mailing backup report mybak-2013-08-27:backup:INFO: PHASE START: Cleanup mybak-2013-08-27:backup:INFO: PHASE END: Cleanup mybak-2013-08-27:backup:INFO: END OF BACKUP /usr/bin/mysql-zrm started successfully [root@master mysql-zrm]# du /backup/zrm/mybak-`date +%F`/* -sh 40M /backup/zrm/mybak-2013-08-27/20130827012922 16K /backup/zrm/mybak-2013-08-27/20130827013928
增量备份只有16K
全备中不含binlog
[root@master mysql-zrm]# mysql-zrm-parse-binlogs --backup-set mybak-`date +%F` --source-directory /backup/zrm/mybak-2013-08-27/20130827012922/ parse-binlogs:INFO: ZRM for MySQL Community Edition - version 3.0 mybak-2013-08-27:parse-binlogs:ERROR: /backup/zrm/mybak-2013-08-27/20130827012922/ contains a full backup and there are no binary logs in this directory.
增量备份中binlog信息中包新增记录:
[root@master mysql-zrm]# mysql-zrm-parse-binlogs --backup-set mybak-`date +%F` --source-directory /backup/zrm/mybak-2013-08-27/20130827013928/ parse-binlogs:INFO: ZRM for MySQL Community Edition - version 3.0
Log filename | Log Position | Timestamp | Event Type | Event |
/backup/zrm/mybak-2013-08-27/20130827013928/mysql-bin.000026 | 4 | 13-08-27 01:29:25 | Start: binlog v 4, server v 5.5.28-log created 130827 1:29:25 | |
/backup/zrm/mybak-2013-08-27/20130827013928/mysql-bin.000026 | 107 | 13-08-27 01:33:00 | Query | /*!\C utf8 *//*!*/; BEGIN /*!*/; |
/backup/zrm/mybak-2013-08-27/20130827013928/mysql-bin.000026 | 179 | 13-08-27 01:33:00 | Intvar | |
/backup/zrm/mybak-2013-08-27/20130827013928/mysql-bin.000026 | 207 | 13-08-27 01:33:00 | Query | insert into shop.ecs_users(user_name,last_time) values('week0',now()) /*!*/; |
/backup/zrm/mybak-2013-08-27/20130827013928/mysql-bin.000026 | 343 | 13-08-27 01:33:00 | Query | COMMIT /*!*/; |
/backup/zrm/mybak-2013-08-27/20130827013928/mysql-bin.000026 | 416 | 13-08-27 01:39:28 | Rotate to mysql-bin.000027 pos: 4 |
模拟误操作删除shop库:
mysql> drop database shop; Query OK, 88 rows affected (0.06 sec)
还原测试:
看看备份集信息
[root@master mysql-zrm]# mysql-zrm-reporter --show restore-info --where backup-set=mybak-2013-08-27 REPORT TYPE : restore-info backup_set backup_date backup_level backup_directory backup_status comment ----------------------------------------------------------------------------------------------------------------------------------------------------- mybak-2013-08-27 Tue 27 Aug 2013 01:39:28 1 /backup/zrm/mybak-2013-08-27/20130827013 Backup succeeded ---- AM CST 928 mybak-2013-08-27 Tue 27 Aug 2013 01:29:22 0 /backup/zrm/mybak-2013-08-27/20130827012 Backup succeeded ---- AM CST 922
先用全备还原
[root@master mysql-zrm]# mysql-zrm --action restore --backup-set mybak-2013-08-27 --source-directory /backup/zrm/mybak-2013-08-27/20130827012922/ restore:INFO: ZRM for MySQL Community Edition - version 3.0 mybak-2013-08-27:restore:WARNING: The lvm-snapshot option is deprecated. Use snapshot-size instead mybak-2013-08-27:restore:INFO: Restored innodb log '/mydata/data/ib_logfile0' mybak-2013-08-27:restore:INFO: Restored innodb log '/mydata/data/ib_logfile1' mybak-2013-08-27:restore:INFO: Restored innodb data file '/mydata/data/ibdata1' mybak-2013-08-27:restore:INFO: Restored database from raw backup: test mybak-2013-08-27:restore:INFO: Restored database from raw backup: shop mybak-2013-08-27:restore:INFO: Restored database from raw backup: mysql mybak-2013-08-27:restore:INFO: Restored database from raw backup: performance_schema mybak-2013-08-27:restore:INFO: Restored database from raw backup: bbs mybak-2013-08-27:restore:INFO: Restore done in 42 seconds. MySQL server has been shutdown. Please restart after verification. [root@master ~]# service mysqld start Starting MySQL... SUCCESS! mysql> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | +---------+-----------+---------------------+ 4 rows in set (0.00 sec)
此时shop库已还原,但是全备后插入的记录(week0)还未恢复
用增量备份还原:
[root@master mysql-zrm]# mysql-zrm --action restore --backup-set mybak-2013-08-27 --source-directory /backup/zrm/mybak-2013-08-27/20130827013928/ restore:INFO: ZRM for MySQL Community Edition - version 3.0 mybak-2013-08-27:restore:WARNING: The lvm-snapshot option is deprecated. Use snapshot-size instead mybak-2013-08-27:restore:INFO: BINLOG = mysqlbinlog --user="backup-user" --password="*****" --host="localhost" --socket="/tmp/mysql.sock" "/backup/zrm/mybak-2013-08-27/20130827013928/"/mysql-bin.[0-9]* >> /tmp/e88CEUVrrj mybak-2013-08-27:restore:INFO: Incremental restore done mybak-2013-08-27:restore:INFO: Restore done in 0 seconds. mysql> select user_id,user_name,last_time from shop.ecs_users; +---------+-----------+---------------------+ | user_id | user_name | last_time | +---------+-----------+---------------------+ | 1 | ecshop | 0000-00-00 00:00:00 | | 2 | vip | 0000-00-00 00:00:00 | | 3 | text | 0000-00-00 00:00:00 | | 5 | zuanshi | 0000-00-00 00:00:00 | | 6 | week0 | 2013-08-27 01:33:00 | +---------+-----------+---------------------+ 5 rows in set (0.01 sec)
至此数据已找回,增备到误操作期间的数据需借助binlog人工恢复,这里不再演示
备份脚本:
#!/bin/bash # # Written by Sanyu ,not tested # Name1=lvm-month Name2=lvm-week mkdir /backup/zrm 2>/dev/null mkdir /etc/mysql-zrm/$Name1 mkdir /etc/mysql-zrm/$Name2 mkconf () { cat >$1<