Three types of Mysql backup

Source: Internet
Author: User
Tags mysql backup mysql login

I. Purpose of backup

Disaster recovery: recover and restore damaged data
Demand change: data needs to be restored to a time before the change due to demand changes
Test: test whether the new function is available

Ii. Backup considerations

How long data can be lost;
How long will the data be restored;
Whether to provide continuous services during recovery;
The recovered object is the entire database, multiple tables, or a single database and a single table.

Iii. Backup Type

1. Database offline as required

Cold Standby (cold backup): mysql service needs to be closed and read/write requests are not allowed;
Warm backup: the service is online, but only supports read requests. Write requests are not allowed;
Hot backup: services are not affected when backup is performed.

Note:

1. This type of backup depends on business needs, rather than backup tools.
2. MyISAM does not support hot backup. InnoDB supports hot backup, but specialized tools are required.

2. Based on the range of the data set to be backed up
Full backup: full backup backs up all character sets.
Incremental backup: The data changed since the last full backup or incremental backup cannot be used independently. To use full backup, the backup frequency depends on the data update frequency.
Differential backup: The data changed since the last full backup of differential backup.
Recommended recovery policy:
Full + incremental + binary log
Full + difference + binary log

3. Backup Data or files

Physical backup: directly back up data files

Advantages:

Backup and recovery operations are relatively simple and can be performed across mysql versions,
Fast Recovery, which belongs to the file system level

Suggestion:

Do not test if the backup is available.
Mysql> check tables; check whether the table is available
Logical backup: Data and code in the backup table

Advantages:

Simple recovery,
The backup result is an ASCII file and can be edited.
Independent from the storage engine
Network backup and recovery are supported.

Disadvantages:

The mysql server process is required for backup or recovery.
The backup results occupy more space,
Floating Point Numbers may lose precision.
After restoration, the epitome needs to be rebuilt.

Iv. Backup objects

1. Data;
2. configuration file;
3. Code: stored procedures, stored functions, and triggers
4. OS-related configuration files
5. Copy related configurations
6. Binary logs

V. Implementation of backup and recovery

1. Use select into outfile to back up and restore Data
1.1 back up the data to be backed up


Copy the Code as follows:
Mysql> use hellodb; // open the hellodb Database
Mysql> select * from students; view the attributes of students
Mysql> select * from students where Age> 30 into outfile '/tmp/stud.txt'; // back up the information of students older than 30


Note:

The directory path of the backup must allow the user currently running the mysql server to access mysql.

After the backup is complete, copy the backup file from the tmp directory, or you will lose the backup purpose.

Go back to the tmp directory to view the backup file.

[Root @ www ~] # Cd/tmp

[Root @ www tmp] # cat stud.txt

3Xie Yanke53M216

4 Ding Dian32M44

6Shi Qing46M5 \ N

13 Tian Boguang33M2 \ N

25Sun Dasheng100M \ N

[Root @ www tmp] #

You will find a text file. Therefore, the database cannot be imported directly. Use load data infile to restore data

Return to the mysql server, delete users older than 30, and the simulation data is damaged.

Mysql> delete from students where Age> 30;

Mysql> load data infile '/tmp/stud.txt' into table students;

2. Use mysqldump to back up and restore Data

Mysqldump is often used for warm backup. Therefore, we need to apply a read lock to the data to be backed up first,

2.1 method of applying a read lock:

1. Add options directly during Backup

-- Lock-all-tables: Apply a read lock to all tables in the database to be backed up.

-- Lock-table only applies a read lock to a single table. Even if it backs up the entire database, it also applies a read lock to the table when we back up a table. Therefore, it applies to backing up a single table.

2. Write commands on the server,

Mysql> flush tables with read lock; Apply a lock to synchronize all tables in the memory to the disk, and then apply a read lock.

Mysql> flush tables with read lock; release the read lock

But for the InnoDB Storage engine, although you can also request the channel read lock, it does not mean that all its data has been synchronized to the disk, so when facing InnoDB, we need to use mysql> show engine innodb status; Check that all InnoDB data has been synchronized to the disk before performing the backup operation.

2.2 backup policy:

Full backup + Incremental backup + binary log

