MYSQL 基于时间点的恢复
MYSQL备份的数据:
1. Innodb存储引擎数据文件
1.1 ibdata*系统表空间文件,存放内部数据词典,回归段包含undo数据,change buffer和doublewriter buffer。
1.2 ib logfile*日志文件,记录事务活动。
1.3 ibd单表数据文件,记录单表的数据。
1.4 frm文件,记录单表的数据词典。
2. 数据目录下的所有文件
2.1 MYD数据文件。
2.2 MYI索引文件
2.3 FRM表数据词典文件
3. 配置文件my cnf配置文件
4. bin log记录变化的日志
备份策略
定期全备和增备
1. 全备
mysqldump --single-transaction --all-databases -uroot --flush-logs > backup_sunday_1_PM.sql
2. 增备,执行下列命令,并拷贝binlog文件到安全目录。
mysqladmin flush-logs
恢复
1. 恢复全备
mysql -uroot -p < backup_sunday_1_PM.sql
2. 恢复增备
mysqlbinlog gbichot2-bin.000007 gbichot2-bin.000008 --stop-date="2005-04-20 9:59:59" | mysql -uroot -p
举例:
1. 生成测试数据:
mysql> select * from mytab;
+------+------------+---------------------+
| id | created | modified |
+------+------------+---------------------+
| 1 | 2014-04-07 | 2014-04-13 11:24:53 |
| 2 | 2014-04-07 | 2014-04-13 11:24:53 |
| 3 | 2014-04-07 | 2014-04-13 11:24:53 |
+------+------------+---------------------+
2. 执行全备
mysqldump --login-path=root --single-transaction --events --triggers --routines --all-databases --flush-logs > /tmp/backup_all_databases.sql
mysql> select * from mytab;
3. 生成增备测试数据
+------+------------+---------------------+
| id | created | modified |
+------+------------+---------------------+
| 1 | 2014-04-07 | 2014-04-13 11:24:53 |
| 2 | 2014-04-07 | 2014-04-13 11:24:53 |
| 3 | 2014-04-07 | 2014-04-13 11:24:53 |
| 4 | 2014-04-13 | 2014-04-13 11:27:34 |
+------+------------+---------------------+
4. 生成新的binlog文件
mysqladmin flush-logs -uroot -p
5. 生成新的测试数据
+------+------------+---------------------+
| id | created | modified |
+------+------------+---------------------+
| 1 | 2014-04-07 | 2014-04-13 11:24:53 |
| 2 | 2014-04-07 | 2014-04-13 11:24:53 |
| 3 | 2014-04-07 | 2014-04-13 11:24:53 |
| 4 | 2014-04-13 | 2014-04-13 11:27:34 |
| 4 | 2014-04-13 | 2014-04-13 11:31:39 |
+------+------------+---------------------+
6. 删除mysql数据文件
7. 新建数据库
cd $MYSQL_HOME
scripts/mysql_install_db --defaults-file=$MYSQL_HOME/my.cnf
cd $MYSQL_HOME
mysqld_safe --defaults-file=$MYSQL_HOME/my.cnf &
8. 恢复数据库,恢复全备
mysql -uroot -p < /tmp/backup_all_databases.sql
mysql> select * from mytab;
+------+------------+---------------------+
| id | created | modified |
+------+------------+---------------------+
| 1 | 2014-04-07 | 2014-04-13 11:24:53 |
| 2 | 2014-04-07 | 2014-04-13 11:24:53 |
| 3 | 2014-04-07 | 2014-04-13 11:24:53 |
+------+------------+---------------------+
9. 恢复增备到11:30
mysqlbinlog mysql-bin.000001 mysql-bin.000002 mysql-bin.000003 mysql-bin.000004 --start-datetime="2014-04-13 11:24:55" --stop-datetime="2014-04-13 11:30:55" | mysql -uroot -p
+------+------------+---------------------+
| id | created | modified |
+------+------------+---------------------+
| 1 | 2014-04-07 | 2014-04-13 11:24:53 |
| 2 | 2014-04-07 | 2014-04-13 11:24:53 |
| 3 | 2014-04-07 | 2014-04-13 11:24:53 |
| 4 | 2014-04-13 | 2014-04-13 11:27:34 |
+------+------------+---------------------+