[Reserved] mysql three backup

First, the purpose of backup

Do disaster recovery: recover the damaged data and restore
needs to change: Due to changes in demand and the need to restore the data to change the previous
test: test the new features are available

Second, the backup issues to be considered

How long can tolerate the loss of data;
restore the data to be completed within how long; 
whether the recovery time need to continue to provide services;
objects recovered, the entire database, multiple tables, or a single database, a single table.

Third, the type of backup

1, depending on whether the database is offline

Cold backup (Cold Backup) : mysql need to shut the service, read and write requests are not allowed to state;
warm equipment (Warm Backup) : Online service, but only supports a read request, allowed to write request;
hot backup (Hot Backup) : Meanwhile backup services are not affected.

Note:

1, this type of backup, depending on business needs, rather than the backup tool
2, MyISAM does not support hot standby, InnoDB hot backup support, but requires special tools

2, according to the range of the set of data to be backed
full backup: full backup, the backup of all characters.
Incremental backup: incremental backup the last full or incremental backup has changed since the data can not be used alone, must rely on full backup, backup frequency depends on the update frequency of the data.
Differential backup: since the last full backup differential backup of changed data.
Proposed recovery strategy:
Full + Incremental + binary log
Full + Differential + binary log

3, according to the backup data file or

Physical backup: Direct backup data files

advantage:

Backup and restore operations are relatively simple and can be cross-mysql version,
fast recovery speed, belonging to the file system level

Suggest:

Must not assume the backup is available, to test
mysql> check tables; detection table is available
logical backup: backup data and code tables

advantage:

Recovery simple,
the results of the backup as an ASCII file, you can edit
nothing to do with the storage engine
can back up and restore the network

Disadvantages:

Backup or restore requires mysql server processes involved in
backup results occupy more space,
floating point precision may be lost
after the reduction, the epitome of the need to rebuild

Four: The backed up objects

1, data;
2, profile;
3, Code: stored procedures, stored functions, trigger
4, os-related profiles
5, arranged to copy the relevant
6, Binary Log

Fifth, the backup and recovery of realization

1 using select into outfile data backup and restore
1.1 backs up data out

MySQL> use hellodb;           // open hellodb library 
mysql> select * from students;    view the properties of students 
MySQL> the SELECT * from students the WHERE Age> 30 INTO outfile '/tmp/stud.txt';     // will be older than thirty information students back out 

note:

Backup directory path must allow the currently running mysql server mysql user has access

The need to copy files to back up away from the tmp directory after the backup is complete, or else lose the purpose of the Backup

Check back to have a backup file tmp directory

[root@www ~]# cd /tmp

[root@www tmp]# cat stud.txt

3Xie Yanke53M216

4Ding Dian32M44

6Shi Qing46M5\N

13Tian Boguang33M2 \ N

25Sun Dasheng100M\N\N

[root@www tmp]#

You will find a text file. It can not be imported directly into the database. Need to use load data infile recovery

Back to the mysql server, delete users older than 30, the analog data is destroyed

mysql> delete from students where Age > 30;
mysql> load data infile '/tmp/stud.txt' into table students;

2, using the tools mysqldump data backup and restore

mysqldump commonly used to prepare warm, so we first need to be applied to the data read lock want to back up,

2.1 applying a read lock:

1. Add option directly when backup

--lock-all-tables is to apply a read lock on all tables of the database to be backed up

Applying a read lock --lock-table only a single table, even if the entire database is backed up, it is only applying a read lock on the table when we back up a particular table, it is applied to backup a single table

2, the server side the writing command,

Copy the code
mysql> flush tables with read lock; lock is applied, showing the memory located on all tables are synchronized to disk up, and then applying a read lock
mysql> flush tables with read lock; read lock release

But for the InnoDB storage engine is concerned, although you can also pleaded read lock, but it does not mean that all the data has been synchronized to disk, 
so when faced InnoDB, we want to use
mysql> show engine innodb status;
InnoDB look at all the data has been synchronized to disk up, only for backup operations.
Copy the code

2.2 backup strategy:

Full backup + incremental backup binary log +

Demonstration backup;

2.3 give a full backup of the database to do:

 [root@www ~]# mysqldump -uroot --single-transaction --master-data=2 --databases hellodb > /backup/hellodb_`date +%F`.sql

--single-transaction: This option can be realized based on hot standby InnoDB tables; thus, no use --lock-all-tables;
--master-data = log recording location of the binary moment of backup, and commented, it is not an annotated
--databases hellodb specified database backup
Then back to the mysql server, 

 

 

2.4 updated data back to the server mysql

mysql> create table tb1 (id int); Create table
mysql> insert into tb1 values ​​(1), (2), (3); insert data, where only presentation, just inserted several data