Demonstrate the backup process;

2.3 make a full backup of the database first:


Copy the Code as follows:
[Root @ www ~] # Mysqldump-uroot -- single-transaction -- master-data = 2 -- databases hellodb>/backup/hellodb _ 'date + % F'. SQL

-- Single-transaction: This option enables hot backup of InnoDB tables. Therefore, you do not need to use -- lock-all-tables at the same time;
-- Master-data = 2 record the location of the binary log at the time of backup, and comment it out. 1 is not commented out.
-- Databases hellodb specifies the database to be backed up

Then return to the mysql server,

2.4 return to the mysql server to update data


Copy the Code as follows:
Mysql> create table tb1 (id int); create a table
Mysql> insert into tb1 values (1), (2), (3); insert data. This example only shows how to insert data.


2.5 first view the location recorded in the full backup file:

 


Copy the Code as follows: [root @ www backup] # cat hellodb_2013-09-08. SQL | less
-- Change master to MASTER_LOG_FILE = 'mysql-bin.000013 ', MASTER_LOG_POS = 15684; the location of the binary log is recorded.


2.6 return to the server:


Copy the Code as follows:
Mysql> show master status; displays the location of the binary log at this time
From the location recorded in the backup file to our location at this time, that is, the incremental part
+ ------------------ + ---------- + -------------- + ------------------ +
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+ ------------------ + ---------- + -------------- + ------------------ +
| Mysql-bin.000004 | 15982 |
+ ------------------ + ---------- + -------------- + ------------------ +


2.7 perform Incremental Backup


Copy the Code as follows:
[Root @ www backup] # mysqlbinlog -- start-position = 15694 -- stop-position = 15982
/Mydata/data/mysql-bin.000013>/backup/hellodb _ 'date + $ F _ % H'. SQL


2.8 return to the server


Copy the Code as follows:
Mysql> insert into tb1 values (4), (5); insert some values
Mysql> drop database hellodb; Delete hellodb database


2.9 export this binary log:

 


Copy the Code as follows:
[Root @ www backup] # mysqlbinlog -- start-position = 15982/mydata/data/mysql-bin.000013 to view the location of the binary log during the delete operation
[Root @ www backup] # mysqlbinlog -- start-position = 15982 -- stop-position = 16176/mydata/data/mysql-bin.000013>/tmp/hellodb. SQL // export binary logs


2.10 Let mysql go offline first

Return to the server:


Copy the Code as follows:
Mysql> set SQL _log_bin = 0; Disable binary logs
Mysql> flush logs; scroll down logs


2.11 simulate database corruption


Copy the Code as follows: mysql> drop database hellodb;

2.12 start data recovery:


Copy the Code as follows:
[Root @ www] # mysql </backup/hellodb_2013-09-08. SQL // import a full backup file
[Root @ www] # mysql </backup/hellodb_2013-09-08_05. SQL // import Incremental backup File
[Root @ www] # mysql


The verification is complete and the result is shown as we expected

Note:

1. In the production environment, we should export data from the entire mysql server instead of a single database. Therefore, we should use -- all-databases
2. When exporting binary logs, you can directly copy the files, but note that the logs are rolled down before backup.
3. Use lvm snapshots for almost hot backup data backup and recovery

3.1 policy:

Full backup + binary log;

3.2 preparation:

Note: The transaction log must be in the same LV as the data file;

3.3 create lvm Lvm creation here is not much said, would like to know click http://www.jb51.net/LINUXjishu/105937.html

3.4 modify the permissions of the files in the mysql main configuration file storage directory and the owner group, and initialize mysql


Copy the Code as follows:
[Root @ www ~] # Mkdir/mydata/data // create a data directory
[Root @ www ~] # Chown mysql: mysql/mydata/data // change owner of the Group
[Root @ www ~] #
[Root @ www ~] # Cd/usr/local/mysql // must be in this directory
[Root @ www mysql] # scripts/mysql_install_db -- user = mysql -- datadir =/mydata/data // initialize mysql


3.5 modify the configuration file:


Copy the Code as follows:
Vim/etc/my. cof
Datadir =/mydata/data add data directory
Sync_binlog = 1 enable this function


3.6 start the service


