Home Database Mysql Tutorial MySQL开源备份工具Xtrabackup备份部署

MySQL开源备份工具Xtrabackup备份部署

Jun 07, 2016 pm 05:29 PM
mysql backup xtrabackup

Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好

  Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品。

  Xtrabackup有两个主要的工具:xtrabackup、innobackupex

  1、xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表

  2、innobackupex是参考了InnoDB Hotbackup的innoback脚本修改而来的.innobackupex是一个perl脚本封装,封装了xtrabackup。主要是为了方便的 同时备份InnoDB和MyISAM引擎的表,但在处理myisam时需要加一个读锁。并且加入了一些使用的选项。如slave-info可以记录备份恢 复后,作为slave需要的一些信息,根据这些信息,可以很方便的利用备份来重做slave。
   
  Xtrabackup下载地址:
 
  选择合适的版本,,我这里选择binary。
 
  解压到/usr/src/目录,解压后目录名percona-xtrabackup-2.1.3,进入到bin目录,即可直接使用innobackupex命令
  # pwd
  /usr/src/percona-xtrabackup-2.1.3/bin
  # ls -l
  total 112396
  -rwxr-xr-x 1 root root  110738 Jun  7 11:43 innobackupex
  -rwxr-xr-x 1 root root  110738 Jun  7 11:43 innobackupex-1.5.1
  -rwxr-xr-x 1 root root  2211237 Jun  7 11:43 xbcrypt
  -rwxr-xr-x 1 root root  2285672 Jun  7 11:43 xbstream
  -rwxr-xr-x 1 root root 13033745 Jun  7 11:43 xtrabackup
  -rwxr-xr-x 1 root root 16333506 Jun  7 11:43 xtrabackup_55
  -rwxr-xr-x 1 root root 80988093 Jun  7 11:43 xtrabackup_56
 
  在开始使用的时候可能会报如下错误:
  sh: xtrabackup_55: command not found
  innobackupex: fatal error: no 'mysqld' group in MySQL options 

  解决办法xtrabackup_55复制到/usr/bin下即可。

  # cp /usr/src/percona-xtrabackup-2.1.3/bin/xtrabackup_55 /usr/bin/
 
 
  建立mysql备份用户:
  mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backupuser'@'localhost' identified by 'skEBfef5E2';
  mysql> FLUSH PRIVILEGES;
   
备份脚本内容(每周一次全备,六次增量备份,保留2周的备份,并压缩):

#!/bin/bash
BEGINTIME=`date +"%Y-%m-%d %H:%M:%S"`
format_time=`date +"%Y-%m-%d_%H:%M:%S"`
week=`date +%w`
backupbin=/usr/src/percona-xtrabackup-2.1.3/bin
backdir=/data/databasebak/bak
file_cnf=/etc/my.cnf
user_name=backupuser
password="skEBfef5E2"
db="db1 db2 db3 db4"
out_log=$backdir/xtrabackup_log_$format_time
time_cost=$backdir/xtrabackup_time.txt

if [ -f "$backdir.lastlastlastweek.gz" ];then
        rm -rf $backdir.lastlastweek.gz
        mv $backdir.lastweek.gz $backdir.lastlastweek.gz
fi

if [ -d "$backdir/rec5" ];then
        gzip -cr $backdir >$backdir.lastweek.gz
        rm -rf $backdir         
        mkdir $backdir
fi
 
#full
if [ ! -d "$backdir/full" ];then
        echo "#####start full backup at $BEGINTIME to directory full" >>$time_cost
        $backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" $backdir/full 1> $out_log 2>&1
        break;
elif [ ! -d "$backdir/rec0" ];then
        echo "#####start 0 incremental backup at $BEGINTIME to directory rec0" >>$time_cost
        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/full $backdir/rec0 1> $out_log 2>&1
        break;
elif [ ! -d "$backdir/rec1" ];then
        echo "#####start 1 incremental backup at $BEGINTIME to directory rec1" >>$time_cost
        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec0 $backdir/rec1 1> $out_log 2>&1
        break;
elif [ ! -d "$backdir/rec2" ];then
        echo "#####start 2 incremental backup at $BEGINTIME to directory rec2" >>$time_cost
        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec1 $backdir/rec2 1> $out_log 2>&1
        break;
elif [ ! -d "$backdir/rec3" ];then
        echo "#####start 3 incremental backup at $BEGINTIME to directory rec3" >>$time_cost
        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec2 $backdir/rec3 1> $out_log 2>&1
        break;
elif [ ! -d "$backdir/rec4" ];then
        echo "#####start 4 incremental backup at $BEGINTIME to directory rec4" >>$time_cost
        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec3 $backdir/rec4 1> $out_log 2>&1
        break;
elif [ ! -d "$backdir/rec5" ];then
        echo "#####start 5 incremental backup at $BEGINTIME to directory rec5" >>$time_cost
        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info --databases="$db" --incremental --incremental-basedir=$backdir/rec4 $backdir/rec5 1> $out_log 2>&1
        break;
 fi
 ENDTIME=`date +"%Y-%m-%d %H:%M:%S"`
 begin_data=`date -d  "$BEGINTIME" +%s`
 end_data=`date -d  "$ENDTIME" +%s`
 spendtime=`expr $end_data - $begin_data`
 echo "it takes $spendtime sec for packing the data directory" >>$time_cost

