2011. 9. 30. 16:38

[Innodb 백업] Percona xtrabackup 설치 및 사용

보통 MySQL 을 백업할땐 mysqldump 를 쓰거나 데이타 디렉토리를 통째로 카피해서 관리하게 된다. 
mysqldump의 경우 복구시 테이블을 생성하고 다시 데이타를 쌓는 부분이라 복구가 불편하고 거기다  InnoDB에서 외래키를 설정했 을 경우 순서가 맞지 않으면 인서트시에 오류가 발생 할 수도 있다.

그리고 파일을 복사할 경우에는 데이타베이스를 중지시키고 데이타파일만을 덮어 씌우면 복구가 완료되기 때문에 편리한 방법이다 다만 파일을 복사할 경우 서버가 운영중인 경우에는 끝나지 않는 트랜잭션이 있을 경우 디스크에 반영되지 않은 상태에서 복사를 하게 되므로 완전한 데이타가 복사되었다는 보장도 할 수 없게 된다.
그래서 완전한 데이타 복사를 위해서는 데이타베이스를 완전히 정지 시키고 파일을 복사해야만 한다.
이렇게 데이타베이스를 정지시키고 복사를 하거나 백업을 하는 것을 cold copy 또는 cold backup 이라고 한다.

백업을 위해 데이타베이스 서버를 정지시킨다면 서비스 운영에 지장이 있기 때문에 데이타베이스를 정지시키지 않고 백업 또는 카피를 하는 것을 cold copy의 반대 개념으로 hot copy 또는 hot backup 이라고 한다.

MyIsam 테이블 타입의 경우 MySQL에서 기본적으로  mysqlhotcopy 라는 툴일 지원하지만 InnoDB의 경우에는 innnodb 사에서 inbackup 이라는 툴을 제공하지만 상용이라 비용을 부담해야 한다. 1년에 500 달러 정도.

http://percona.com 라는 사이트에서 이와 같은 innodb를 hot backup 할 수 있는 xtrabackup를 오픈소스로 제공하기 때문에 이 프로그램의 설치와 사용에 대해 써보겠다.

우선 아래 주소에서 공식 메뉴얼을 한번 살펴보자.
설치에 대한 공식 메뉴얼 : http://www.percona.com/doc/percona-xtrabackup/installation.html

상당히 자세하게 나와있기 때문에 따로 설명할 필요도 없겠지만 영어 울렁증이 있는 사람을 위해 정리를 해보겠다. 
아 그리고 나는 Centos 6.0을 쓰고 있으며 귀차니즘으로 인해 그냥 YUM으로 설치를 하겠다. 위 페이지에 
download page 를 찾아가보면 버젼별로 banary, source, rpm, deb 을 제공해주고 있으니 다운받아 설치하면 되겠다. 

YUM으로 설치를 하기 위해서는 percona에서 제공하는 repository를 추가 해야 한다.
64bit : rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
32bit : rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.i386.rpm
이 명령을 실행하여 repository를 추가하도록 한다.

처음 어리둥절 했던 부분이 
yum list percona 했을때 나오는 것들이었는데 Percona-Server-server-51.x86_64 이런것들은 percona 에서 제공하는 mysql 을 변형한 데이타베이스로 보인다. 설치해보지는 않았지만 이곳저곳에서 줏어 들은 바로는 Percona-Server에서는 InnoDB 를 수정?한 XtraDB 타입을 제공하는 것으로 보인다.

yum list xtrabackup* 하면 실제 내가 필요한 패키지가 리스팅 되게된다.
yum install xtrabackup 해서 설치를 하자.

xtrabackup에 설치되는 실행파일은 3가지로 yum(rpm)설치 일 경우 /usr/bin 에 설치된다.
innobackupex : 펄로된 backup 스크립트 myisam, innodb, xtradb 지원
xtrabackup : c로 컴파일된 바이너리, 실제 실행파일
tar4ibd : 백업파일을 tar로 저장할때 사용하는 실행파일

1. 백업디렉토리 생성 
# mkdir -p /mysqlbackup/xtrabackup/

2. 데이타 백업
# /usr/bin/innobackupex --user UUUUUUU --password PPPPPPP /mysqlbackup/xtrabackup
.............
.............
>> log scanned up to (542812997)
xtrabackup: Stopping log copying thread.
xtrabackup: Transaction log of lsn (542812997) to (542812997) was copied.
110719 18:25:44 innobackupex: All tables unlocked
110719 18:25:44 innobackupex: Connection to database server closed

innobackupex: Backup created in directory '/mysqlbackup/xtrabackup/2011-07-19_18-25-17'
innobackupex: MySQL binlog position: filename 'mysql-bin.000012', position 599281561
innobackupex: MySQL slave binlog position: master host '', filename '', position
110719 18:25:44 innobackupex: completed OK!