Copy the Code as follows:
[Root @ www mysql] # service mysqld start
Mysql> set session SQL _log_bin = 0; Disable binary logs
Mysql> source/backup/all_db_2013-09-08. SQL read backup files


3.7 return to the mysql server:


Copy the Code as follows:
Mysql> flush tables with read lock; read lock request
Note: Do not exit. Start another terminal:
Mysql> show master status; view the location of the binary file
+ ------------------ + ---------- + -------------- + ------------------ +
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+ ------------------ + ---------- + -------------- + ------------------ +
| Mysql-bin.000004 | 107 |
+ ------------------ + ---------- + -------------- + ------------------ +
1 row in set (0.00 sec)
Mysql> flush logs; it is recommended to scroll down the log. This makes it easy to back up logs.


3.8 export binary files and create directories for separate storage


Copy the Code as follows:
[Root @ www ~] # Mkdir/backup/limian
[Root @ www ~] # Mysql-e 'show master status; '>/backup/limian/binlog.txt
[Root @ www ~] #


3.9 create a snapshot for the volume where the data is located:


Copy the Code as follows: [root @ www ~] # Lvcreate-L 100 M-s-p r-n mysql_snap/dev/myvg/mydata

Return to the server and release the read lock.


Copy the Code as follows:
Mysql> unlock tables;
[Root @ www ~] # Mount/dev/myvg/mysql_snap/mnt/data
[Root @ www data] # cp */backup/limian/
[Root @ www data] # lvremove/dev/myvg/mylv_snap


3.10 update the database data and delete the first data file in the data directory to simulate database corruption


Copy the Code as follows:
Mysql> create table limiantb (id int, name CHAR (10 ));
Mysql> insert into limiantb values (1, 'Tom ');
[Root @ www data] # mysqlbinlog -- start-position = 187 mysql-bin.000003>/backup/limian/binlog. SQL
[Root @ www backup] # cd/mydata/data/
[Root @ www data] # rm-rf *
[Root @ www ~] # Cp-a/backup/limian/*/mydata/data/
[Root @ www data] # chown mysql: mysql *


3.11 Test

Start the service


Copy the Code as follows:
[Root @ www data] # service mysqld start
[Root @ www data] # mysql login Test
Mysql> show databases;
Mysql> SET SQL _log_bin = 0
Mysql> source/backup/limian/binlog. SQL; # binary recovery
Mysql> show tables; # view recovery results
Mysql> SET SQL _log_bin = 1; # enable binary log


Note: This method is similar to hot backup to back up data files, and the data files can be stored in lvm to flexibly change the lvm Size Based on the Data size. The backup method is also very simple.

4. Xtrabackup-based Backup Recovery

Official site: www.percona.com

Advantages:

1. fast and reliable full backup
2. transactions are not affected during the backup process.
3. Supports data stream, network transmission, and compression, so it can effectively save disk resources and network bandwidth.
4. Data availability can be automatically verified by backup.

Install Xtrabackup


Copy the Code as follows:
[Root @ www ~] # Rpm-ivh percona-xtrabackup-2.1.4-656.rhel6.i686.rpm


Its latest software available from http://www.percona.com/software/percona-xtrabackup/

Note: We should have the permission to back up the database, but note that we should grant the minimum permission to the user during database backup to ensure security,

4.1 prerequisites:

It should be determined that one tablespace is used for a single table; otherwise, backup and recovery for a single table are not supported.
Add the mysqld section in the configuration file

Innodb_file_per_table = 1

4.2 backup policy
Full backup + Incremental backup + binary log
4.3 prepare directories for storing backup data


Copy the Code as follows:
[Root @ www ~] # Makdir/innobackup


4.4 complete backup:


Copy the Code as follows:
[Root @ www ~] # Innobackupex -- user = root -- password = mypass/innobackup/


Note:

1. As long as innobackupex: completed OK is displayed in the last line !, It indicates that your backup is correct.
2. Note that after each backup, a directory named after the current time point is automatically created under the Data Directory to store the backup data. Let's see what is there.

