码迷,mamicode.com
首页 > 数据库 > 详细

利用XtraBackup给MYSQL热备(基于数据文件)

时间:2014-11-24 16:41:31      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   使用   sp   for   文件   

利用XtraBackup给MYSQL热备(基于数据文件)

利用XtraBackupMYSQL热备(基于数据文件)
利用 XtraBackup 快速配置主从服务器
这XtraBackup 个主要包含2个命令 xtrabackup 和 innobackupex 第一个是他的主程序 另外个他自己封装的脚本一般用脚本来完成备份
这是工作笔记 比较凌乱 凑合着看吧

获取XtraBackup

获取XtraBackup(其中1.6里面有个WIN版本的 测试后很头疼各种BUG未解决 WIN下的MYSQL还是使用远程备份吧)

http://www.percona.com/downloads/XtraBackup/

1 # 例如我的环境是CENTOS 6X 64BIT的
2 mkdir -p $HOME/downloads;cd $HOME/downloads

 

 

安装

1 # 然后安装
2 # 安装前可能需要 yum -y install libaio perl-Time-HiRes
3 # 然后再。。。
4 # 查看安装包信息
5 rpm -qip percona-xtrabackup-2.1.5-680.rhel6.x86_64.rpm
6 # 安装
7 rpm -ivh percona-xtrabackup-2.1.5-680.rhel6.x86_64.rpm
8 # 拆卸
9 rpm -e percona-xtrabackup

 

1 # 否则可能出现以下提示
2 error: Failed dependencies:
3         libaio.so.1()(64bit) is needed by percona-xtrabackup-2.1.5-680.rhel6.x86_64
4         libaio.so.1(LIBAIO_0.1)(64bit) is needed by percona-xtrabackup-2.1.5-680.rhel6.x86_64
5         libaio.so.1(LIBAIO_0.4)(64bit) is needed by percona-xtrabackup-2.1.5-680.rhel6.x86_64
6         perl(Time::HiRes) is needed by percona-xtrabackup-2.1.5-680.rhel6.x86_64

 

1 # 好 这次安装顺利过去了
2 xtrabackup --version
3 xtrabackup version 2.1.5 for Percona Server 5.1.70 unknown-linux-gnu (x86_64) (revision id: 680)

 

1 #
2 开始备份(如果在MY.CNF里面配置了[XtraBackup]节的用户和密码就可以使用不带密码的方式备份)
3 BACKUP_DIR="/home/ftp/b/backup_data/wwwroot/2013/11/23/2"
4 mkdir -p $BACKUP_DIR
5 # xtrabackup --backup --datadir=Mysql源数据目录 --target-dir=Mysql备份数据存放目录
6 xtrabackup --backup --datadir=/var/lib/mysql/ --target-dir=$BACKUP_DIR
7 xtrabackup --backup --user=root --password=密码 --datadir=/var/lib/mysql/ --target-dir=/home/ftp/b/backup_data/wwwroot/2013/11/23/-4

 

1 innobackupex --user=root --password=密码 --defaults-file=/etc/my.cnf /home/ftp/b/backup_data/wwwroot/2013/11/23/2.1
2 # 我晕 真是折腾啊
3 innobackupex: Connected to MySQL server
4 innobackupex: Error: Support for MySQL 5.1 with builtin InnoDB (not the plugin) was removed in Percona XtraBackup 2.1. The last version to support MySQL 5.1 with builtin InnoDB was Percona XtraBackup 2.0.

 

1 # 拆卸
2 rpm -e percona-xtrabackup

 

1 # 重新下载
2 mkdir -p $HOME/downloads;cd $HOME/downloads
3 lftp -c pget -n 30 http://www.percona.com/redir/downloads/XtraBackup/XtraBackup-2.0.0/RPM/rhel6/x86_64/percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm

 

1 # 查看安装包信息
2 rpm -qip percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm

 

1 # 安装
2 rpm -ivh percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm
3 warning: percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
4 Preparing...                ########################################### [100%]
5    1:percona-xtrabackup     ########################################### [100%]
6 xtrabackup --version
7 xtrabackup version 2.0.0 for Percona Server 5.1.59 unknown-linux-gnu (x86_64) (revision id: 417)

 

测试备份

1 innobackupex --user=root --password=密码 --defaults-file=/etc/my.cnf /home/ftp/b/backup_data/wwwroot/

 

好 这下对鸟。。。

