利用mysqldump和mysqlbinlog实现完全备份和增量备份
(2017-11-26 23:26:31)
标签:
mysqldumpmysqlbinlogmysql完全备份mysql增量备份 |
一、数据备份基础:
1、备份类型:
完全备份:整个数据集;
部分部分:只备份数据子集;
增量备份:仅备份最近一次完全备份或增量备份(如果存在增量)以来变化的数据;
差异备份:仅备份最近一次完全备份以来变化的数据;
2、备份方式:
(1)、与存储引擎有关:热备份、温备份、冷备份:
(2)、与存储引擎无关:物理备份、逻辑备份
3、引擎支持的备份方式:
MyISAM:支持温备,不能热备
4、备份时需要考虑的因素:
5、备份什么
6、备份方案的设计:
7、备份工具:
mysqldump:逻辑备份工具,适用所有存储引擎,温备;完全备份、部分备份;对InnoDB存储引擎支持热备;
cp,tar等复制归档工具:物理备份工具,适用所有存储引擎;冷备:完全备份,部分备份;
mysqlbinlog:查看二进制日志文件并利用其做增量备份
二、备份工具详解:mysqldump、mysqlbinlog
1、逻辑备份工具:mysqldump,mydumper,phpMyAdmin
(1)、mysqldump:客户端命令,通过mysql协议连接至mysqld服务器
(2)引擎支持备份方式及实现:
MyISAM:只支持温备;必须锁定备份库,而后才能启动备份操作,
支持锁定的参数如下:
(3)、其他选项:
2、备份建议:
二进制日志文件不应该与数据文件放在同一磁盘;
建议还原时关闭二进制日志记录
三、利用mysqldump做完全备份还原
1、mysqldump做部分备份和完全备份:
[root@www ~]# cp hellodb.sql /root/
cp: "hellodb.sql" 与"/root/hellodb.sql" 为同一文件
[root@www ~]# file hellodb.sql
hellodb.sql: ASCII text, with very long lines
[root@www ~]# less hellodb.sql
-- MySQL dump 10.13
--
-- Current Database: `hellodb`
CREATE DATABASE `hellodb` ;
USE `hellodb`;
-- Table structure for table `classes`
DROP TABLE IF EXISTS `classes`;
;
;
CREATE TABLE `classes` (
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
;
#修改MyISAM引擎为InnoDB,使其支持热备
[root@www ~]# sed -i 's/MyISAM/InonoDB/g' hellodb.sql
[root@www ~]# sed -i 's/InonoDB/InnoDB/g' hellodb.sql
[root@www ~]# less hellodb.sql
-- MySQL dump 10.13
--
-- Host: localhost
-- ------------------------------------------------------
-- Server version
-- MySQL dump 10.13
--
-- Host: localhost
-- ------------------------------------------------------
-- Server version
DROP TABLE IF EXISTS `classes`;
;
;
CREATE TABLE `classes` (
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
;
--
-- Dumping data for table `classes`
--
mysql> SHOW DATABASES;
+--------------------+
| Database
+--------------------+
| information_schema |
| hellodb
| mysql
| test
| testdb
+--------------------+
5 rows in set (0.04 sec)
mysql> use hellodb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes
| coc
| courses
| scores
| students
| teachers
| toc
+-------------------+
7 rows in set (0.00 sec)
mysql> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name
+-------+---------------+-----+--------+---------+-----------+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.01 sec)
mysql> \q
Bye
[root@www ~]# mysqldump -uroot
Usage: mysqldump [OPTIONS] database [tables]
OR
OR
For more options, use mysqldump --help
[root@www ~]# mysqldump -uroot --databases hellodb
没有输出重定向则默认输出
-- MySQL dump 10.13
-- Host: localhost
-- ------------------------------------------------------
-- Server version
CREATE DATABASE `hellodb` ;
.......
部分备份:将hellodb.sql备份到/root/hellodb.sql
[root@www ~]# mysqldump -uroot --databases hellodb > /root/hellodb.sql
[root@www ~]# ls
hellodb.sql
[root@www ~]# mkdir /root/sqlbak
[root@www ~]# mysqldump -uroot --database hellodb >/root/sqlbak/hellodb.sql
[root@www ~]# cd sqlbak
[root@www sqlbak]# ls
hellodb.sql
[root@www sqlbak]# less hellodb.sql
-- MySQL dump 10.13
--
-- Host: localhost
-- ------------------------------------------------------
-- Server version
CREATE DATABASE `hellodb` ;
DROP TABLE IF EXISTS `classes`;
;
;
CREATE TABLE `classes` (
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
;
......
#将hellodb、testdb两个数据库都备份到/root/sqlbak/hellodb_mydb.sql下
[root@www ~]# mysqldump -uroot --databases hellodb testdb > /root/sqlbak/hellodb_mydb.sql
[root@www ~]# cd sqlbak
[root@www sqlbak]# ls
hellodb_mydb.sql
[root@www sqlbak]#
[root@www sqlbak]# cd
备份所有数据库的数据到/root/sqlbak/all.sql文件下
[root@www ~]# mysqldump -uroot --all-databases --lock-all-tables --master-data=2 > /root/sqlbak/all.sql
mysqldump: Error: Binlogging on server not active
提示Binlogging为启动,
编辑/etc/my.cnf文件,设置log_bin=ON
[root@www etc]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log_bin=ON
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@www ~]# service mysqld restart
停止 mysqld: [确定]
正在启动 mysqld: [确定]
[root@www ~]# mysql
Welcome to the MySQL monitor.
mysql> SHOW GLOBAL VARIABLES LIKE '%log%';
+-----------------------------------------+---------------------------------+
| Variable_name
+-----------------------------------------+---------------------------------+
| back_log
| binlog_cache_size
| binlog_direct_non_transactional_updates |
OFF
| binlog_format
| expire_logs_days
| general_log
| general_log_file
| innodb_flush_log_at_trx_commit
| innodb_locks_unsafe_for_binlog
| innodb_log_buffer_size
| innodb_log_file_size
| innodb_log_files_in_group
| innodb_log_group_home_dir
| innodb_mirrored_log_groups
| log
| log_bin
| log_bin_trust_function_creators
| log_bin_trust_routine_creators
| log_error
| log_output
| log_queries_not_using_indexes
| log_slave_updates
| log_slow_queries
| log_warnings
| max_binlog_cache_size
| max_binlog_size
| max_relay_log_size
| relay_log
| relay_log_index
| relay_log_info_file
| relay_log_purge
| relay_log_space_limit
| slow_query_log
| slow_query_log_file
| sql_log_bin
| sql_log_off
| sql_log_update
| sync_binlog
+-----------------------------------------+---------------------------------+
38 rows in set (0.00 sec)
mysql> SHOW MASTER LOGS;
+-----------+--------------+
| Log_name
+-----------+--------------+
| ON.000001 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql>
[root@www ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log_bin=mysql-bin
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
~
"/etc/my.cnf" 11L, 269C 已写入
[root@www ~]# service mysqld restart
停止 mysqld: [确定]
正在启动 mysqld: [确定]
[root@www ~]# mysql
Welcome to the MySQL monitor.
Your MySQL connection id is 2
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW GLOBAL VARIABLES LIKE '%log%';
+-----------------------------------------+---------------------------------+
| Variable_name
+-----------------------------------------+---------------------------------+
| back_log
| binlog_cache_size
| binlog_direct_non_transactional_updates |
OFF
| binlog_format
| expire_logs_days
| general_log
| general_log_file
| innodb_flush_log_at_trx_commit
| innodb_locks_unsafe_for_binlog
| innodb_log_buffer_size
| innodb_log_file_size
| innodb_log_files_in_group
| innodb_log_group_home_dir
| innodb_mirrored_log_groups
| log
| log_bin
| log_bin_trust_function_creators
| log_bin_trust_routine_creators
| log_error
| log_output
| log_queries_not_using_indexes
| log_slave_updates
| log_slow_queries
| log_warnings
| max_binlog_cache_size
| max_binlog_size
| max_relay_log_size
| relay_log
| relay_log_index
| relay_log_info_file
| relay_log_purge
| relay_log_space_limit
| slow_query_log
| slow_query_log_file
| sql_log_bin
| sql_log_off
| sql_log_update
| sync_binlog
+-----------------------------------------+---------------------------------+
38 rows in set (0.00 sec)
mysql> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name
+------------------+-----------+
| mysql-bin.000001 |
+------------------+-----------+
1 row in set (0.00 sec)
做全量备份,备份所有数据到/root/sqlbak/all.sql文件中
[root@www ~]# mysqldump -uroot --all-databases --lock-all-tables --master-data=2 > /root/sqlbak/all.sql
[root@www ~]# less ./sqlbak/all.sql
2、完全备份还原:
[root@www ~]# scp ./sqlbak/all.sql 192.168.88.130:/root/
root@192.168.88.130's password:
all.sql
备用机上192.168.88.130:
[root@test ~]# service mysqld start
正在启动 mysqld: [确定]
[root@test ~]# mysql
mysql> show databases;
+--------------------+
| Database
+--------------------+
| information_schema |
| mysql
| test
+--------------------+
3 rows in set (0.05 sec)
mysql> quit
Bye
将完全备份导入
[root@test ~]# mysql < all.sql
[root@test ~]# mysql
mysql> show databases;
+--------------------+
| Database
+--------------------+
| information_schema |
| hellodb
| mysql
| test
| testdb
+--------------------+
5 rows in set (0.00 sec)
mysql> use hellodb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name
+-------+---------------+-----+--------+---------+-----------+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.01 sec)
修改的部分未导入
mysql> quit
Bye
四、利用mysqlbinlog做增量备份还原
1、为完全备份后所作的修改做增量备份:--start-position=106
mysql> use hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
#插入数据,有意做修改,演示增量备份
mysql> INSERT INTO students (Name,Age,Gender,ClassID,TeacherID) VALUES ('Cao Cao',15,'M',3,7);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name
+-------+---------------+-----+--------+---------+-----------+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+-------+---------------+-----+--------+---------+-----------+
26 rows in set (0.00 sec)
mysql> DELETE FROM students WHERE StuID=3;
Query OK, 1 row affected (0.01 sec)
mysql> QUIT
Bye
#没有输出重定向则默认输出到标准输出
[root@www ~]# mysqlbinlog --start-position=106 /var/lib/mysql/mysql-bin.000001
;
;
DELIMITER ;
# at 4
#171123
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK;
BINLOG '
......
[root@www ~]# mysqlbinlog --start-position=106 /var/lib/mysql/mysql-bin.000001 > incre.sql
利用二进制日志记录还原修改后的记录,做增量备份
结合二进制日志记录做时间点还原
[root@www ~]# ls
apr-1.5.2
[root@www ~]# scp incre.sql 192.168.88.130:/root
root@192.168.88.130's password:
incre.sql
2、还原增量备份
[root@test ~]# ls
all.sql
[root@test ~]# mysql < incre.sql
[root@test ~]# mysql
mysql> use hellodb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name
+-------+---------------+-----+--------+---------+-----------+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)