/mysqlbackup/xtrabackup/2011-07-19_18-25-17 와 같은 형태로 생성된다.
복원시 my.cnf 설정이 동일해야 하므로 my.cnf 도 백업 해두는 것이 좋다.

3. 로그 백업
# /usr/bin/innobackupex --user UUUUUUU --password PPPPPPP --apply-log /mysqlbackup/xtrabackup/2011-07-19_18-25-17
.............
.............
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
110719 18:32:40 InnoDB: Starting shutdown...
110719 18:32:40 InnoDB: Shutdown completed; log sequence number 542814220
110719 18:32:40 innobackupex: completed OK!

4. 복구, 데이타베이스를 중지 시키고 data디렉토리를 비운후에(기왕이면 기존파일 따로 보관, 새로 만들어서)
# /usr/bin/innobackupex --copy-back /mysqlbackup/xtrabackup/2011-07-19_18-25-17

5. 혹시나 ib_logfile에서 사이즈 오류가 날경우 my.cnf 를 수정한다.
InnoDB: Error: log file ./ib_logfile0 is of different size 0 134217728 bytes
InnoDB: than specified in the .cnf file 0 126877696 bytes!
110720 13:48:47 [ERROR] Plugin 'InnoDB' init function returned error.
110720 13:48:47 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
110720 13:48:47 [ERROR] Unknown/unsupported storage engine: InnoDB
110720 13:48:47 [ERROR] Aborting
.............
.............

# emacs /etc/my.cnf
innodb_log_file_size = XXX M

나머지 부분은 메뉴얼을 보면 쉽게 나와있으므로 한번 읽어보기 바람.

옵션 레퍼런스
Options

--help
This option displays a help screen and exits.

--version
This option displays the innobackupex version and copyright notice and then exits.

--apply-log
Prepare a backup in BACKUP-DIR by applying the transaction log file named xtrabackup_logfile located in the same directory. Also, create new transaction logs. The InnoDB configuration is read from the file backup-my.cnf created by innobackupex when the backup was made.

--redo-only
This option is passed directly to xtrabackup’s xtrabackup --apply-log-only option. This forces xtrabackup to skip the “rollback” phase and do a “redo” only. This is necessary if the backup will have incremental changes applied to it later. See the xtrabackup documentation for details.

--copy-back
Copy all the files in a previously made backup from the backup directory to their original locations.

--include
This option is a regular expression to be matched against table names in databasename.tablename format. It is passed directly to xtrabackup ‘s xtrabackup --tables option. See the xtrabackup documentation for details.

--defaults-file
This option accepts a string argument that specifies what file to read the default MySQL options from. It is also passed directly to xtrabackup ‘s defaults-file option. See the xtrabackup documentation for details.

--databases
This option accepts a string argument that specifies the list of databases that innobackupex should back up. The list is of the form databasename1[.table_name1] databasename2[.table_name2] .... If this option is not specified, all databases containing MyISAM and InnoDB tables will be backed up. Please make sure that --databases contains all of the InnoDB databases and tables, so that all of the innodb.frm files are also backed up. In case the list is very long, this can be specified in a file, and the full path of the file can be specified instead of the list. (See option --tables-file.)

--tables-file
This option accepts a string argument that specifies the file in which there are a list of names of the form database.table, one per line. The option is passed directly to xtrabackup ‘s --tables-file option.

--throttle
This option accepts an integer argument that specifies the number of I/O operations (i.e., pairs of read+write) per second. It is passed directly to xtrabackup’s xtrabackup --throttle option.

--export
This option is passed directly to xtrabackup --export option. It enables exporting individual tables for import into another server. See the xtrabackup documentation for details.

--use-memory
This option accepts a string argument that specifies the amount of memory in bytes for xtrabackup to use for crash recovery while preparing a backup. Multiples are supported providing the unit (e.g. 1MB, 1GB). It is used only with the option --apply-log. It is passed directly to xtrabackup ‘s xtrabackup --use-memory option. See the xtrabackup documentation for details.

--password = 'PASSWORD'
This option accepts a string argument specifying the password to use when connecting to the database. It is passed to the mysql child process without alteration. See mysql –help for details.

--user = 'USER'
This option accepts a string argument that specifies the user (i.e., the MySQL username used when connecting to the server) to login as, if that’s not the current user. It is passed to the mysql child process without alteration. See mysql –help for details.

--port
This option accepts a string argument that specifies the port to use when connecting to the database server with TCP/IP. It is passed to the mysql child process. It is passed to the mysql child process without alteration. See mysql –help for details.

--socket
This option accepts a string argument that specifies the socket to use when connecting to the local database server with a UNIX domain socket. It is passed to the mysql child process without alteration. See mysql –help for details.