01 InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
02 and Percona Inc 2009-2012.  All Rights Reserved.
03  
04 This software is published under
05 the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.
06  
07 131123 09:22:25  innobackupex: Starting mysql with options:  --defaults-file=‘/etc/my.cnf‘ --password=xxxxxxxx --user=‘root‘ --unbuffered --
08 131123 09:22:25  innobackupex: Connected to database with mysql child process (pid=30774)
09 131123 09:22:31  innobackupex: Connection to database server closed
10 IMPORTANT: Please check that the backup run completes successfully.
11            At the end of a successful backup run innobackupex
12            prints "completed OK!".
13  
14 innobackupex: Using mysql  Ver 14.14 Distrib 5.1.69, for redhat-linux-gnu (x86_64) using readline 5.1
15 innobackupex: Using mysql server version Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
16  
17 innobackupex: Created backup directory /home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31
18 131123 09:22:31  innobackupex: Starting mysql with options:  --defaults-file=‘/etc/my.cnf‘ --password=xxxxxxxx --user=‘root‘ --unbuffered --
19 131123 09:22:31  innobackupex: Connected to database with mysql child process (pid=30841)
20 131123 09:22:33  innobackupex: Connection to database server closed
21  
22 131123 09:22:33  innobackupex: Starting ibbackup with command: xtrabackup_51  --defaults-file="/etc/my.cnf" --backup --suspend-at-end --target-dir=/home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31
23 innobackupex: Waiting for ibbackup (pid=30861) to suspend
24 innobackupex: Suspend file ‘/home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31/xtrabackup_suspended‘
25  
26 xtrabackup_51 version 2.0.0 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: 417)
27 xtrabackup: uses posix_fadvise().
28 xtrabackup: cd to /var/lib/mysql
29 xtrabackup: Target instance is assumed as followings.
30 xtrabackup:   innodb_data_home_dir = ./
31 xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend
32 xtrabackup:   innodb_log_group_home_dir = ./
33 xtrabackup:   innodb_log_files_in_group = 2
34 xtrabackup:   innodb_log_file_size = 5242880
35 >> log scanned up to (0 44243)
36 [01] Copying ./ibdata1 to /home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31/ibdata1
37 [01]        ...done
38  
39 131123 09:22:35  innobackupex: Continuing after ibbackup has suspended
40 131123 09:22:35  innobackupex: Starting mysql with options:  --defaults-file=‘/etc/my.cnf‘ --password=xxxxxxxx --user=‘root‘ --unbuffered --
41 131123 09:22:35  innobackupex: Connected to database with mysql child process (pid=30874)
42 131123 09:22:37  innobackupex: Starting to lock all tables...
43 >> log scanned up to (0 44243)
44 >> log scanned up to (0 44243)
45 131123 09:22:47  innobackupex: All tables locked and flushed to disk
46  
47 131123 09:22:47  innobackupex: Starting to backup .frm, .MRG, .MYD, .MYI,
48 innobackupex: .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files in
49 innobackupex: subdirectories of ‘/var/lib/mysql‘
50 innobackupex: Backing up files ‘/var/lib/mysql/blog/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (49 files)
51 innobackupex: Backing up file ‘/var/lib/mysql/info/db.opt‘
52 innobackupex: Backing up files ‘/var/lib/mysql/bd_2kdp_com/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (49 files)
53 >> log scanned up to (0 44243)
54 innobackupex: Backing up file ‘/var/lib/mysql/www_2kdp_com/db.opt‘
55 innobackupex: Backing up files ‘/var/lib/mysql/mysql/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (69 files)
56 innobackupex: Backing up file ‘/var/lib/mysql/clean_cache/db.opt‘
57 innobackupex: Backing up file ‘/var/lib/mysql/union/db.opt‘
58 innobackupex: Backing up files ‘/var/lib/mysql/movie/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (49 files)
59 innobackupex: Backing up file ‘/var/lib/mysql/link_2kdp/db.opt‘
60 innobackupex: Backing up files ‘/var/lib/mysql/silkime_com/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (295 files)
61 >> log scanned up to (0 44243)
62 innobackupex: Backing up file ‘/var/lib/mysql/copy_site/db.opt‘
63 innobackupex: Backing up files ‘/var/lib/mysql/sell_bdl/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (133 files)
64 131123 09:22:59  innobackupex: Finished backing up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt files
65  
66 innobackupex: Resuming ibbackup
67  
68 xtrabackup: The latest check point (for incremental): ‘0:44243‘
69 xtrabackup: Stopping log copying thread.
70 .>> log scanned up to (0 44243)
71 >> log scanned up to (0 44243)
72  
73 xtrabackup: Transaction log of lsn (0 44243) to (0 44243) was copied.
74 131123 09:23:01  innobackupex: All tables unlocked
75 131123 09:23:01  innobackupex: Connection to database server closed
76  
77 innobackupex: Backup created in directory ‘/home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31‘
78 # 这一句我特别喜欢 真的。。。折腾那么久 就为了这个。。。。。
79 innobackupex: MySQL binlog position: filename ‘mysql-bin.000001‘, position 25959
80 131123 09:23:01  innobackupex: completed OK!

 

