search
HomeDatabaseMysql Tutorial 生产环境究竟是使用mysqldump还是xtrabackup来备份与恢复数据库?

一个合格的运维工程师或者dba工程师,如果有从事数据库方面的话,首先需要做的就是备份,如果没有备份,出现问题的话,你的业务就会出问题,你的工作甚至会。。

一个合格的运维工程师或者dba工程师,如果有从事数据库方面的话,首先需要做的就是备份,如果没有备份,出现问题的话,你的业务就会出问题,香港服务器,你的工作甚至会。。。

所以备份是重要的,但光有备份还不行,备份后如果出现问题,你还得使用备份数据来恢复,但恢复数据的时间一般都是很长的,不符合业务需求,所以一个快速备份与恢复的软件就很有必要。

之前我在维护mysql数据库的时候,使用mysqldump来进行备份与恢复,在备份的时候锁住表,然后全部备份,在数据少的时候没问题,但如果数据很多,不允许锁表,同时需要恢复数据块的情况,mysqldump就不适合了,我在恢复一个4G数据文件的数据库的时候,恢复的数据是使用mysqldump的数据,恢复了3个小时还没有反应,香港服务器,造成的影响很严重,所以我开始寻找其他的别发软件来满足以上的需求,幸好找到了,就是使用xtrabackup来进行备份与恢复,恢复4G数据文件的数据库,仅需要14秒,同时在备份的时候不会锁表,而且支持增量备份,香港虚拟主机,所以把我的比较分享给大家,希望对大家有益!

Xtrabackup 是percona公司的开源项目,用以实现类似innodb官方的热备份工具InnoDB Hot Backup的功能,能够非常快速地备份与恢复mysql数据库。 Xtrabackup中包含两个工具:

xtrabackup是用于热备份innodb, xtradb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构;

innobackupex是将xtrabackup进行封装的perl脚本,提供了备份myisam表的能力。

由于innobackupex的功能更为全面和完善,所以,本文以innobackupex作为基础进行研究描述。

下面介绍xtrabackup的全部、增量的备份与恢复。

一、下载与安装

1、下载

  • wget  
  • 2、安装依赖库

    如果是debian系列的话

  • apt-get install debhelper autotools-dev libaio-dev wget automake   libtool bison libncurses-dev libz-dev cmake bzr 
  • 如果是redhat系列的话

  • yum install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr   bison libtool ncurses-devel zlib-devel 
  • 3、解压

  • tar zxvf xtrabackup-1.6.7.tar.gz  
  • 4、进入目录

  • cd xtrabackup-1.6.7 
  • 5、复制

    其中,

    innobackupex是我们要使用的备份工具;

    xtrabackup是被封装在innobackupex之中的,innobackupex运行时需要调用它;

    xtrabackup_51是xtrabackup运行时需要调用的工具;

    tar4ibd是以tar流的形式产生备份时用来打包的工具。

    6、对某个数据库进行全部备份的命令介绍

  • 说明:
          --database=test 单独对test数据库做备份 ,若是不添加此参数那就那就是对全库做备份
          2>/tmp/data/err.log 输出信息写入日志中
          1>/tmp/data/test.tar.gz 打包压缩存储到该文件中

     

    二、对数据库的全部备份与恢复

    下面开始测试xtrabackup的全部备份

    (1)先进入mysql里创建一个新的test数据库

     

    (2)然后备份test的整个数据库

    使用下面的backup.sh脚本

    现在开始运行此脚本

    然后到data里查看结果

    可以看到备份完成了

    (3)恢复数据库

    先关闭mysql服务,然后再删除test数据库文件

    开始恢复数据库

    先把之前/tmp/data里的数据库压缩版给解压

    注意:这里tar解包必须使用-i参数,否则解压出来的文件只有一个backup-my.cnf

    查看一下restore的内容

    然后将备份文件中的日志应用到备份文件中的数据文件上

    这里的--apply-log指明是将日志应用到数据文件上,完成之后将备份文件中的数据恢复到数据库中:

    然后再查看一下当前目录内容

    然后再修改test的用户与组为mysql

    然后启动mysql,并查看test数据库的表里内容

    可以看到数据库已经恢复完成

    可能大家有个疑问,为什么我这里不像很多网上的文章里是在apply-log后,使用copy-back如果使用/usr/bin/innobackupex --copy-back命令后,会报Original data directory is not empty! at /usr/local/xtrabackup/bin/innobackupex line 538.恢复的目录必须为空。经查官网,这是xtrabackup的一个BUG。
    innobackupex–copy-back was run. With this bug fix, innobackupex–copy-back operation if the destination is not empty, avoiding potential data loss or a strang combination of a restored backup and previous data. Bug Fixed: #737569 (Valentine Gostev) will now error out of the did not check that MySQL datadir was empty before

    所以在apply-log后直接复制数据目录到数据库的位置上吧。

    三、对数据库的增量备份与恢复

    为了进行增量备份,先对数据库添加一些数据

    然后进行增量的备份

     

    其中,--incremental指明是增量备份,--incremental-basedir指定上次完整备份或者增量备份文件的位置。这里的增量备份其实只针对的是InnoDB,对于MyISAM来说,还是完整备份。

    在进行增量备份的恢复之前,先关闭数据库,然后删除数据库test

    增量备份的恢复

    然后再进入恢复的目录/tmp/data

    跟全部备份一样,把test恢复到/var/lib/mysql里

    然后修改用户与组

    然后启动msyql,并查看test数据库里test表的内容

    可以看到增量备份已经恢复完成。

     下面的链接是我在生产环境适应xtrabackup来恢复全部备份的数据库的案例

    本文出自 “吟—技术交流” 博客,请务必保留此出处

  • 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

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools