XTRABACKUP
xtrabackup是percona公司开发的第三方备份软件,只备份innodb引擎表的ibd文件(frm不备份),不备份myisam引擎表。备份性能很高,备份期间没有任和锁,ddl、dml、select都不受影响
xtrabackup全备:
step1:这步只是在系统层做了个拷贝,数据库目录只拷贝ibd文件,frm表结构需要恢复时手动拷贝,log文件不备份
[[email protected] data]# xtrabackup --backup --datadir=/mysql/data/--target-dir=/root/bak
[[email protected] bak]# ls
db1 ibdata1 xtrabackup_checkpoints xtrabackup_logfile
step2:这步会根据第一步的lsn结果扫描日志文件,将备份期间的新log、undo,补充到备份文件中,使他们合并成为完整的有一致性的
[[email protected] bak]# xtrabackup --prepare --use-memory=50M--datadir=/mysql/data/--target-dir=/root/bak
[[email protected] bak]# ls
db1 ibdata1 xtrabackup_binlog_pos_innodb xtrabackup_checkpoints xtrabackup_logfile
xtrabackup 增量备:
周一:首先做一个全备,但不要做prepare
insert into test3 values(1);commit;
xtrabackup --backup --datadir=/mysql/data/ --target-dir=/root/bak/1/
周二:基于周一做增量备,--incremental-basedir设置为周一全备的目录,表示基于哪天做增量
insert into test3 values(2);commit;
xtrabackup --backup --datadir=/mysql/data/ --target-dir=/root/bak/2/ --incremental-basedir=/root/bak/1/
周三:基于周二做增量备
insert into test3 values(3);commit;
xtrabackup --backup --datadir=/mysql/data/ --target-dir=/root/bak/3/ --incremental-basedir=/root/bak/2/
恢复:
--apply-log-only 只应用日志,把增量合在一起,而不产生日志
xtrabackup --prepare --apply-log-only --redo-only --target-dir=/root/bak/1/
xtrabackup --prepare --apply-log-only --redo-only --target-dir=/root/bak/1/ --incremental-basedir=/root/bak/2/
xtrabackup --prepare --apply-log-only --redo-only --target-dir=/root/bak/1/ --incremental-basedir=/root/bak/3/
这时目录1,数据才是完整的了
xtrabackup --prepare --target-dir=/root/bak/1
最终生成完整备份,可以看到目录1下面生成了ib_logfile日志文件
INNOBACKUPEX
使用innobackupex做完整备份:
先保证my.cnf配置文件中配置了 basedir 和 datadir,如果配置文件在其他位置,指定‘--defaults-file=/etc/my.cnf’
step1:生成完整备份,同时copy innodb和myisam以及frm文件,会锁myisam表
[[email protected] bak]# innobackupex --user=root /root/bak
InnoDBBackupUtility v1.5.1-xtrabackup;Copyright2003,2009InnobaseOy
and Percona LLC and/or its affiliates 2009-2013. AllRightsReserved.
This software is published under
the GNU GENERAL PUBLIC LICENSE Version2,June1991.
Get the latest version of PerconaXtraBackup, documentation, and help resources:
http://www.percona.com/xb/p
14081014:09:34 innobackupex:Connecting to MySQL server with DSN ‘dbi:mysql:;mysql_read_default_group=xtrabackup‘ as ‘root‘ (using password: NO).
14081014:09:34 innobackupex:Connected to MySQL server
14081014:09:34 innobackupex:Executing a version check against the server...
14081014:09:34 innobackupex:Done.
IMPORTANT:Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".
innobackupex:Using mysql server version 5.5.30-log
innobackupex:Created backup directory /root/bak/2014-08-10_14-09-34
14081014:09:34 innobackupex:Starting ibbackup with command: xtrabackup --defaults-group="mysqld"--backup --suspend-at-end --target-dir=/root/bak/2014-08-10_14-09-34--tmpdir=/tmp --extra-lsndir=‘/tmp‘
innobackupex:Waitingfor ibbackup (pid=15364) to suspend
innobackupex:Suspend file ‘/root/bak/2014-08-10_14-09-34/xtrabackup_suspended_2‘
xtrabackup version 2.2.3 based on MySQL server 5.6.17Linux(x86_64)(revision id:)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /mysql/data
xtrabackup: open files limit requested 0,set to 40960
xtrabackup:using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir =./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir =./
xtrabackup: innodb_log_files_in_group =2
xtrabackup: innodb_log_file_size =5242880
>> log scanned up to (1670110)
[01]Copying./ibdata1 to /root/bak/2014-08-10_14-09-34/ibdata1
>> log scanned up to (1670110)
[01] ...done
>> log scanned up to (1670110)
[01]Copying./db1/test3.ibd to /root/bak/2014-08-10_14-09-34/db1/test3.ibd
[01] ...done
[01]Copying./db1/test1.ibd to /root/bak/2014-08-10_14-09-34/db1/test1.ibd
[01] ...done
[01]Copying./db1/test4.ibd to /root/bak/2014-08-10_14-09-34/db1/test4.ibd
[01] ...done
[01]Copying./mysql/abc.ibd to /root/bak/2014-08-10_14-09-34/mysql/abc.ibd
[01] ...done
[01]Copying./db2/test.ibd to /root/bak/2014-08-10_14-09-34/db2/test.ibd
[01] ...done
>> log scanned up to (1670110)
xtrabackup:Creating suspend file ‘/root/bak/2014-08-10_14-09-34/xtrabackup_suspended_2‘ with pid ‘15364‘
14081014:09:38 innobackupex:Continuing after ibbackup has suspended
14081014:09:38 innobackupex:Executing FLUSH TABLES WITH READ LOCK...
14081014:09:38 innobackupex:All tables locked and flushed to disk
14081014:09:38 innobackupex:Starting to backup non-InnoDB tables and files
innobackupex: in subdirectories of ‘/mysql/data‘
innobackupex:Backing up file ‘/mysql/data/db1/test4.frm‘
innobackupex:Backing up file ‘/mysql/data/db1/test3.frm‘
>> log scanned up to (1670110)
innobackupex:Backing up file ‘/mysql/data/db1/db.opt‘
innobackupex:Backing up file ‘/mysql/data/db1/test1.frm‘
innobackupex:Backing up files ‘/mysql/data/mysql/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘(73 files)
innobackupex:Backing up files ‘/mysql/data/performance_schema/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘(18 files)
innobackupex:Backing up file ‘/mysql/data/db2/db.opt‘
innobackupex:Backing up file ‘/mysql/data/db2/test.frm‘
14081014:09:38 innobackupex:Finished backing up non-InnoDB tables and files
14081014:09:38 innobackupex:Executing FLUSH ENGINE LOGS...
14081014:09:38 innobackupex:Waitingfor log copying to finish
xtrabackup:The latest check point (for incremental):‘1670110‘
xtrabackup:Stopping log copying thread.
.>> log scanned up to (1670110)
xtrabackup:Creating suspend file ‘/root/bak/2014-08-10_14-09-34/xtrabackup_log_copied‘ with pid ‘15364‘
xtrabackup:Transaction log of lsn (1670110) to (1670110) was copied.
14081014:09:39 innobackupex:All tables unlocked
innobackupex:Backup created in directory ‘/root/bak/2014-08-10_14-09-34‘
innobackupex:MySQL binlog position: filename ‘mysql-bin.000011‘, position 107
14081014:09:39 innobackupex:Connection to database server closed
14081014:09:39 innobackupex: completed OK!
step2:同步日志文件
[[email protected] bak]# innobackupex --use-memory=50M --apply-log /root/bak/2014-08-10_14-09-34/
恢复完整备份:
service mysql stop
清空data目录
innobackupex会根据配置文件中datadir进行恢复
[[email protected] data]# innobackupex --copy-back /root/bak/2014-08-10_14-09-34/
chown mysql. * -R
service mysql start
使用innodbbackupex做增量备份
mkdir {1,2,3}
周一:
insert into test3 values(1);commit;
[[email protected] bak]# innobackupex --user=root /root/bak/1/
周二:
insert into test3 values(2);commit;
[[email protected] bak]# innobackupex --user=root --incremental /root/bak/2/ --incremental-basedir=/root/bak/1/2014-08-10_14-37-26/
周三:
insert into test3 values(3);commit;
[[email protected] 2]# innobackupex --user=root --incremental /root/bak/3/ --incremental-basedir=/root/bak/2/2014-08-10_14-42-01/
恢复增量备份:
准备原始备份:--redo-only同xtrabackup的apply-log-only
[[email protected] 2]# innobackupex --apply-log --redo-only /root/bak/1/2014-08-10_14-37-26/ --use-memory=50M --user=root
叠加周二增量:
[[email protected] 2]# innobackupex --apply-log --redo-only /root/bak/1/2014-08-10_14-37-26/ --incremental-dir=/root/bak/2/2014-08-10_14-42-01/ --use-memory=50m --user=root
叠加周三增量:
[[email protected] 2]# innobackupex --apply-log --redo-only /root/bak/1/2014-08-10_14-37-26/ --incremental-dir=/root/bak/3/2014-08-10_14-43-40/ --use-memory=50m --user=root
这样最终在目录1生成完整备份文件。最后执行:
[[email protected] 2]# innobackupex --apply-log /root/bak/1/2014-08-10_14-37-26/ --use-memory=50m --user=root
将完整的增量备份恢复到数据库:
service mysql stop
清空data目录
[[email protected] mysql]# innobackupex --copy-back /root/bak/1/2014-08-10_14-37-26/
chown mysql. * -R
service mysql start
OK~~