数据库备份方案:
全量备份
增量备份
差异备份
备份方案 | 特点 |
---|---|
全量备份 | 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。 数据恢复快。 备份时间长 |
增量备份 | 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份 与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象 是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量 备份后所产生的增加和修改的文件,如此类推。 没有重复的备份数据 备份时间短 恢复数据时必须按一定的顺序进行 |
差异备份 | 备份上一次的完全备份后发生变化的所有文件。 差异备份是指在一次全备份后到进行差异备份的这段时间内 对那些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。 |
语法:
mysqldump [OPTIONS] database [tables ...]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
常用的OPTIONS:
-uUSERNAME //指定数据库用户名
-hHOST //指定服务器主机,请使用ip地址
-pPASSWORD //指定数据库用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
备份整个数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hanyuce |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> show tables;
+-------------------+
| Tables_in_hanyuce |
+-------------------+
| xiaoxiong |
+-------------------+
1 row in set (0.00 sec)
[root@xiaoxiong ~]# mysqldump -uroot -p123456 --all-databases > all_20190819.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@xiaoxiong ~]# ls
all_20190819.sql daxiong mysql_hanyuce.sh
anaconda-ks.cfg daxiong.tar.bz2
备份hanyuce库的xiaoxiong表
[root@xiaoxiong ~]# mysqldump -uroot -p123456 hanyuce xiaoxiong > xiaoxiong_20190819.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@xiaoxiong ~]#
[root@xiaoxiong ~]# ls
all_20190819.sql daxiong mysql_hanyuce.sh
anaconda-ks.cfg daxiong.tar.bz2 xiaoxiong_20190819.sql
备份hanyuce数据库
[root@xiaoxiong ~]# mysqldump -uroot -p123456 hanyuce > hanyuce_20190819.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@xiaoxiong ~]# ls
all_20190819.sql daxiong.tar.bz2 xiaoxiong_20190819.sql
anaconda-ks.cfg hanyuce_20190819.sql
daxiong mysql_hanyuce.sh
模拟误删hanyuce数据库
mysql> drop database hanyuce
-> ;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
恢复hanyuce数据库
[root@xiaoxiong ~]# mysql -uroot -p123456 < hanyuce_20190819.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hanyuce |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
开启MySQL服务器的二进制日志功能
[root@xiaoxiong ~]# vim /etc/my.cnf
[root@xiaoxiong ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin=mysql-bin #启用binlog日志
server-id=1 #数据库服务器唯一标识符,主库的server-id值必须比从库的小
对数据库进行完全备份
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hanyuce |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables from hanyuce;
+-------------------+
| Tables_in_hanyuce |
+-------------------+
| hyc |
| xiaoxiong |
+-------------------+
2 rows in set (0.00 sec)
mysql> select * from hanyuce.xiaoxiong;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | daxiong | 10 |
| 2 | haha | 15 |
| 3 | xixi | 17 |
| 4 | heihei | 20 |
| 6 | dada | 30 |
| 7 | xiaoxion | 22 |
| 8 | dadaguia | 5 |
+----+----------+------+
7 rows in set (0.00 sec)
mysql> select * from hanyuce.hyc;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
+----+-------+------+
2 rows in set (0.00 sec)
完全备份
[root@xiaoxiong ~]# mysqldump -uroot -p123456 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all_20190819.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@xiaoxiong ~]# ls
all_20190819.sql daxiong mysql_hanyuce.sh
anaconda-ks.cfg daxiong.tar.bz2
增加新内容
mysql> use hanyuce;
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> insert into hyc values(9,'wangqing',20);
mysql> insert into hyc values(9,'wangqing',20);
Query OK, 1 row affected (0.00 sec)
mysql> select * from hyc;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 9 | wangqing | 20 |
+----+----------+------+
3 rows in set (0.00 sec)
模拟误删数据
mysql> drop database hanyuce;
Query OK, 2 rows affected (0.06 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
由此可以看到hanyuce数据库已经删除
[root@xiaoxiong ~]# ll /opt/data/
总用量 122976
-rw-r-----. 1 mysql mysql 56 8月 19 16:01 auto.cnf
-rw-r-----. 1 mysql mysql 716 8月 19 19:30 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 8月 21 13:25 ibdata1
-rw-r-----. 1 mysql mysql 50331648 8月 21 13:25 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 8月 19 16:01 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 8月 21 11:20 ibtmp1
-rw-r-----. 1 mysql mysql 475 8月 19 19:29 musql-bin.000002
-rw-r-----. 1 mysql mysql 177 8月 19 19:30 musql-bin.000003
-rw-r-----. 1 mysql mysql 38 8月 19 19:29 musql-bin.index
drwxr-x---. 2 mysql mysql 4096 8月 19 19:05 mysql
-rw-r-----. 1 mysql mysql 320 8月 21 13:25 mysql-bin.000001
-rw-r-----. 1 mysql mysql 19 8月 21 11:20 mysql-bin.index
-rw-r-----. 1 mysql mysql 5 8月 21 11:20 mysql.pid
drwxr-x---. 2 mysql mysql 8192 8月 19 16:01 performance_schema
drwxr-x---. 2 mysql mysql 8192 8月 19 16:01 sys
-rw-r-----. 1 mysql mysql 34578 8月 21 11:23 xiaoxiong.err
[root@xiaoxiong ~]# mysqladmin -uroot -p123456 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@xiaoxiong ~]# ll /opt/data/
总用量 122980
-rw-r-----. 1 mysql mysql 56 8月 19 16:01 auto.cnf
-rw-r-----. 1 mysql mysql 716 8月 19 19:30 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 8月 21 13:25 ibdata1
-rw-r-----. 1 mysql mysql 50331648 8月 21 13:25 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 8月 19 16:01 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 8月 21 11:20 ibtmp1
-rw-r-----. 1 mysql mysql 475 8月 19 19:29 musql-bin.000002
-rw-r-----. 1 mysql mysql 177 8月 19 19:30 musql-bin.000003
-rw-r-----. 1 mysql mysql 38 8月 19 19:29 musql-bin.index
drwxr-x---. 2 mysql mysql 4096 8月 19 19:05 mysql
-rw-r-----. 1 mysql mysql 367 8月 21 13:26 mysql-bin.000001
-rw-r-----. 1 mysql mysql 154 8月 21 13:26 mysql-bin.000002
-rw-r-----. 1 mysql mysql 38 8月 21 13:26 mysql-bin.index
-rw-r-----. 1 mysql mysql 5 8月 21 11:20 mysql.pid
drwxr-x---. 2 mysql mysql 8192 8月 19 16:01 performance_schema
drwxr-x---. 2 mysql mysql 8192 8月 19 16:01 sys
-rw-r-----. 1 mysql mysql 34578 8月 21 11:23 xiaoxiong.err
恢复完全备份
[root@xiaoxiong ~]# mysql -uroot -p123456 < all_20190819.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hanyuce |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use hanyuce;
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_hanyuce |
+-------------------+
| hyc |
| xiaoxiong |
+-------------------+
2 rows in set (0.00 sec)
mysql> show binlog events in 'mysql-bin.000001'\G
*************************** 1. row ***************************
Log_name: mysql-bin.000001
Pos: 4
Event_type: Format_desc
Server_id: 1
End_log_pos: 123
Info: Server ver: 5.7.22-log, Binlog ver: 4
*************************** 2. row ***************************
Log_name: mysql-bin.000001
Pos: 123
Event_type: Previous_gtids
Server_id: 1
End_log_pos: 154
Info:
*************************** 3. row ***************************
Log_name: mysql-bin.000001
Pos: 154
Event_type: Anonymous_Gtid
Server_id: 1
End_log_pos: 219
Info: SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
*************************** 4. row ***************************
Log_name: mysql-bin.000001
Pos: 219
Event_type: Query
Server_id: 1
End_log_pos: 320
Info: drop database hanyuce
*************************** 5. row ***************************
Log_name: mysql-bin.000001
Pos: 320
Event_type: Rotate
Server_id: 1
End_log_pos: 367
Info: mysql-bin.000002;pos=4
5 rows in set (0.00 sec)
可以看到误删除数据库的位置在219
使用mysqlbinlog恢复差异备份
[root@xiaoxiong ~]# mysqlbinlog --stop-position=219 /opt/data/mysql-bin.000001 |mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@xiaoxiong ~]# mysql -uroot -p123456 -e 'select * from hanyuce.hyc;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
+----+-------+------+