--host
This option accepts a string argument that specifies the host to use when connecting to the database server with TCP/IP. It is passed to the mysql child process without alteration. See mysql –help for details.

--no-timestamp
This option prevents creation of a time-stamped subdirectory of the BACKUP-ROOT-DIR given on the command line. When it is specified, the backup is done in BACKUP-ROOT-DIR instead.

--slave-info
This option is useful when backing up a replication slave server. It prints the binary log position and name of the master server. It also writes this information to the xtrabackup_slave_info file as a CHANGE MASTER command. A new slave for this master can be set up by starting a slave server on this backup and issuing a CHANGE MASTER command with the binary log position saved in the xtrabackup_slave_info file.

--no-lock
Use this option to disable table lock with FLUSH TABLES WITH READ LOCK. Use it only if ALL your tables are InnoDB and you DO NOT CARE about the binary log position of the backup.

--ibbackup-binary = 'autodetect'
This option accepts a string argument that specifies which xtrabackup binary should be used. The string should be the command used to run XtraBackup. The option can be useful if the xtrabackup binary is not in your search path or working directory and the database server is not accessible at the moment. If this option is not specified, innobackupex attempts to determine the binary to use automatically. By default, xtrabackup is the command used. When option --apply-log is specified, the binary is used whose name is in the file xtrabackup_binary in the backup directory, if that file exists, or will attempt to autodetect it. However, if --copy-back is selected, xtrabackup is used unless other is specified.

--incremental
This option tells xtrabackup to create an incremental backup, rather than a full one. It is passed to the xtrabackup child process. When this option is specified, either --incremental-lsn or --incremental-basedir can also be given. If neither option is given, option --incremental-basedir is passed to xtrabackup by default, set to the first timestamped backup directory in the backup base directory.

--incremental-basedir
This option accepts a string argument that specifies the directory containing the full backup that is the base dataset for the incremental backup. It is used with the --incremental option.

--incremental-dir
This option accepts a string argument that specifies the directory where the incremental backup will be combined with the full backup to make a new full backup. It is used with the --incremental option.

--incremental-lsn
This option accepts a string argument that specifies the log sequence number (LSN) to use for the incremental backup. It is used with the --incremental option. It is used instead of specifying --incremental-basedir. For databases created by MySQL and Percona Server 5.0-series versions, specify the as two 32-bit integers in high:low format. For databases created in 5.1 and later, specify the LSN as a single 64-bit integer.

--extra-lsndir
This option accepts a string argument that specifies the directory in which to save an extra copy of the xtrabackup_checkpoints file. It is passed directly to xtrabackup ‘s --extra-lsndir option. See the xtrabackup documentation for details.

--remote-host
This option accepts a string argument that specifies the remote host on which the backup files will be created, by using an ssh connection.

--stream
This option accepts a string argument that specifies the format in which to do the streamed backup. The backup will be done to STDOUT in the specified format. Currently, the only supported format is tar. Uses tar4ibd, which is available in XtraBackup distributions. If you specify a path after this option, it will be interpreted as the value of tmpdir.

--tmpdir
This option accepts a string argument that specifies the location where a temporary file will be stored. It should be used when --remote-host or --stream is specified. For these options, the transaction log will first be stored to a temporary file, before streaming or copying to a remote host. This option specifies the location where that temporary file will be stored. If the option is not specifed, the default is to use the value of tmpdir read from the server configuration.

--tar4ibd
Uses tar4ibd in --stream mode. It is enabled by default if no other command is specified (e.g. --force-tar).

--force-tar
This option forces the use of tar when creating a streamed backup, rather than tar4ibd, which is the default.

--scp-opt = '-Cp -c arcfour'
This option accepts a string argument that specifies the command line options to pass to scp when the option --remost-host is specified. If the option is not specified, the default options are -Cp -c arcfour.

--parallel
This option accepts an integer argument that specifies the number of threads the xtrabackup child process should use to back up files concurrently. Note that this option works on file level, that is, if you have several .ibd files, they will be copied in parallel. If you have just single big .ibd file, it will have no effect. It is passed directly to xtrabackup’s xtrabackup --parallel option. See the xtrabackup documentation for details.

--safe-slave-backup
Stop slave SQL thread and wait to start backup until Slave_open_temp_tables in SHOW STATUS is zero. If there are no open temporary tables, the backup will take place, otherwise the SQL thread will be started and stopped until there are no open temporary tables. The backup will fail if Slave_open_temp_tables does not become zero after --safe-slave-backup-timeout seconds. The slave SQL thread will be restarted when the backup finishes.

--safe-slave-backup-timeout
How many seconds --safe-slave-backup` should wait for Slave_open_temp_tables to become zero. Defaults to 300 seconds.