search
HomeDatabaseMysql TutorialMySQL复制+快照恢复误删除操作实验测试_MySQL

bitsCN.com

MySQL复制+快照恢复误删除操作实验测试

 

下面假定2个场景:

 

场景1:主从架构,没有延迟,某DBA误操作:drop database 【复制+快照:在线备份】

场景2:存在不确定性或者风险性较大的操作,如升级测试,大表变更【啥事都在快照上折腾,不行大不了就将之删除】

 

场景2比较简单,随便在新建的一个快照上折腾,搞砸就把快照删除,再新建一个,2个字:随便玩

下面我们对场景1进行模拟

 

恢复方法:

① 恢复备库上的快照

② 根据binlog执行point-in-time恢复

 

先为备库创建快照

 

[root@localhost ~]# vgs  VG   #PV #LV #SN Attr   VSize VFree  vg     4   1   0 wz--n- 3.81G 1.81G[root@localhost ~]# lvs  LV    VG   Attr   LSize Origin Snap%  Move Log Copy%  Convert  mysql vg   -wi-ao 2.00G      [root@localhost ~]# lvcreate --size 1G --snapshot --name backup_mysql /dev/vg/mysql  Logical volume "backup_mysql" created[root@localhost ~]# lvs  LV           VG   Attr   LSize Origin Snap%  Move Log Copy%  Convert  backup_mysql vg   swi-a- 1.00G mysql    0.00                          mysql        vg   owi-ao 2.00G      [root@localhost ~]# mount /dev/vg/backup_mysql  /mnt/backup[root@localhost ~]# cd /mnt/backup/[root@localhost backup]# lslost+found  mysql[root@localhost backup]# tar -jcv -f /mnt/snapshot/mysql.tar.bz2 *[root@localhost ~]# lvremove --force /dev/vg/backup_mysql   Logical volume "backup_mysql" successfully removed

 

 

 

这里为什么要先备份快照再还原呢?

其一,昂贵的IO,因为磁头要在快照区和系统区来回跑

其二,快照区空间不足,因为是COW原理

 

在 2013-10-12 9:57 某位无经验DBA错误地执行了drop database snapshots:

 

在备库上确认查看:

 

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || test               |+--------------------+

 

可见库已经被删除了

 

同时在主库或者备库查看当前的二进制日志坐标并记录下来

[mysql@localhost mysql]$ mysqladmin -uroot -poracle shutdown131012 09:59:36 mysqld_safe mysqld from pid file /mnt/lvm/mysql/data/localhost.localdomain.pid ended[1]+  Done                    mysqld_safe[root@localhost ~]# umount /mnt/backup[root@localhost ~]# lvremove --force /dev/vg/backup_mysql   Logical volume "backup_mysql" successfully removed[root@localhost ~]# umount /mnt/lvm                                 [root@localhost ~]# mkfs -t ext3 /dev/vg/mysql[root@localhost ~]# mount /dev/vg/mysql /mnt/lvm[root@localhost ~]# tar -jxv -f /mnt/snapshot/mysql.tar.bz2  -C /mnt/lvm[mysql@localhost ~]$ mysqld_safe &

 

 

通过binlog执行point-in-time恢复 

 

[mysql@localhost ~]$ mysqlbinlog --stop-datetime="2013-10-12 10:9:56" /mnt/lvm/mysql/data/mysql-bin.000008 | mysql -uroot -poracle

 

 

确认数据是否恢复:

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || snapshots          || test               |+--------------------+4 rows in set (0.00 sec)mysql> use snapshots;Database changedmysql> show tables;+---------------------+| Tables_in_snapshots |+---------------------+| t                   |+---------------------+1 row in set (0.00 sec)mysql> select * from t;+----+| id |+----+|  1 |+----+1 row in set (0.00 sec)

 

 

到此,整个简单的测试就算完成了

这种复制+快照的备份架构可以实现在线实时的备份,个人感觉是个不错的备份解决方案

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.