1.物理备份:对数据库操作系统的物理文件(如数据文件、日志文件等)的备份
物理备份又可以分为脱机备份(冷备份)和联机备份(热备份)
(1)冷备份:是在关闭数据库的时候进行的
(2)热备份:数据库处于运行状态,这种备份方法依赖于数据库的日志文件
2.逻辑备份:对数据库逻辑组件(如表等数据库对象)的备份
从数据库的备份策略角度,备份可分为完全备份、差异备份、增量备份
完全备份:每次对数据进行完整的备份
差异备份:备份那些自从上次完全备份之后被修改过的文件
增量备份:只有那些在上次完全备份或者增量备份后被修改的文件才会被备份
MySQL完全备份
完全备份是对整个数据库的备份、数据库结构的文件结构的备份
完全备份保存的是备份完成时刻的数据库
完全被封是增量备份的基础
完全备份的优点
MySQL数据库的备份可以采用多种方式
//安装环境依赖包
yum install -y ncurses-devel autoconf cmake gcc gcc-c++
//解压缩包
tar xzvf mysql-5.6.26.tar.gz
cd mysql-5.6.26
//配置过程
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ //定义安装目录
-DDEFAULT_CHARSET=utf8 \ //安装中文识别的字符集
-DDEFAULT_COLLATION=utf8_general_ci \ //安装中文识别的字符集
-DEXTRA_CHARSETS=all \
-DSYSCONFIDIR=/etc \ //定义配置文件安装目录
-DMYSQL_DATADIR=/home/mysql/ \ //数据存放位置目录
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock //连接数据库文件
//安装与编译
make && make install
//覆盖原有的主配置文件
cp support-files/my-default.cnf /etc/my.cnf
//复制一份启动脚本文件到service中
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld //添加权限
chkconfig --add /etc/init.d/mysqld //添加,可使用service管理数据库
chkconfig mysqld --level 35 on //添加,开机自启动
//设置环境变量,是系统识别mysql数据库内部命令
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
echo $PATH
//创建管理用户
useradd -s /sbin/nologin mysql
chown -R mysql:mysql /usr/local/mysql/
//初始化数据库
/usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
//建立软连接文件
ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
//指向路径
vim /etc/init.d/mysqld
basedir=/usr/local/mysql //数据库目录
datadir=/home/mysql //数据存放目录
//开启服务
service mysqld start
netstat -anpt | grep 3306
#设置数据库管理员密码
mysqladmin -u root -p password
#登陆数据库
mysql -uroot -p
//创建新数据库
create database info;
Query OK, 1 row affected (0.01 sec)
//进入数据库,创建新表的结构
create table yun (
-> id int(4) not null primary key auto_increment,
-> name char(10) not null,
-> age int(3) not null,
-> addr varchar(50) default 'nj');
Query OK, 0 rows affected (0.03 sec)
//查询表的结构
desc yun;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | NULL | |
| age | int(3) | NO | | NULL | |
| addr | varchar(50) | YES | | nj | |
+-------+-------------+------+-----+---------+----------------+
//插入数据
insert into yun (name,age,addr) values ('zhangsan',18,'bj');
Query OK, 1 row affected (0.01 sec)
insert into yun (name,age,addr) values ('lisi',19,'bj');
Query OK, 1 row affected (0.01 sec)
//查看表中的数据
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
+----+----------+-----+------+
MySQL备份思路
定期实施备份,制定备份计划或者策略,并严格遵守
除了进行完全备份,开启MySQL服务器的日志功能是很重要的
物理方式备份
//切换到存放数据目录
/home/mysql/info
//备份数据
tar jcvf /opt/mysql-$(date +%F).tar.xz /home/mysql/info/
tar: 从成员名中删除开头的“/”
/home/mysql/info/
/home/mysql/info/db.opt
/home/mysql/info/yun.frm
/home/mysql/info/yun.ibd
//查看备份数据的压缩包
cd /opt/
ls
mysql-2020-01-07.tar.xz
逻辑方式备份
//使用命令进行逻辑备份
mysqldump -u root -p info >/opt/info.sql
Enter password:
//查看备份文件
cd /opt/
ls
info.sql //脚本文件
//多个数据库进行逻辑备份
mysqldump -u root -pabc123 --databases mysql yun >/opt/db_mysql_yun.sql
//对数据库所有数据进行逻辑备份
mysqldump -u root -pabc123 --opt --all-databases >/opt/all.sql
//对某个数据库中特定的表进行备份
mysqldump -u root -pabc123 info yun >/opt/info_yun.sql
//对某个数据库中特定表的结构进行备份
mysqldump -u root -pabc123 -d info yun >/opt/info_yun_desc.sql
使用脚本进行备份恢复
//进入数据库
show tables;
+----------------+
| Tables_in_info |
+----------------+
| yun |
+----------------+
1 row in set (0.00 sec)
//模拟删除yun这张表
drop table yun;
Query OK, 0 rows affected (0.04 sec)
//查看是否删除
show tables;
Empty set (0.00 sec)
//使用命令恢复yun这张表
source /opt/info.sql
//查看yun这张表中的数据
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
+----+----------+-----+------+
2 rows in set (0.00 sec)
//删除yun这张表
drop table yun;
Query OK, 0 rows affected (0.00 sec)
//删除成功
show tables;
Empty set (0.00 sec)
//使用命令恢复
mysql -u root -pabc123 info < /opt/info.sql
//进入数据库查看是否恢复成功
mysql -uroot -p
Enter password: //输入密码进入
use info; //进入此数据库
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
+----+----------+-----+------+
2 rows in set (0.00 sec)
//开启二进制日志文件功能
vim /etc/my.cnf
log-bin=mysql-bin
//重启mysql服务
service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
//查看二进制日志文件
cd /home/mysql/
ls
mysql-bin.000001 //日志文件
//先执行一次完整型备份
mysqldump -u root -pabc123 info > /opt/info.sql
ls /opt/
info.sql
//进行增量备份,生成增量备份二进制文件
mysqladmin -u root -pabc123 flush-logs
mysql-bin.000002
//进入数据库,查看表的数据
mysql -u root -pabc123
use info;
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
+----+----------+-----+------+
//插入新的数据,并模拟误操作
insert into yun (name,age,addr) values ('wangwu',23,default); //正常插入数据
Query OK, 1 row affected (0.00 sec)
delete from yun where name='lisi'; //误删除lisi的数据
Query OK, 1 row affected (0.00 sec)
insert into yun (name,age,addr) values ('zhaoliu',33,default);
Query OK, 1 row affected (0.00 sec)
//查看表的数据
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 3 | wangwu | 23 | nj |
| 4 | zhaoliu | 33 | nj |
+----+----------+-----+------+
//进行增量备份,生成新的日志文件
mysqladmin -u root -pabc123 flush-logs
mysql-bin.000003
//将误操作的日志文件mysql-bin.000002中的数据导出,查看该信息
mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000002 >/opt/bak.txt
vim /opt/bak.txt //查找误操作的信息
insert into yun (name,age,addr) values ('wangwu',23,default) //正常操作
delete from yun where name='lisi' //误操作
insert into yun (name,age,addr) values ('zhaoliu',33,default) //正常操作
//删除表
drop table yun;
Query OK, 0 rows affected (0.02 sec)
//查看是否删除
select * from yun;
ERROR 1146 (42S02): Table 'info.yun' doesn't exist
//先进行完全备份恢复
source /opt/info.sql
Query OK, 0 rows affected (0.01 sec)
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
+----+----------+-----+------+
//按照日志文件中的误操作的时间节点进行增量备份恢复,跳节点,不恢复误操作
quit
Bye
mysqlbinlog --no-defaults --stop-datetime='2020-01-07 17:23:42' /home/mysql/mysql-bin.000002 | mysql -u root -p
Enter password: //输入密码
//进入数据库,查看wangwu用户数据是否恢复
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
| 3 | wangwu | 23 | nj |
+----+----------+-----+------+
3 rows in set (0.00 sec)
//按照日志文件的时间节点,恢复zhaoliu用户数据
mysqlbinlog --no-defaults --start-datetime='2020-01-07 17:24:25' /home/mysql/mysql-bin.000002 | mysql -u root -pabc123
//进入数据库查看zhaoliu用户数据是否恢复
mysql> select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
| 3 | wangwu | 23 | nj |
| 4 | zhaoliu | 33 | nj |
+----+----------+-----+------+
4 rows in set (0.00 sec)
//删除表的数据
drop table yun;
Query OK, 0 rows affected (0.00 sec)
select * from yun;
ERROR 1146 (42S02): Table 'info.yun' doesn't exist
//进行完全备份恢复
source /opt/info.sql
Query OK, 0 rows affected (0.00 sec)
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
+----+----------+-----+------+
2 rows in set (0.00 sec)
//查看日志文件,按照atID号进行增量恢复
mysqlbinlog --no-defaults --stop-position='475' /home/mysql/mysql-bin.000002 | mysql -u root -pabc123 //恢复wangwu用户数据
mysqlbinlog --no-defaults --start-position='613' /home/mysql/mysql-bin.000002 | mysql -u root -pabc123 //恢复zhaoliu用户数据
//进入数据库,查看wangwu和zhaoliu用户数据是否恢复
select * from yun;
+----+----------+-----+------+
| id | name | age | addr |
+----+----------+-----+------+
| 1 | zhangsan | 18 | bj |
| 2 | lisi | 19 | bj |
| 3 | wangwu | 23 | nj |
| 4 | zhaoliu | 33 | nj |
+----+----------+-----+------+
4 rows in set (0.00 sec)