Mysql 备份工具XtraBackup全量备份

Percona安装: http://donald-draper.iteye.com/admin/blogs/2314832
命令介绍:
The database user used to connect to the server and its password are specified by the --user and --password option,
$ innobackupex --user=DBUSER --password=SECRET /path/to/backup/dir/
$ innobackupex --user=LUKE  --password=US3TH3F0RC3 --stream=tar ./ | bzip2 -
$ xtrabackup --user=DVADER --password=14MY0URF4TH3R --backup --target-dir=/data/bkps/
If you don’t use the --user option, Percona XtraBackup will assume
the database user whose name is the system user executing it.
Other Connection Options
According to your system, you may need to specify one or more of the following options to connect to the server:
Option Description
–port The port to use when connecting to the database server with TCP/IP.
–socket The socket to use when connecting to the local database.
–host The host to use when connecting to the database server with TCP/IP.

innobackupex
    a wrapper script that provides functionality to backup a
    whole MySQL database instance with MyISAM, InnoDB, and XtraDB tables.
xtrabackup
    a compiled C binary, which copies only InnoDB and XtraDB data
xbstream
    new utility that allows streaming and extracting files to/from the xbstream format.

It is possible to use the xtrabackup binary alone, however,
the recommend way is using it through the innobackupex wrapper script and
let it execute xtrabackup for you. It might be helpful to first learn
how to use innobackupex, and then learn how to use xtrabackup for
having a better low-level understanding or control of the tool if needed.
---------------------------------------------------------------------------
命令实例:
# --defaults-file指定mysql配置文件
# innobackupex --defaults-file=/etc/my.cnf --user=donald --password=123456 /home/test/base
备份, 添加--no-timestamp选项,无时间戳,直接备份在base中,否则在base以时间戳建一个文件夹
innobackupex --user=donald --password=123456  --no-timestamp /home/test/base
准备还原:--use-memory 使用内存
innobackupex --apply-log --use-memory=100M /home/test/base
开始:
innobackupex --copy-back /home/test/base
注意:innobackupex备份的时候,innobackupex必须可以访问mysql datafile目录
以下是全量备份实验:
我的mysqldata数据目录是msyqldata
修改mysql数据目录权限:
[donald@test home]$ ls -al
total 12
drwxr-xr-x.  7 root   root    79 Aug  3 10:48 .
dr-xr-xr-x. 17 root   root  4096 Jul 26 14:32 ..
drwx------. 18 donald user  4096 Aug  5 11:38 donald=
drwxrwxr-x.  5 mysql  mysql 4096 Aug  5 11:37 mysqldata
drwxr-xr-x   5 mysql  mysql  119 Jul 26 15:51 mysqldata1
drwxrwxr-x.  2 mysql  mysql   64 Jul 26 16:05 mysqllog
drwxr-x---   4 donald user    30 Aug  5 15:00 test

[donald@test home]$ sudo chmod -R 777 mysqldata

[donald@test home]$ ls -al
total 12
drwxr-xr-x.  7 root   root    79 Aug  3 10:48 .
dr-xr-xr-x. 17 root   root  4096 Jul 26 14:32 ..
drwx------. 18 donald user  4096 Aug  5 11:38 donald=
drwxrwxrwx.  5 mysql  mysql 4096 Aug  5 11:37 mysqldata
drwxr-xr-x   5 mysql  mysql  119 Jul 26 15:51 mysqldata1
drwxrwxr-x.  2 mysql  mysql   64 Jul 26 16:05 mysqllog
drwxr-x---   4 donald user    30 Aug  5 15:00 test

备份:
donald@test home]$ innobackupex --user=donald --password=123456  /home/test/base

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013.  All Rights Reserved.

This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p
....