2.5 to view the full backup file position inside the record:

[root@www backup]# cat hellodb_2013-09-08.sql | less
- CHANGE MASTER TO MASTER_LOG_FILE = 'mysql-bin.000013', MASTER_LOG_POS = 15684; log records the location of the binary

2.6 back to the server:

Copy the code
mysql> show master status;
Display at this time of the binary log position, from a position inside the backup file to record our position at this time, that is, the incremental part
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      15982 |              |                  |
+------------------+----------+--------------+------------------+
Copy the code

2.7 do incremental backups

[root@www backup]# mysqlbinlog --start-position=15694 --stop-position=15982
/mydata/data/mysql-bin.000013 > /backup/hellodb_`date +$F_%H`.sql

2.8 and then back to the server

mysql> insert into tb1 values ​​(4), (5); in some numerical inserted
mysql> drop database hellodb; deleted hellodb library 

The export was 2.9 binary log:

mysqlbinlog --start-position = 15982 /mydata/data/mysql-bin.000013 
when the viewing position of the binary log deletion # mysqlbinlog --start-position = 15982 --stop -position = 16176 / mydata / data / mysql-bin .000013> /tmp/hellodb.sql
// export binary log

2.10 Let mysql offline

mysql> set sql_log_bin = 0;   Close binary log 
mysql> flush logs; scroll down logs 

2.11 simulation database corruption

mysql> drop database hellodb;

2.12 began to recover data:

[root @ the WWW] # MySQL </backup/hellodb_2013-09-08.sql  
 // import the full backup file
[root@www ]# mysql < /backup/hellodb_2013-09-08_05.sql 
// Import incremental backup files
[root@www ]# mysql< hellodb.sql 
// Import binary file

Verification is complete, the result is that we want it to be

Note:

1, in a real production environment, we should export that data throughout the mysql server, rather than a single library, you should use-Databases --all
2, when export binary log, you can directly copy the file to, but It should be noted that, scroll down the log before you back up.
3, by using the snapshot lvm almost hot standby data backup and recovery

3.1 Strategy:

Full backup + binary log;

3.2 Preparation:

Note: The transaction log file must be followed by data on the same LV;

3.3 Lvm create lvm created here is not to say, want to know, then click http://www.jb51.net/LINUXjishu/105937.html

3.4 mysql modify the main configuration files are stored in the directory file permissions and owner is a group, and initialize the mysql

Copy the code code is as follows:

[the root WWW @ ~] # mkdir / mydata / Data              // Create Data Directory 
[the root WWW @ ~] # chown MySQL: MySQL / mydata / Data   // group ownership change is 
[root @ www ~] # cd / usr / local / mysql /     // this directory must stand       
[the root @ WWW mysql] # scripts / --datadir the mysql_install_db --user = mysql = / mydata / Data   // initialization mysql 

3.5 modify the configuration file:

vim /etc/my.cof
datadir = / mydata / data   added data directory 
sync_binlog = 1   turn on this feature 

3.6 Start Service

[root@www mysql]# service mysqld start
mysql> set session sql_log_bin = 0;   Close binary log 
mysql> source /backup/all_db_2013-09-08.sql   reading backup file

3.7 back to mysql server:

Copy the code
mysql> FLUSH TABLES WITH READ LOCK;
Request a read lock Note: Do not quit, the other from a terminal:
mysql> SHOW MASTER STATUS;         
See where binary files
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      107 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> FLUSH LOGS;  
Recommendations scroll down the log. In this way, when the backup log will be very convenient
Copy the code

3.8 Export binary files, create a separate directory store

[root@www ~]# mkdir /backup/limian
[root@www ~]# mysql -e 'show master status;' > /backup/limian/binlog.txt

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

[root@www ~]# lvcreate -L 100M -s -p r -n mysql_snap /dev/myvg/mydata 

Back to the server, release the read lock

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 updated database data, and delete data directory to the data file, database corruption simulation

1
2
3
4
5
6
7
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 Service

Copy the code
[root@www data]# service mysqld start
[Root @ www data] # mysql landing test
mysql> SHOW DATABASES;
mysql> SET sql_log_bin=0
mysql> source / backup / limian / binlog.sql; # binary recovery
mysql> SHOW TABLES; # review the recovery results
mysql> SET sql_log_bin = 1; # opening binary log

NOTE: this manner the hot standby mode is close to a backup data file, and data files on lvm lvm the flexibility to change the size according to the size of the data back up is also very simple.
Copy the code

4, based on Xtrabackup do backup and recovery

Official site: www.percona.com

Advantage:

1, fast and reliable full backup
2, the backup process will not affect the affairs
3, supports data streaming, network transmission, compression, so it can effectively conserve disk resources and network bandwidth.
4, can automatically check data backup availability.