[Root @ www 2013-09-12_11-04 4] # ls
The backup-my.cnf ibdata1 performance_schema xtrabackup_binary xtrabackup_checkpoints.
Hellodb mysql test xtrabackup_binlog_info xtrabackup_logfile
[Root @ www 2013-09-12_11-04 4] #
Xtrabackup_checkpoints: Backup Type, backup status, And LSN (log serial number) range information;
Xtrabackup_binlog_info: the binary log file currently in use by the mysql server and the location of the binary log event until the moment of backup.
Xtrabackup_logfile: non-text file, xtrabackup your own log file
Xtrabackup_binlog_pos_innodb: the current position of the binary log file used for InnoDB or XtraDB tables.
Backup-my.cnf: Configure mysqld in the data file during Backup

4.5 return to the mysql server to update the data


Copy the Code as follows:
Mysql> use hellodb;
Mysql> delete from students where StuID> = 24;


4.6 Incremental Backup


Copy the Code as follows:
Innobackupex -- user = root -- password = mypass -- incremental/innobackup/-- incremental-basedir =/innobackup/2013-09-12_11-03-04/
-- Incremental specifies the Backup Type
-- Incremental-basedir = specifies the Backup Based on which incremental backup is made. Here it is a full backup file, which can merge incremental backup data into a full backup.


4.7 second Increment

Modify data first


Copy the Code as follows:
Mysql> insert into students (Name, Age, Gender, ClassID, TeacherID) values ('Tom ', 33, 'M', 2, 4 );
Innobackupex -- user = root -- password = mypass -- incremental/innobackup/-- incremental-basedir =/innobackup/2013-09-12_11-37-01/
Here, you only need to change the last directory to the data directory of the first Incremental backup.


4.8 last data change without Incremental Backup


Copy the Code as follows: mysql> delete from coc where id = 14;

4.9 back up the binary log file (because Incremental backup is not performed for the last modification, the binary log must be relied on for time point recovery)


Copy the Code as follows: [root @ www data] # cp mysql-bin.000003/tmp/

4.10 simulate database crash


Copy the Code as follows:
[Root @ www data] # service mysqld stop
[Root @ www data] # rm-rf *


Preparations before recovery

4.11 synchronize full backup data


Copy the Code as follows: [root @ www ~] # Innobackupex -- apply-log -- redo-only/innobackup/2013-09-12_11-03-04/

4.12 perform data synchronization for the first increment


Copy the Code as follows:
Innobackupex -- apply-log -- redo-only/innobackup/2013-09-12_11-03-04/-- incremental-basedir =/innobackup/2013-09-12_11-37-01/


4.13 perform data synchronization for the second Increment


Copy the Code as follows:
Innobackupex -- apply-log -- redo-only/innobackup/2013-09-12_11-03-04/-- incremental-basedir =/innobackup/2013-09-12_11-45-53/
-- Apply-log: cancels a transaction that has not been committed during backup. The transaction logs that have been committed are still applied to the database.


Note:

For xtrabackup, it is backed up based on transaction logs and data files. The backed up data may contain uncommitted transactions or transactions that have been committed but not synchronized to the database files, we should also pre-process the transaction to synchronize the committed transaction to the data file, and roll back the uncommitted transaction. Therefore, the database backed up cannot be recovered immediately.

Preprocessing process:

First, only committed transactions are synchronized to the data file for the full backup file. Note that data rollback cannot be performed for the transaction when there is an increment, otherwise, your Incremental Backup will be ineffective.

Then merge the first Incremental Backup into the full backup file,

And so on, merge the subsequent increments into the files after the previous merge. In this way, we only need to take the full backup + binary log to restore the data at a time point.

4.14 data recovery


Copy the Code as follows:
[Root @ www ~] # Service mysqld stop
[Root @ www data] # rm-rf * simulate database crash
[Root @ www ~] # Innobackupex -- copy-back/innobackup/2013-09-12_11-03-04/
-- Copy-back database recovery, followed by the location of the Backup Directory


4.15 Detection:


Copy the Code as follows:
[Root @ www ~] # Cd/mydata/data/
[Root @ www data] # chown mysql: mysql *
[Root @ www data] # service mysqld start
Detailed source reference: http://www.jb51.net/article/41570.htm

Http://www.jb51.net/article/41570.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.