标签:mysql 备份 mysqldump 恢复 xtrabackup
1.mysqldump:
shell> mysqldump [options] db_name [tbl_name ...] #可以备份单个数据库的单个表,不会自动创建数据库,恢复时需要先创建数据库 shell> mysqldump [options] --databases db_name ... shell> mysqldump [options] --all-databases 例如: mysqldump -uroot --databases hellodb > /root/hellodbbak.sql #仅备份一个数据库 mysqldump -uroot --databases hellodb mysql > /root/dbbak.sql #同时备份多个库
对于MyIsam引擎的数据库:
MyISAM:支持温备;锁定备份库,而后启动备份操作;
锁定方法:
--lock-all-tables:锁定所有库的所有表;
--lock-tables:对于每个单独的数据库,在启动备份之前锁定其所有表
上面两个参数对InnoDB表一样生效,实现温备;
InnoDB:支持热备;
--single-transaction 备份数据前启动一个事务,达到数据一致效果
其它选项:
-E, --events:备份指定数据库相关的所有event scheduler;
-R, --routines:备份指定数据库相关的所有存储过程和存储函数;
--triggers:备份表相关的触发器;
--master-data[=#]:
1:记录为CHANGE MASTER TO语句,此语句不被注释;
2:记录为注释的CHANGE MASTER TO语句;
CHANGE MASTER TO记录了启动备份前时间处于哪个二进制文件的哪个位置
--flush-logs:锁定表完成后,执行flush logs命令可以实现二进制日志滚动
注意:二进制日志文件不应该与数据文件放在同一磁盘;二进制日志文件记录了全库信息,不能执行单独库还原,除非编辑它只针对一个库作用后再还原。
应该经常对二进制文件做增量备份,例如每过半填,让二进制日志自动滚动一次,把之前的备份起来。
示例: [root@testserver ~]# mysqldump -uroot --databases hellodb --lock-tables --master-data=2 > /root/hellodbbak.sql [root@testserver ~]# less hellodbbak.sql | grep CHANGE -- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000011‘, MASTER_LOG_POS=7655;#这样就得到了当前使用的二进制日志文件名和当前记录位置 修改当前库里的某些数据: MariaDB [hellodb]> INSERT INTO students (Name,Age,Gender,ClassID,TeacherID) VALUES (‘Chao Gai‘,47,‘M‘,3,7); Query OK, 1 row affected (0.00 sec) DELETE FROM students WHERE StuID=3; Query OK, 1 row affected (0.00 sec) 此时mysql服务器挂了,我们把数据还原到另一个服务器上,执行恢复操作: 还原前要注意先要关掉Bin_log set bin_log=0,等还原完了再开启bin_log [root@node1 ~]# mysql < hellodbbak.sql 查看恢复的状况:发现没有完全恢复:晁盖没进来,谢烟客还在 MariaDB [hellodb]> SELECT * FROM students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) 继续使用二进制文件恢复:根据上面列出来的二进制日志文件和当前记录位置 [root@testserver ~]# mysqlbinlog --start-position=7655 /mydata/data/mysql-bin.000011 > incre.sql 看一下这个二进制日志文件: [root@testserver ~]# mysqlbinlog --start-position 7655 /mydata/data/mysql-bin.000011 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #151110 21:35:57 server id 1 end_log_pos 245 Start: binlog v 4, server v 5.5.44-MariaDB-log created 151110 21:35:57 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ‘ vfJBVg8BAAAA8QAAAPUAAAABAAQANS41LjQ0LU1hcmlhREItbG9nAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAC98kFWEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAA8nm5Lg== ‘/*!*/; # at 7655 #151110 23:14:20 server id 1 end_log_pos 7726 Query thread_id=10 exec_time=0 error_code=0 SET TIMESTAMP=1447168460/*!*/; SET @@session.pseudo_thread_id=10/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=0/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; BEGIN /*!*/; # at 7726 #151110 23:14:20 server id 1 end_log_pos 7754 Intvar SET INSERT_ID=26/*!*/; # at 7754 #151110 23:14:20 server id 1 end_log_pos 7907 Query thread_id=10 exec_time=0 error_code=0 use `hellodb`/*!*/; SET TIMESTAMP=1447168460/*!*/; INSERT INTO students (Name,Age,Gender,ClassID,TeacherID) VALUES (‘Chao Gai‘,47,‘M‘,3,7) /*!*/; # at 7907 #151110 23:14:20 server id 1 end_log_pos 7934 Xid = 780 COMMIT/*!*/; # at 7934 #151110 23:16:55 server id 1 end_log_pos 8005 Query thread_id=10 exec_time=0 error_code=0 SET TIMESTAMP=1447168615/*!*/; BEGIN /*!*/; # at 8005 #151110 23:16:55 server id 1 end_log_pos 8105 Query thread_id=10 exec_time=0 error_code=0 SET TIMESTAMP=1447168615/*!*/; DELETE FROM students WHERE StuID=3 /*!*/; # at 8105 #151110 23:16:55 server id 1 end_log_pos 8132 Xid = 781 COMMIT/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/; 拷贝到那台好的服务器进行还原: [root@testserver ~]# scp incre.sql root@192.168.3.131:/root/ root@192.168.3.131‘s password: incre.sql 100% 2373 2.3KB/s 00:00 [root@node1 ~]# mysql < incre.sql 查看:发现谢烟客没了,晁盖来了,数据被完整恢复了! +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | | 26 | Chao Gai | 47 | M | 3 | 7 | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec)
本文出自 “明天过后” 博客,请务必保留此出处http://leeyan.blog.51cto.com/8379003/1711630
标签:mysql 备份 mysqldump 恢复 xtrabackup
原文地址:http://leeyan.blog.51cto.com/8379003/1711630