备份还原案例
前提:log_bin=ON
一、早上上班后误删数据库,如何恢复数据库
1、假设早上已经完成备份 (开启二进制),然后在students表里新增了一条数据
[[email protected] ~]#mysqldump -A -F --single-transaction --master-data=2 > /backup/full.sqlMariaDB [hellodb]> insert students values (26,‘laobai‘,30,‘M‘,7,2);
2、然后在10点时,误删数据库 ;用户在访问 students表 的时候 提示错误,表找不到,然后其他用户又新增一个表
MariaDB [hellodb]> drop table students;
MariaDB [hellodb]> insert teachers values(5,‘mht‘,‘46‘,‘M‘);
MariaDB [hellodb]> select * from students; ERROR 1146 (42S02): Table ‘hellodb.students‘ doesn‘t exist
3、为了防止问题继续扩大,我们先上个只读锁,禁止用户修改。
MariaDB [(none)]> flush table with read lock;
4、我们先查看此时的日志文件
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 30343 |
| mysql-bin.000002 | 1038814 |
| mysql-bin.000003 | 7698 |
| mysql-bin.000004 | 773 |
+------------------+-----------+
5、为了区分新旧日志,我们刷新一下日志
MariaDB [(none)]> flush logs;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 30343 |
| mysql-bin.000002 | 1038814 |
| mysql-bin.000003 | 7698 |
| mysql-bin.000004 | 816 |
6、我们查看一下二进制文件,确认是第四个文件且编号为245的文件 ,然后将文件导入至backup目录下
[[email protected] ~]#less /backup/full.sql -- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000004‘, MASTER_LOG_POS=245;
[[email protected] ~]#mysqlbinlog --start-position=245 /date/binlog/mysql-bin.000004 > /backup/bin.sql
7、上部操作是将全部备份 加上 出故障之间的备份文件导了出来,我们还需要找到删除表的记录并删除,然后利用二进制文件进行恢复 ,发现表已经全部恢复。
[[email protected] backup]#vim /backup/bin.sql #将删除表的指令删除 [[email protected] backup]#rm -rf /date/mysql/* #删库(删之前一定要确认有备份) [[email protected] backup]#systemctl restart mariadb [[email protected] backup]#mysql < /backup/full.sql #将全备份还原 [[email protected] backup]#mysql < /backup/bin.sql #将修改过的文件还原 [[email protected] backup]#mysql Database changed MariaDB [hellodb]> select * from teachers -> ; +-----+---------------+-----+--------+ | TID | Name | Age | Gender | +-----+---------------+-----+--------+ | 1 | Song Jiang | 45 | M | | 2 | Zhang Sanfeng | 94 | M | | 3 | Miejue Shitai | 77 | F | | 4 | Lin Chaoying | 93 | F | | 5 | mht | 46 | M | +-----+---------------+-----+--------+ 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 | | 26 | laobai | 30 | M | 7 | 2 | +-------+---------------+-----+--------+---------+-----------+
原文地址:https://www.cnblogs.com/huxiaojun/p/9193806.html