01 # 现在我们cd到备份目录 看看搞了些什么.
02 # 除了数据库本身的文件外 发现还有 另外5个文件 有用的写下备注
03 backup-my.cnf # 这个是用作数据库数据文件指向到这个备份目录的时候用的 直接把里面的内容复制到系统的my.cnf
04 xtrabackup_binlog_info # 记录了备份完成时候的 二进制日志和 Position指针位置
05 xtrabackup_logfile
06 xtrabackup_binary
07 xtrabackup_checkpoints # 这个里面指明了备份类型backup_type 如 full-backuped 增量...
08 # 这个东西可以写个脚本来读取 从服务器那边那个做成自动配置
09 cat xtrabackup_binlog_info
10 mysql-bin.000001        25959

 

1 # 现在再来测试下压缩备份
2 # 一些比较有用的选项
3   # --slave-info(slave-info可以记录备份恢 复后,作为slave需要的一些信息,根据这些信息,可以很方便的利用备份来重做slave。)
4   # --no-lock(备份过程不锁定表)
5   # 2>$BACKUP_DIR/`date +%Y-%m-%d_%H-%M-%S`/mysqlbackup.log(备份过程中的输出信息保存,适合封装到自己的脚本里面去什么的)
6  
7 #TIME1=`date +%Y%m%d%H%M%S`;echo 勤劳的Jrobot:我在`date +%Y/%m/%d_%H:%M:%S`开始备份!
8 #echo "勤劳的Jrobot:我在`date +%Y/%m/%d_%H:%M:%S`开始备份!" >> /home/ftp/b/backup_data/`date +%Y-%m-%d_%H-%M-%S`/backup.log

 

1 BACKUP_DIR="/home/ftp/b/backup_data/wwwroot"
2 USER="用户"
3 PSWD="密码"
4 MY="/etc/my.cnf"
5 LOG="MySQL_Last_Backup.LOG"
6 LAST_GZIP_BACKUP="LASTBACKUP.TAR.GZ"
7 #mkdir -p $BACKUP_DIR/`date +%Y-%m-%d_%H-%M-%S`/

 

记录备份日志

1 innobackupex --user=$USER --password=$PSWD --slave-info --defaults-file=$MY $BACKUP_DIR/ 2>$BACKUP_DIR/$LOG

 

gzip压缩备份

1 innobackupex --user=$USER --password=$PSWD --slave-info --defaults-file=$MY $BACKUP_DIR/ --stream=tar gzip>$BACKUP_DIR/$LAST_GZIP_BACKUP

 

解压这个包看看或者直接SCP到从机

1 tar xvzfi LASTBACKUP.TAR.GZ
2 cat xtrabackup_binlog_info # 哈哈开心了吧

 

在从服务器上解压这个后 开始配置同步

1 #slave stop;
2 #CHANGE MASTER TO MASTER_HOST=‘主机地址‘,MASTER_USER=‘从复制账号‘,MASTER_PASSWORD=‘密码‘,MASTER_LOG_FILE=‘mysql-bin.00000X‘,MASTER_LOG_POS=1;
3 #slave start;

 

总结 这个东西的封装后的脚本是很强大 用来做主从配置也很快 但是有个问题 就是不支持远程备份。你比如说 你想从Win服务器备份到Linux的就蛋疼了。这个软件的主要文档和参考资料的环境几乎都是基于LINUX的。WIN下着实蛋疼。。。

利用XtraBackup给MYSQL热备(基于数据文件)

标签:blog   http   io   ar   os   使用   sp   for   文件   

原文地址:http://www.cnblogs.com/L-H-R-X-hehe/p/4118837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!