Ubuntu 12.04.5 LTS
mysql 5.5.47
备份路径 /backup/
apt-get install percona-xtrabackup
root@iZu1dc59z8tZ:/var/lib# innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=123123 /backup/
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/ubuntu
160217 11:17:53 innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/etc/mysql/my.cnf;mysql_read_default_group=xtrabackup' as 'root' (using password: YES).
160217 11:17:53 innobackupex: Connected to MySQL server
160217 11:17:53 innobackupex: Executing a version check against the server...
160217 11:17:53 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.47-0ubuntu0.14.04.1 innobackupex: Created backup directory /backup/2016-02-17_11-17-53 160217 11:17:53 innobackupex: Starting ibbackup with command: xtrabackup_55 --defaults-file="/etc/mysql/my.cnf" --defaults-group="mysqld" --backup --suspend-at-end --target-dir=/backup/2016-02-17_11-17-53 --tmpdir=/tmp innobackupex: Waiting for ibbackup (pid=30486) to suspend innobackupex: Suspend file '/backup/2016-02-17_11-17-53/xtrabackup_suspended_2' xtrabackup_55 version 2.1.8 for Percona Server 5.5.35 Linux (x86_64) (revision id: undefined) xtrabackup: uses posix_fadvise(). xtrabackup: cd to /var/lib/mysql 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 (3233814) [01] Copying ./ibdata1 to /backup/2016-02-17_11-17-53/ibdata1 [01] ...done >> log scanned up to (3233814) xtrabackup: Creating suspend file '/backup/2016-02-17_11-17-53/xtrabackup_suspended_2' with pid '30487' 160217 11:17:55 innobackupex: Continuing after ibbackup has suspended 160217 11:17:55 innobackupex: Starting to lock all tables... 160217 11:17:55 innobackupex: All tables locked and flushed to disk 160217 11:17:55 innobackupex: Starting to backup non-InnoDB tables and files innobackupex: in subdirectories of '/var/lib/mysql' innobackupex: Backing up files '/var/lib/mysql/phpmyadmin/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (37 files) >> log scanned up to (3233814) innobackupex: Backing up files '/var/lib/mysql/mysql/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (72 files) innobackupex: Backing up files '/var/lib/mysql/performance_schema/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files) innobackupex: Backing up files '/var/lib/mysql/blog/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (29 files) 160217 11:17:56 innobackupex: Finished backing up non-InnoDB tables and files 160217 11:17:56 innobackupex: Waiting for log copying to finish xtrabackup: The latest check point (for incremental): '3233814' xtrabackup: Stopping log copying thread. .>> log scanned up to (3233814) xtrabackup: Creating suspend file '/backup/2016-02-17_11-17-53/xtrabackup_log_copied' with pid '30487' xtrabackup: Transaction log of lsn (3233814) to (3233814) was copied. 160217 11:17:57 innobackupex: All tables unlocked innobackupex: Backup created in directory '/backup/2016-02-17_11-17-53' 160217 11:17:57 innobackupex: Connection to database server closed 160217 11:17:57 innobackupex: completed OK!
completed OK! 表示备份成功。
还有很多其他常用的参数。
–stream=tar 压缩类型, 默认是输出到终端,如果保存为文件
innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=*** /backup/ --stream=tar > /backup/`date +"%Y%m%d_%H%M%S"`.tar
#or gzip 压缩
innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=*** /backup/ --stream=tar | gzip > /backup/`date +"%Y%m%d_%H%M%S"`.tar.gz
–parallel=4 –throttle=400 并行个数,根据主机配置选择合适的,默认是1个,多个可以加快备份速度。
恢复备份文件要保证MySQL datadir文件为空,否则会报错
service mysql stop #停止MySQL
mv /var/lib/mysql/ /var/lib/mysql_bak #备份原mysql 的datadir
mkdir /var/lib/mysql
如果加了压缩,需要解压
mkdir /backup/20160217_114521
tar -ixvf /backup/20160217_114521.tar.gz -C /backup/20160217_114521
innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log /backup/20160217_114521
innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /backup/20160217_114521
chown mysql: /var/lib/mysql -R
service mysql start
增量备份需要基于全备,先假设我们已经有了一个全备(/backup/20160217_114521),在该全表的基础上做增量备份。
第一次在数据库插入一条数据,在全备上做增量备份
innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=123123 --incremental-basedir=/backup/20160217_114521 --incremental /backup/
incremental-basedir=全备的目录
–incremental=增量备份的目录
第二次在数据库插入一条数据,在第一次增量备份上做增量备份
innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=123123 --incremental-basedir=/backup/2016-02-17_12-15-30 --incremental /backup/
incremental-basedir=在第一次增量备份的目录
--incremental=增量备份的目录
备份目录下,有一个文件xtrabackup_checkpoints记录着备份信息,全备的信息如下:
root@iZu1dc59z8tZ:/backup# cat 20160217_114521/xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0
to_lsn = 3233814
last_lsn = 3233814
compact = 0
root@iZu1dc59z8tZ:/backup# cat 2016-02-17_12-15-30/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 3233814
to_lsn = 3234430
last_lsn = 3234430
compact = 0
root@iZu1dc59z8tZ:/backup# cat 2016-02-17_12-1/xtrabackup_checkpoints
2016-02-17_12-15-30/ 2016-02-17_12-17-31/
root@iZu1dc59z8tZ:/backup# cat 2016-02-17_12-17-31/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 3234430
to_lsn = 3234537
last_lsn = 3234537
compact = 0
从上面可以看出,增量备份的from_lsn正好等于全备的to_lsn。
//测试恢复
service mysql stop #停止MySQL
mv /var/lib/mysql/ /var/lib/mysql_bak #备份原mysql 的datadir
mkdir /var/lib/mysql
innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log --redo-only /backup/20160217_114521
//第一次增量
innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log --redo-only /backup/20160217_114521 --incremental-dir=/backup/2016-02-17_12-15-30/
……
//最后一次增量
innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log /backup/20160217_114521 --incremental-dir=/backup/2016-02-17_12-17-31/
innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /backup/20160217_114521
chown mysql: /var/lib/mysql -R
service mysql start