查看生成的文件:
[donald@test base]$ ls
2016-08-05_16-08-30
[donald@test base]$ cd 2016-08-05_16-08-30/
[donald@test 2016-08-05_16-08-30]$ ls -al
total 274468
drwxr-xr-x 5 donald user      4096 Aug  5 16:08 .
drwx------ 3 donald user        32 Aug  5 16:10 ..
-rw-r--r-- 1 donald user       295 Aug  5 16:08 backup-my.cnf
-rw-r----- 1 donald user 281018368 Aug  5 16:08 ibdata1
drwx------ 2 donald user      4096 Aug  5 16:08 mysql
drwxr-xr-x 2 donald user      4096 Aug  5 16:08 performance_schema
drwx------ 2 donald user      4096 Aug  5 16:08 test
-rw-r--r-- 1 donald user        22 Aug  5 16:08 xtrabackup_binlog_info
-rw-r----- 1 donald user        97 Aug  5 16:08 xtrabackup_checkpoints
-rw-r--r-- 1 donald user       573 Aug  5 16:08 xtrabackup_info
-rw-r----- 1 donald user      2560 Aug  5 16:08 xtrabackup_logfile

查看检查点文件:
[donald@test 2016-08-05_16-08-30]$ cat xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 13194628771
last_lsn = 13194628771
compact = 0

准备恢复:

[donald@test home]$ innobackupex --apply-log /home/test/base/2016-08-05_16-08-30

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013.  All Rights Reserved.

This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p

160805 16:13:09  innobackupex: Starting the apply-log operation

IMPORTANT: Please check that the apply-log run completes successfully.
           At the end of a successful apply-log run innobackupex
           prints "completed OK!".

转移mysql数据文件:
[donald@test home]$ sudo service mysql stop
Shutting down MySQL.. SUCCESS!

[donald@test home]$ sudo mv mysqldata mysqldata2
[donald@test home]$ ls
donald=  mysqldata1  mysqldata2  mysqllog  test

全量恢复:
[donald@test home]$ sudo innobackupex --copy-back /home/test/base/2016-08-05_16-08-30/
.....
innobackupex: Starting to copy InnoDB log files
innobackupex: in '/home/test/base/2016-08-05_16-08-30'
innobackupex: back to original InnoDB log directory '/home/mysqldata'
innobackupex: Copying '/home/test/base/2016-08-05_16-08-30/ib_logfile1' to '/home/mysqldata/ib_logfile1'
innobackupex: Copying '/home/test/base/2016-08-05_16-08-30/ib_logfile0' to '/home/mysqldata/ib_logfile0'
innobackupex: Finished copying back files.

160805 16:22:57  innobackupex: completed OK!
[donald@test home]$ ls
donald=  mysqldata  mysqldata1  mysqldata2  mysqllog  test
[donald@test home]$ ls -al
total 16
drwxr-xr-x.  8 root   root    96 Aug  5 16:21 .
dr-xr-xr-x. 17 root   root  4096 Jul 26 14:32 ..
drwx------. 18 donald user  4096 Aug  5 11:38 donald=
drwxr-xr-x   5 root   root  4096 Aug  5 16:22 mysqldata
drwxr-xr-x   5 mysql  mysql  119 Jul 26 15:51 mysqldata1
drwxrwxrwx.  5 mysql  mysql 4096 Aug  5 16:19 mysqldata2
drwxrwxr-x.  2 mysql  mysql   64 Jul 26 16:05 mysqllog
drwxr-x---   4 donald user    30 Aug  5 15:00 test
恢复mysql数据目录权限
[donald@test home]$ sudo chown -R mysql:mysql mysqldata
[donald@test home]$ ls -al
total 16
drwxr-xr-x.  8 root   root    96 Aug  5 16:21 .
dr-xr-xr-x. 17 root   root  4096 Jul 26 14:32 ..
drwx------. 18 donald user  4096 Aug  5 11:38 donald=
drwxr-xr-x   5 mysql  mysql 4096 Aug  5 16:22 mysqldata
drwxr-xr-x   5 mysql  mysql  119 Jul 26 15:51 mysqldata1
drwxrwxrwx.  5 mysql  mysql 4096 Aug  5 16:19 mysqldata2
drwxrwxr-x.  2 mysql  mysql   64 Jul 26 16:05 mysqllog
drwxr-x---   4 donald user    30 Aug  5 15:00 test
[donald@test home]$ sudo service mysql start
Starting MySQL.. SUCCESS!
至此全量恢复完成,注意在备份恢复的过程中, mysql目录文件夹的权限



猜你喜欢

转载自donald-draper.iteye.com/blog/2315812