部署备份脚本;

# crontab -l

10 2 * * *  /opt/cron/mysqldbbakup.sh

恢复:

#全备恢复:

innobackupex --apply-log --redo-only  --defaults-file=$file_cnf --user=$user_name --password=$password    $backdir/full

#增备恢复,修改增量备份目录,逐个apply-log,到第五天的增量rec4都是输出都显示innobackupex: completed OK!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Xtrabackup for incremental backup of mysql How to use Xtrabackup for incremental backup of mysql May 30, 2023 pm 02:50 PM

MySQL incremental backup using Inside, there is only one binary. In addition, for the sake of compatibility, innobackupex is used as a soft link of xtrabackup. That is, xtrabackup now supports non-Innodb table backup, and Innobackupex will be removed in the next version (8.0 has been removed). It is recommended to use xtraba

Multiple backup solutions for MySql: How to efficiently create and restore MySQL backups Multiple backup solutions for MySql: How to efficiently create and restore MySQL backups Jun 15, 2023 pm 03:28 PM

MySql is a commonly used relational database management system that is widely used in various business and application scenarios. For MySQL backup issues, the selection and execution method of the backup plan are crucial. In this article, we will introduce various backup options and how to create and restore MySQL backups efficiently. 1. Selection of backup plan In the process of selecting a MySQL backup plan, you should choose a backup plan that suits you based on the business scenario and actual situation. Cold backup The so-called cold backup is to complete the MySQL database.

How to use MySQL data backup and recovery tools for disaster recovery How to use MySQL data backup and recovery tools for disaster recovery Aug 02, 2023 am 09:06 AM

How to use MySQL data backup and recovery tools to achieve disaster recovery. Data backup and recovery are a very important part of the database management process. Backing up your data protects your database from accidental corruption, hardware failure, or other catastrophic events. As a popular relational database management system, MySQL provides some powerful tools to achieve data backup and recovery. This article will introduce how to use MySQL's data backup and recovery tools to achieve disaster recovery. MySQL data backup tool-mysql

How to effectively manage and maintain ibd files in MySQL database How to effectively manage and maintain ibd files in MySQL database Mar 16, 2024 am 11:21 AM

In the MySQL database, each InnoDB table corresponds to an .ibd file, which stores the table's data and indexes. Therefore, for the management and maintenance of MySQL database, the management of ibd files is also particularly important. This article will introduce how to effectively manage and maintain ibd files in a MySQL database and provide specific code examples. 1. Check and optimize table space First, we can check the disk space usage of the table using the following SQL statement: SELECTTAB

MySQL rolling backup techniques for data MySQL rolling backup techniques for data Jun 15, 2023 pm 07:47 PM

MySQL is a popular relational database that is widely used in various fields. However, like other applications, MySQL has risks such as data corruption, crashes, and malicious attacks. Therefore, backing up your data is crucial. Backups can provide security and some form of "undo" functionality to data, reducing or even eliminating instability and risk. The most common backup types are full backup and incremental backup. However, if you need frequent, real-time backups, rolling backups are a better approach. A rolling backup is when an acceptable

MySql database backup: How to achieve efficient MySQL database backup and recovery MySql database backup: How to achieve efficient MySQL database backup and recovery Jun 15, 2023 pm 11:37 PM

MySQL is one of the most widely used relational database management systems currently. Its efficiency and reliability make it the first choice for many enterprises and developers. But for various reasons, we need to back up the MySQL database. Backing up a MySQL database is not an easy task because once the backup fails, important data may be lost. Therefore, in order to ensure data integrity and recoverability, some measures must be taken to achieve efficient MySQL database backup and recovery. This article will introduce how to achieve

Analysis of project experience on MySQL database backup and recovery performance tuning Analysis of project experience on MySQL database backup and recovery performance tuning Nov 04, 2023 am 09:46 AM

Analysis of MySQL database backup and recovery performance tuning project experience In daily operation and maintenance, MySQL database backup and recovery work is indispensable. However, in the face of multi-terabyte or even petabyte-level data scale, the time and resource consumption required for backup and recovery often become key factors restricting database performance. This article will share some practical experiences and techniques through a practical case of backup and recovery performance tuning for a large Internet enterprise. 1. Selection of backup solutions. Based on different business needs and data scale, the selection of backup solutions should also be considered.

How to backup and restore database after mysql installation How to backup and restore database after mysql installation Apr 08, 2025 am 11:45 AM

There is no absolutely optimal MySQL database backup and recovery solution, and it needs to be selected based on the amount of data, business importance, RTO and RPO. 1. Logical backup (mysqldump) is simple and easy to use, suitable for small databases, but slow and huge files; 2. Physical backup (xtrabackup) is fast, suitable for large databases, but is more complicated to use. The backup strategy needs to consider the backup frequency (RPO decision), backup method (data quantity and time requirement decision) and storage location (off-site storage is more secure), and regularly test the backup and recovery process to avoid backup file corruption, permission problems, insufficient storage space, network interruption and untested issues, and ensure data security.

See all articles