Installation Xtrabackup

[root@www ~]# rpm -ivh percona-xtrabackup-2.1.4-656.rhel6.i686.rpm 

The latest version of the software can be obtained from http://www.percona.com/software/percona-xtrabackup/

Note: When a backup of the database, we should have the authority, but should be noted that the minimum user permissions when the database backup should be given to ensure the safety,

4.1 Prerequisite:

It should be determined using a single table in a table space, or do not support the backup and restore a single table.
In the mysqld section of the configuration file inside plus

 

innodb_file_per_table = 1

4.2 backup strategy
full backup + incremental backup binary log +
4.3 prepare a directory for storing backup data

[root@www ~]# mkdir /innobackup

4.4 to do a full backup:

[root@www ~]# innobackupex --user=root --password=mypass /innobackup/

Note:

1, as long as the last line of the display innobackupex: completed OK !, it means that your backup is correct.
2. Also note that after each backup, automatically creates a current time point directory named in the data directory for storing backup data, then we have to see what

 

Copy the code
[root@www 2013-09-12_11-03-04]# ls
backup-my.cnf ibdata1 performance_schema xtrabackup_binary xtrabackup_checkpoints
hellodb mysql test xtrabackup_binlog_info xtrabackup_logfile
[root@www 2013-09-12_11-03-04]#
xtrabackup_checkpoints: backup type, backup status, and the LSN (Log Sequence Number) range information;
xtrabackup_binlog_info: mysql server binary log files currently in use up until the moment the backup location binary log events.
xtrabackup_logfile: non-text files, xtrabackup own log file
xtrabackup_binlog_pos_innodb: current position binary log file and binary log files for InnoDB or XtraDB table.
backup-my.cnf: When backing up data files on the configuration of mysqld
Copy the code

4.5 Back mysql server for data update

mysql> use hellodb;
mysql> delete from students where StuID>=24;

4.6 Incremental Backup

innobackupex --user=root --password=mypass --incremental /innobackup/--incremental-basedir=/innobackup/2013-09-12_11-03-04/
--incremental specify the type of backup
--incremental-basedir = specify the incremental backup is based on which a backup, here is the full backup file, merge the data so you can put incremental backup to a full backup to

4.7 second increments

Go to modify the data

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 only the last directory to make the first incremental backup data directory can be
Copy the code
4.8 The last time the data changes but did not do incremental backups
mysql> delete from coc where id=14;

4.9 binary log file backup, and (because of last modification, did not do incremental backups, to rely on the binary logs do point in time recovery)
[root@www data]# cp mysql-bin.000003 /tmp/

4.10 simulation database crash
[root@www data]# service mysqld stop
[root@www data]# rm -rf *

Preparation for recovery

4.11 pairs to do a full backup of data synchronization
[root@www ~]# innobackupex --apply-log --redo-only /innobackup/2013-09-12_11-03-04/

4.12 pairs of the first to do incremental data synchronization
innobackupex --apply-log --redo-only /innobackup/2013-09-12_11-03-04/ --incremental-basedir=/innobackup/2013-09-12_11-37-01/

4.13 pairs of the second increments do data synchronization
innobackupex --apply-log --redo-only /innobackup/2013-09-12_11-03-04/ --incremental-basedir=/innobackup/2013-09-12_11-45-53/
Meaning --apply-log that did not commit the time to back up the transaction canceled, but still has commit the transaction log to the database

Note:

For xtrabackup is concerned, it is based on the transaction log and data file backup, the backup data may contain transactions that have not submitted or has been submitted but not yet synchronized to the database file transaction, they should also do their pre-treatment, to have transaction submitted to synchronize data files, uncommitted transactions to be rolled back. So its database backup, recovery can not be used immediately.

Pretreatment process:

First of all affairs full backup only the files submitted to synchronize data files, should be noted that there is incremental when data do not roll back the transaction, or your incremental backup would be no effects.

Then the first incremental backup incorporated into the full backup file,

And so, after a few times the increment are merged into one file after the merger front, so then we just took the full backup + binary log, you can do point in time recovery.
Copy the code

4.14 Data Recovery

[root@www ~]# service mysqld stop
[Root @ www data] # rm -rf * database crash simulation
[root@www ~]# innobackupex --copy-back /innobackup/2013-09-12_11-03-04/
--copy-back database recovery, back to keep the location of the backup directory

4.15 Detection:

[root@www ~]# cd /mydata/data/
[root@www data]# chown mysql:mysql *
[root@www data]#service mysqld start

 

A detection result of the normal data.

Original Address: https://www.cnblogs.com/fengzhongzhuzu/p/9101782.html

Guess you like

Origin www.cnblogs.com/e2fs/p/11884651.html