mmm_tools配置手册

目录
一、文档申明 
二、mmm_tools介绍 
三、预备知识 
四、环境需求 
五、演示环境 
六、配置mmm_tools.cnf 
七、工具演示 
mmm_clone: 
A、使用mmm_clone完成db2到db1的数据同步,并且完成master-master架构的搭建。 
mmm_backup: 
A、使用scp方法把db1的数据备份到本地的/mmm_backup中 
B、使用ssh-gz方法把db1的数据备份到本地的/mmm_backup中 
C、使用rdiff方法把db2的数据备份到本地的/mmm_backup中 
mmm_restore: 
A、使用scp拷贝模式还原数据库。 
B、还原rdiff的备份 
八、故障总结 


一、文档申明
 此文档并非官方文档,官方文档对mmm_tools的介绍介几乎为零,此文档为本人测试和使用得出的结论,如果文档中有错误的地方,希望多包涵,更希望你能发送邮件([email protected])告诉我哪里错了。本文档只是个人工作经验总结,由本文档引起的不良后果本人概不负责。
                                                              作者:andy.feng
      网名:fh.cn
Email:[email protected]
BLOG:http://linuxguest.blog.51cto.com
2011/7/8


二、mmm_tools介绍
 mmm_tools是mysql-mmm软件的一个附带工具箱,自带三个工具。
 mmm_clone //用来克隆一个mysql数据库到另外的机器,并且自动完成master-master或者master-slave的环境搭建,很好用。
 mmm_backup //用来备份一个mysql数据库,备份的时候使用了lvm快照技术,所以支持热备,并且锁表的时间相当的短,支持三种拷贝的方法,scp,ssh-gz,rdiff,下面会详细介绍。
 mmm_restare  //还原数据到一个mysql数据库,并完成master-master或者master-slave的环境搭建,不过还原还是建议手动操作,除非你对此工具很熟悉,能够熟练使用,这样才能降低操作带来的风险。

三、预备知识
mysql-mmm中文手册:http://linuxguest.blog.51cto.com/195664/578311
mysql-mmm安装手册:http://linuxguest.blog.51cto.com/195664/578307
lvm快照技术:http://asram.blog.51cto.com/1442164/313073
使用lvm快照备份mysql:http://linuxguest.blog.51cto.com/195664/585036
如果你对以上知识没有了解的话,请先阅读以上资料,并预先搭建好mysql-mmm架构,应为mmm_tools是依赖mysql-mmm架构工作的。

四、环境需求
一、 搭建好mysql-mmm环境
二、 安装好mysql数据库,所有服务器尽量使用相同的mysql版本
三、 mysql数据库软件放在lvm的逻辑卷上面(备份需要lvm快照功能),并且存放mysql数据库的逻辑卷组除了mysql数据库使用的空间外,还要剩余1-10G的空间用于创建快照,快照空间大小根据你数据库的大小和短时间的会改变的数据量大小来定,宁多勿少。
四、 安装rdiff-backup,rdiff-backup是支持增量备份的一种软件。在mysql-backup rdiff拷贝方法中会用到。
参考:http://wiki.rdiff-backup.org/wiki/index.php/Installations

五、演示环境
 我会通过展示我的mysql-mmm配置文件来展示我的演示环境。
 数据库版本:mysql-5.1.40,可以通过mysql-mmm安装文档找到下载地址。
 操作系统:RHEL5.4
 mysql-mmm:mysql-mmm-2.2.1
 
 由于是演示环境,所以我只用了两台服务器,和mysql-mmm安装手册中有一点差别,请大家注意。
IP地址 主机 角色
192.168.199.128 db1 mmm-monitor  mmm-agent
192.168.199.129 db2 mmm-agent
 其他配置参见我的mysql-mmm相关配置文件:
# cat mmm_common.conf
active_master_role      writer
<host default>
        cluster_interface               eth0
        pid_path                                /var/run/mmm_agentd.pid
        bin_path                                /usr/lib/mysql-mmm/
    replication_user        slave
    replication_password    slave
        agent_user                              mmm_agent
        agent_password                         mmm_agent
        mysql_port                              9188
</host>
<host db1>
        ip                                      192.168.199.128
        mode                                    master
        peer                                    db2
</host>
<host db2>
        ip                                      192.168.199.129
        mode                                    master
        peer                                    db1
</host>
<role writer>
        hosts                                   db1, db2
        ips                                     192.168.199.200
        mode                                   exclusive
</role>
<role reader>
        hosts                                   db1, db2
        ips                                     192.168.199.201,192.168.199.202
        mode                                   balanced
</role>

# cat mmm_mon.conf
include mmm_common.conf

<monitor>
        ip                                      192.168.199.128
        pid_path                                /var/run/mmm_mond.pid
        bin_path                                /usr/lib/mysql-mmm/
        status_path                             /var/lib/misc/mmm_mond.status
        ping_ips                                192.168.199.128,192.168.199.129
</monitor>

<host default>
        monitor_user                    mmm_monitor
        monitor_password                mmm_monitor
</host>

debug 0

六、配置mmm_tools.cnf
每个服务器的mmm_tools.cnf可以不一样,具体配置根据本级环境而定。
配置如下:
include mmm_agent.conf //导入agent和common的配置
default_copy_method                     scp  //默认选择copy的方法,从下面定义的中间选择一个(scp,rdiff,ssh-gz),稍后我们挨个测试。
clone_dirs                              var  //逻辑卷中需要备份的目录,clone的源目录,restore的目标目录,使用逻辑卷挂载的相对路径。比如我的逻辑卷挂载到/usr/local/mysql/这个目录,那么我们要备份的数据库文件放在/usr/local/mysql/var/这个目录中,这里我们就写var就行了。
<host default>
    ssh_user                    root //ssh使用的用户名,如果有证书,就可以直接拷贝,没有制作证书就会交互的让你输入密码。
    lvm_snapshot_size           1G  //快照的撤销空间大小,也就是lvcreate -L参数的大小,具体请研究lvcreate命令,根据你的实际情况来填写此项。
    lvm_logical_volume          lvmdb //前主机环境要备份文件所在的逻辑卷
    lvm_volume_group            myvg //前主机环境要备份文件所在的逻辑卷组
    lvm_mount_dir                       /mmm_snapshot //创建lvm快照后的临时挂载点,可以随意填写,填写后,自己创建此目录
    lvm_mount_opts                      -o rw //挂载lvm快照的参数,修改成这样就行了,默认的参数存在问题。

        tools_user                              mmm_tools //mmm_tools使用的一个用户,要求有super,replication client,reload权限。
        tools_password                          mmm_tools //mmm_tools用户的密码.

        backup_dir                              /mmm_backup //mmm_backup备份出的文件保存路径,mmm_restore还原时使用的源数据目录,如果此路径中已经有备份存在的话,mmm_backup时就会要求你重新写一个路径,rdiff拷贝方式可以一直把备份存在一个目录里,填写后创建此目录。
        restore_dir                             /mysql //使用mmm_restore命令,数据会被还原到此目录,本来这个目录应该写/usr/local/mysql/,但是为了避免误操作,还原覆盖了有用数据,所以就写到其他目录。
</host>

<copy_method scp> // default_copy_method调用的拷贝的方法,除了这里配置默认的,在使用命令的时候可以单独指定。scp方法就是普通的scp命令,如果你ssh端口修改过的话 –P后面写上你修改过的端口就行了。
    backup_command scp -P22 -c blowfish -r %SSH_USER%@%IP%:%SNAPSHOT%/%CLONE_DIR% %DEST_DIR%/
    restore_command cp -axv %BACKUP_DIR%/* %DEST_DIR%/
        true_copy 1
</copy_method>

<copy_method rdiff> //如果想使用这个拷贝方法,需要安装rdiff-backup工具,请参考上面给出的wiki地址安装和使用。
        backup_command rdiff-backup --ssh-no-compression -v 5 !--include %SNAPSHOT%/%CLONE_DIR%! --exclude %SNAPSHOT% %SSH_USER%@%IP%::%SNAPSHOT%/ %DEST_DIR%/
        restore_command rdiff-backup --force -v 5 -r %VERSION% %BACKUP_DIR% %DEST_DIR%/.mmm_restore; cp -axvl --remove-destination %DEST_DIR%/.mmm_restore/*  %DEST_DIR%/; rm -r
%DEST_DIR%/.mmm_restore/
        incremental_command rdiff-backup --parsable-output -l %BACKUP_DIR%
        single_run 1
        incremental 1
</copy_method>

<copy_method ssh-gz> //使用gzip压缩后在使用scp拷贝
        backup_command ssh -P35555 -c blowfish %SSH_USER%@%IP% "cd '%SNAPSHOT%'; tar cv !'%CLONE_DIR%'!" | gzip > %DEST_DIR%/backup.tar.gz
        restore_command cd %DEST_DIR%; tar xzfv %BACKUP_DIR%/backup.tar.gz
        single_run 1
</copy_method>

至此mmm-tools.cnf配置文件讲解完毕。

七、工具演示
mmm_clone:
 用法:
 Usage: /usr/sbin/mmm_clone [--config <config file>] --host <host> --clone-mode <mode> [--copy-method <copy method>] [--dest-dir <dir>]
Where:
  host       : db1 | db2
  clone-mode : master-master | master-slave | slave-slave
  copy-method: scp (default: ssh-gz)
  dest-dir   : directory where data should be cloned to
 要求:
1、mysql-mmm已经安装好,并启动了。
 2、db1和db2的数据库软件已经安装好,包括my.cnf已经配置好(具体配置和注意事项请参照mysql-mmm安装手册)。
 3、将要使用的数据导入到db2,并且把db1和db2需要的授权的用户都在db2上建立好(建立的时候注意可以访问的主机的限制)。
 完成以上任务后,启动db2。把db1的/usr/local/mysql/var/目录清空。
 db1:
 # pwd && ls
/usr/local/mysql/var
# mmm_control show
  db1(192.168.199.128) master/HARD_OFFLINE. Roles:
  db2(192.168.199.129) master/ONLINE. Roles: reader(192.168.199.201), reader(192.168.199.202), writer(192.168.199.200)
A、现在我们使用mmm_clone完成db2到db1的数据同步,并且完成master-master架构的搭建。
db1:
# mmm_clone --host db2  --copy-method scp  --clone-mode master-master --dest-dir /usr/local/mysql/
INFO: Checking destination directory '/usr/local/mysql/'...
INFO: Directory is ok
 Source host                    : db2
 Destination dir                : /usr/local/mysql/
 Dirs to clone                  : var
 Clone mode                     : master-master
 Copy method                    : scp
 Replication peer               : db2
 Setup master-master replication: yes
 Dry run                        : no
WARN: MySQL is not running now, skipping shutdown ...
INFO: Verifying ssh connection to remote host '[email protected]' (command: ssh  -p 22 [email protected] date)...
[email protected]'s password:
2011年 07月 09日 星期六 22:01:13 CST
INFO: OK: SSH connection works fine!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/create_snapshot
[email protected]'s password:
File descriptor 4 (socket:[173814]) leaked on lvcreate invocation. Parent PID 11003: perl
  Logical volume "mmm_snapshot" created
OK: Snapshot created!
INFO: Copying 'var' from snapshot on host 'db2' with copy method 'scp'
INFO: Executing command scp -P22 -c blowfish -r [email protected]:/mmm_snapshot/var /usr/local/mysql//
[email protected]'s password:
host.frm                                                                                                                 100% 9510     9.3KB/s   00:00   
user.frm                                                                                                                 100%   10KB  10.2KB/s   00:00   
procs_priv.frm                                                                                                           100% 8875     8.7KB/s   00:00   
time_zone.MYD                                                                                                            100%    0     0.0KB/s   00:00   
省略。。。。。。。。。。。。。。。。   
rdiff.MYD                                                                                                                100%   20     0.0KB/s   00:00   
rdiff.frm                                                                                                                100% 8590     8.4KB/s   00:00   
t.MYI                                                                                                                    100% 2048     2.0KB/s   00:00   
mysql-bin.000002                                                                                                         100%  125     0.1KB/s   00:00   
mysql-bin.000003                                                                                                         100%  724     0.7KB/s   00:00   
mysql-bin.000001                                                                                                         100%  149     0.2KB/s   00:00   
mysql-bin.index                                                                                                          100%   95     0.1KB/s   00:00   
test2.example.com.pid                                                                                                    100%    5     0.0KB/s   00:00   
mysql-bin.000005                                                                                                         100%  106     0.1KB/s   00:00   
mysql-bin.000004                                                                                                         100%  350     0.3KB/s   00:00   
INFO: Copied directory var!
INFO: Copying '_mmm' from snapshot on host 'db2' with copy method 'scp'
INFO: Executing command scp -P22 -c blowfish -r [email protected]:/mmm_snapshot/_mmm /usr/local/mysql//
[email protected]'s password:
status.txt                                                                                                               100%  315     0.3KB/s   00:00   
my.cnf                                                                                                                   100% 5569     5.4KB/s   00:00   
INFO: Copied directory _mmm!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/remove_snapshot
[email protected]'s password:
  Logical volume "mmm_snapshot" successfully removed
OK: Snapshot removed!
INFO: Cleaning dump from master.info and binary logs...
INFO: Deleting master binary logs: mysql-bin.*
removed `/usr/local/mysql/var/mysql-bin.000001'
removed `/usr/local/mysql/var/mysql-bin.index'
removed `/usr/local/mysql/var/mysql-bin.000002'
removed `/usr/local/mysql/var/mysql-bin.000004'
removed `/usr/local/mysql/var/mysql-bin.000005'
removed `/usr/local/mysql/var/mysql-bin.000003'
INFO: Deleting .info and .pid  files...
removed `/usr/local/mysql/var/test2.example.com.pid'
INFO: Changing permissions on mysql data dir...
INFO: MySQL is not running. Going to start it...
Starting MySQL...                                          [  OK  ]
INFO: MySQL has been started!
INFO: Changing master of host db1 to 192.168.199.129 ...
INFO: Successfully changed master.
INFO: Changing master of host db2 to 192.168.199.128 ...
INFO: Successfully changed master.
INFO: Clone operation finished!

 

检查结果:
[root@andy var]# mmm_control show
  db1(192.168.199.128) master/AWAITING_RECOVERY. Roles:
  db2(192.168.199.129) master/ONLINE. Roles: reader(192.168.199.201), reader(192.168.199.202), writer(192.168.199.200)

[root@andy var]# mmm_control set_online db1
OK: State of 'db1' changed to ONLINE. Now you can wait some time and check its new roles!
[root@andy var]# mmm_control show
  db1(192.168.199.128) master/ONLINE. Roles: reader(192.168.199.202)
  db2(192.168.199.129) master/ONLINE. Roles: reader(192.168.199.201), writer(192.168.199.200)

[root@andy var]# mmm_control checks all
db2  ping         [last change: 2011/07/03 02:10:35]  OK
db2  mysql        [last change: 2011/07/09 21:48:05]  OK
db2  rep_threads  [last change: 2011/07/09 22:01:22]  OK
db2  rep_backlog  [last change: 2011/07/09 22:01:22]  OK: Backlog is null
db1  ping         [last change: 2011/07/02 18:27:51]  OK
db1  mysql        [last change: 2011/07/09 22:01:22]  OK
db1  rep_threads  [last change: 2011/07/09 22:01:22]  OK
db1  rep_backlog  [last change: 2011/07/02 18:27:51]  OK: Backlog is null
当然,你还可以登陆到每台机器,使用 show slave status\G来详细检查。
呵呵,是不是很爽呢。
mmm_backup:
 基于上面搭建的环境,我们来演示三种copy方法。
 用法:
 Usage: /usr/sbin/mmm_backup [--config <config file>] --host <host> [--copy-method <copy method>] [--dest-dir <dir>]
Where:
  host       : db1 | db2
  copy-method: rdiff | scp | ssh-gz (default: ssh-gz)
  dest-dir   : directory where data should be backed up to
A、使用scp方法把db1的数据备份到本地的/mmm_backup中。
[root@andy ~]# mmm_backup --host db1 --copy-method scp --dest-dir /mmm_backup/
INFO: Checking local destination directory '/mmm_backup/'...
INFO: Directory is ok
INFO: Verifying ssh connection to remote host '[email protected]' (command: ssh  -p 22 [email protected] date)...
[email protected]'s password:
Sat Jul  9 22:10:05 CST 2011
INFO: OK: SSH connection works fine!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/create_snapshot
[email protected]'s password:
File descriptor 4 (socket:[376105]) leaked on lvcreate invocation. Parent PID 21921: perl
  Logical volume "mmm_snapshot" created
OK: Snapshot created!
INFO: Copying 'var' from snapshot on host 'db1' with copy method 'scp'
INFO: Executing command scp -P22 -c blowfish -r [email protected]:/mmm_snapshot/var /mmm_backup//
[email protected]'s password:
andy-relay-bin.000002                                                                                                    100%  251     0.3KB/s   00:00   
master.info                                                                                                              100%   69     0.1KB/s   00:00   
mysql-bin.000001                                                                                                         100%  106     0.1KB/s   00:01   
andy.example.com.pid                                                                                                     100%    6     0.0KB/s   00:00   
t.MYD                                                                                                                    100%  120     0.1KB/s   00:00   
rdiff.MYI                                                                                                                100% 1024     1.0KB/s   00:00   
rdiff.frm                                                                                                                100% 8590     8.4KB/s   00:00   
t.frm                                                                                                                    100% 8586     8.4KB/s   00:00   
t.MYI                                                                                                                    100% 2048     2.0KB/s   00:00   
省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
columns_priv.MYI                                                                                                         100% 4096     4.0KB/s   00:00   
general_log.frm                                                                                                          100% 8776     8.6KB/s   00:00   
slow_log.CSM                                                                                                             100%   35     0.0KB/s   00:00   
ndb_binlog_index.MYI                                                                                                     100% 1024     1.0KB/s   00:00   
time_zone_leap_second.MYI                                                                                                100% 1024     1.0KB/s   00:00   
relay-log.info                                                                                                           100%   49     0.1KB/s   00:00   
andy-relay-bin.index                                                                                                     100%   48     0.1KB/s   00:00   
INFO: Copied directory var!
INFO: Copying '_mmm' from snapshot on host 'db1' with copy method 'scp'
INFO: Executing command scp -P22 -c blowfish -r [email protected]:/mmm_snapshot/_mmm /mmm_backup//
[email protected]'s password:
status.txt                                                                                                               100% 2344     2.3KB/s   00:00   
my.cnf                                                                                                                   100% 5570     5.4KB/s   00:00   
copy_method.txt                                                                                                          100%    3     0.0KB/s   00:00   
INFO: Copied directory _mmm!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/remove_snapshot
[email protected]'s password:
  Logical volume "mmm_snapshot" successfully removed
OK: Snapshot removed!
[root@andy ~]# cd /mmm_backup/
[root@andy mmm_backup]# ls
_mmm  var
[root@andy mmm_backup]# ls /mmm_backup/var/
andy.example.com.pid   andy-relay-bin.000002  master.info  mysql-bin.000001  relay-log.info
andy-relay-bin.000001  andy-relay-bin.index   mysql        mysql-bin.index   test

B、使用ssh-gz方法把db1的数据备份到本地的/mmm_backup中。
注意,此时/mmm_backup中已经存在一个备份了,所以在备份到这个目录会不成功,我们可以重新指定一个目录,或者将此目录中备份删掉。
db1:
# cd /mmm_backup/
[root@andy mmm_backup]# ls
_mmm  var
[root@andy mmm_backup]# rm -rf *
[root@andy mmm_backup]# mmm_backup --host db1 --copy-method ssh-gz --dest-dir /mmm_backup/
INFO: Checking local destination directory '/mmm_backup/'...
INFO: Directory is ok
INFO: Verifying ssh connection to remote host '[email protected]' (command: ssh  -p 22 [email protected] date)...
[email protected]'s password:
Sat Jul  9 22:31:09 CST 2011
INFO: OK: SSH connection works fine!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/create_snapshot
[email protected]'s password:
File descriptor 4 (socket:[387068]) leaked on lvcreate invocation. Parent PID 23250: perl
  Logical volume "mmm_snapshot" created
OK: Snapshot created!
INFO: Copying files from snapshot on host 'db1' with copy method 'ssh-gz'
INFO: Executing command ssh -P22 -c blowfish [email protected] "cd '/mmm_snapshot'; tar cv  'var'" | gzip > /mmm_backup//backup.tar.gz
[email protected]'s password:
var/
var/andy-relay-bin.000002
var/master.info
var/mysql-bin.000001
var/andy.example.com.pid
var/test/
省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
var/mysql/ndb_binlog_index.MYI
var/mysql/time_zone_leap_second.MYI
var/relay-log.info
var/andy-relay-bin.index
INFO: Copied directories 'var' from host 'db1'!
INFO: Copying '_mmm' from snapshot on host 'db1' with copy method 'scp'
INFO: Executing command scp -P22 -c blowfish -r [email protected]:/mmm_snapshot/_mmm /mmm_backup//
[email protected]'s password:
status.txt                                                                                                               100% 2344     2.3KB/s   00:00   
my.cnf                                                                                                                   100% 5570     5.4KB/s   00:00   
copy_method.txt                                                                                                          100%    3     0.0KB/s   00:00   
INFO: Copied directory _mmm!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/remove_snapshot
[email protected]'s password:
  Logical volume "mmm_snapshot" successfully removed
OK: Snapshot removed!
[root@andy mmm_backup]# ls
backup.tar.gz  _mmm
这种备份优势相当于第一种来说,传说的数据量要少很多,备份的数据占用空间也会少很多,scp方法占用的空间会比ssh-gz压缩后多6倍左右。

C、使用rdiff方法把db2的数据备份到本地的/mmm_backup中。
使用词拷贝方法,记得要先安装rdiff-backup工具。
db1:
[root@andy mmm_backup]# mmm_backup --host db2 --copy-method rdiff --dest-dir /mmm_backup/      
INFO: Checking local destination directory '/mmm_backup/'...
INFO: Directory is ok
INFO: Verifying ssh connection to remote host '[email protected]' (command: ssh  -p 22 [email protected] date)...
[email protected]'s password:
2011年 07月 09日 星期六 22:38:31 CST
INFO: OK: SSH connection works fine!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/create_snapshot
[email protected]'s password:
File descriptor 4 (socket:[194118]) leaked on lvcreate invocation. Parent PID 15146: perl
  Logical volume "mmm_snapshot" created
OK: Snapshot created!
INFO: Copying files from snapshot on host 'db2' with copy method 'rdiff'
INFO: Executing command rdiff-backup --ssh-no-compression -v 5  --include /mmm_snapshot/var --exclude /mmm_snapshot [email protected]::/mmm_snapshot/ /mmm_backup//
Using rdiff-backup version 1.2.8
Executing ssh [email protected] rdiff-backup --server
[email protected]'s password:
Unable to import module xattr.
Extended attributes not supported on filesystem at /mmm_snapshot
Unable to import module posix1e from pylibacl package.
POSIX ACLs not supported on filesystem at /mmm_snapshot
Unable to import win32security module. Windows ACLs
not supported by filesystem at /mmm_snapshot
escape_dos_devices not required by filesystem at /mmm_snapshot
-----------------------------------------------------------------
Detected abilities for source (read only) file system:
  Access control lists                         Off
  Extended attributes                          Off
  Windows access control lists                 Off
  Case sensitivity                             On
  Escape DOS devices                           Off
  Escape trailing spaces                       Off
  Mac OS X style resource forks                Off
  Mac OS X Finder information                  Off
-----------------------------------------------------------------
Unable to import module xattr.
Extended attributes not supported on filesystem at /mmm_backup/rdiff-backup-data/rdiff-backup.tmp.0
Unable to import module posix1e from pylibacl package.
POSIX ACLs not supported on filesystem at /mmm_backup/rdiff-backup-data/rdiff-backup.tmp.0
Unable to import win32security module. Windows ACLs
not supported by filesystem at /mmm_backup/rdiff-backup-data/rdiff-backup.tmp.0
escape_dos_devices not required by filesystem at /mmm_backup/rdiff-backup-data/rdiff-backup.tmp.0
-----------------------------------------------------------------
Detected abilities for destination (read/write) file system:
  Ownership changing                           On
  Hard linking                                 On
  fsync() directories                          On
  Directory inc permissions                    On
  High-bit permissions                         On
  Symlink permissions                          Off
  Extended filenames                           On
  Windows reserved filenames                   Off
  Access control lists                         Off
  Extended attributes                          Off
  Windows access control lists                 Off
  Case sensitivity                             On
  Escape DOS devices                           Off
  Escape trailing spaces                       Off
  Mac OS X style resource forks                Off
  Mac OS X Finder information                  Off
-----------------------------------------------------------------
Backup: must_escape_dos_devices = 0
Starting mirror /mmm_snapshot to /mmm_backup
Processing changed file .
Processing changed file var
Processing changed file var/master.info
Processing changed file var/mysql
Processing changed file var/mysql/columns_priv.MYD
省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Processing changed file var/test2.example.com.pid
INFO: Copied directories 'var' from host 'db2'!
INFO: Copying '_mmm' from snapshot on host 'db2' with copy method 'scp'
INFO: Executing command scp -P22 -c blowfish -r [email protected]:/mmm_snapshot/_mmm /mmm_backup//
[email protected]'s password:
status.txt                                                                                                               100% 2345     2.3KB/s   00:00   
my.cnf                                                                                                                   100% 5569     5.4KB/s   00:00   
INFO: Copied directory _mmm!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/remove_snapshot
[email protected]'s password:
  Logical volume "mmm_snapshot" successfully removed
OK: Snapshot removed!

在db2上更新一下test.t表,创建t1表,然后再备份一次。
mysql> insert into t(name) values ('example');
mysql> create table t1  like t;

[root@andy mmm_backup]# mmm_backup --host db2 --copy-method rdiff --dest-dir /mmm_backup/
INFO: Checking local destination directory '/mmm_backup/'...
INFO: Directory is ok
INFO: Verifying ssh connection to remote host '[email protected]' (command: ssh  -p 22 [email protected] date)...
[email protected]'s password:
2011年 07月 09日 星期六 22:43:47 CST
INFO: OK: SSH connection works fine!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/create_snapshot
[email protected]'s password:
File descriptor 4 (socket:[197475]) leaked on lvcreate invocation. Parent PID 15945: perl
  Logical volume "mmm_snapshot" created
OK: Snapshot created!
INFO: Copying files from snapshot on host 'db2' with copy method 'rdiff'
INFO: Executing command rdiff-backup --ssh-no-compression -v 5  --include /mmm_snapshot/var --exclude /mmm_snapshot [email protected]::/mmm_snapshot/ /mmm_backup//
Using rdiff-backup version 1.2.8
Executing ssh [email protected] rdiff-backup --server
[email protected]'s password:
Unable to import module xattr.
Extended attributes not supported on filesystem at /mmm_snapshot
Unable to import module posix1e from pylibacl package.
POSIX ACLs not supported on filesystem at /mmm_snapshot
Unable to import win32security module. Windows ACLs
not supported by filesystem at /mmm_snapshot
escape_dos_devices not required by filesystem at /mmm_snapshot
-----------------------------------------------------------------
Detected abilities for source (read only) file system:
  Access control lists                         Off
  Extended attributes                          Off
  Windows access control lists                 Off
  Case sensitivity                             On
  Escape DOS devices                           Off
  Escape trailing spaces                       Off
  Mac OS X style resource forks                Off
  Mac OS X Finder information                  Off
-----------------------------------------------------------------
Unable to import module xattr.
Extended attributes not supported on filesystem at /mmm_backup/rdiff-backup-data/rdiff-backup.tmp.0
Unable to import module posix1e from pylibacl package.
POSIX ACLs not supported on filesystem at /mmm_backup/rdiff-backup-data/rdiff-backup.tmp.0
Unable to import win32security module. Windows ACLs
not supported by filesystem at /mmm_backup/rdiff-backup-data/rdiff-backup.tmp.0
escape_dos_devices not required by filesystem at /mmm_backup/rdiff-backup-data/rdiff-backup.tmp.0
-----------------------------------------------------------------
Detected abilities for destination (read/write) file system:
  Ownership changing                           On
  Hard linking                                 On
  fsync() directories                          On
  Directory inc permissions                    On
  High-bit permissions                         On
  Symlink permissions                          Off
  Extended filenames                           On
  Windows reserved filenames                   Off
  Access control lists                         Off
  Extended attributes                          Off
  Windows access control lists                 Off
  Case sensitivity                             On
  Escape DOS devices                           Off
  Escape trailing spaces                       Off
  Mac OS X style resource forks                Off
  Mac OS X Finder information                  Off
-----------------------------------------------------------------
Backup: must_escape_dos_devices = 0
Starting increment operation /mmm_snapshot to /mmm_backup
Processing changed file .
Incrementing mirror file /mmm_backup
Processing changed file var
Incrementing mirror file /mmm_backup/var
Processing changed file var/master.info
Incrementing mirror file /mmm_backup/var/master.info
Processing changed file var/mysql-bin.000005
Incrementing mirror file /mmm_backup/var/mysql-bin.000005
Processing changed file var/relay-log.info
Incrementing mirror file /mmm_backup/var/relay-log.info
Processing changed file var/test
Incrementing mirror file /mmm_backup/var/test
Processing changed file var/test/t.MYD
Incrementing mirror file /mmm_backup/var/test/t.MYD
Processing changed file var/test/t.MYI
Incrementing mirror file /mmm_backup/var/test/t.MYI
Processing changed file var/test/t1.MYD
Incrementing mirror file /mmm_backup/var/test/t1.MYD
Processing changed file var/test/t1.MYI
Incrementing mirror file /mmm_backup/var/test/t1.MYI
Processing changed file var/test/t1.frm
Incrementing mirror file /mmm_backup/var/test/t1.frm
Processing changed file var/test2-relay-bin.000002
Incrementing mirror file /mmm_backup/var/test2-relay-bin.000002
INFO: Copied directories 'var' from host 'db2'!
INFO: Copying '_mmm' from snapshot on host 'db2' with copy method 'scp'
INFO: Executing command scp -P22 -c blowfish -r [email protected]:/mmm_snapshot/_mmm /mmm_backup//
[email protected]'s password:
status.txt                                                                                                               100% 2345     2.3KB/s   00:00   
my.cnf                                                                                                                   100% 5569     5.4KB/s   00:00   
INFO: Copied directory _mmm!
INFO: ssh  -p 22 [email protected] /usr/lib/mysql-mmm//tools/remove_snapshot
[email protected]'s password:
  Logical volume "mmm_snapshot" successfully removed
OK: Snapshot removed!
从以上内容来看,rdiff-backup是以文件为单位来做增量备份的,对于那些很大的表,这种增量备份就没太大意义了。
mmm_restore:
还原也分三种拷贝模式,scp和ssh-gz差不多,rdiff属于增量备份,可以选择还原的版本号。这里我们就只演示scp和rdiff两种还原。
用法:
Usage: /usr/sbin/mmm_restore [--config <config file>] [--mode <mode>] [--version <version | list>] [--src-dir <dir>] [--dest-dir <dir>] [--dry-run]
Where:
  src-dir : directory where backup resides
  dest-dir: directory where backup should be restored to
  mode    : data-only, single-single, slave-single, master-single, master-slave, slave-slave
  version :
    - when run with 'list' parameter, displays available versions of incremental backups
    - if version is specified, tries to restore backup for specified version
  dry-run : check everything and exit without any changes
从上面可以看出,mmm_restore使用的—src-dir和—dest-dir,而不是—host。
A、使用scp拷贝模式还原数据库。
现在我们模仿db1数据库文件丢失,需要还原数据的场景。首先手工删除test数据文件。
db1:
[root@andy var]# cd /usr/local/mysql/var/ && rm -rf test/
[root@andy var]# ls
andy.example.com.pid   andy-relay-bin.000002  master.info  mysql-bin.000001  relay-log.info
andy-relay-bin.000001  andy-relay-bin.index   mysql        mysql-bin.index
还原数据:
[root@andy var]# mmm_restore
INFO: Checking restore source directory '/mmm_backup'...
INFO: Directory is ok
 Source dir            : /mmm_backup
 Destination dir       : /mysql
 Dirs to restore       : var
 Restore mode          : single-single
 Skip mysqld operations: no
 Dry run               : no
WARN: MySQL is not running now, skipping shutdown ...
INFO: Checking destination directory '/mysql'...
INFO: Directory is ok
INFO: Executing command cp -axv /mmm_backup/* /mysql/
`/mmm_backup/_mmm' -> `/mysql/_mmm'
`/mmm_backup/_mmm/copy_method.txt' -> `/mysql/_mmm/copy_method.txt'
`/mmm_backup/_mmm/status.txt' -> `/mysql/_mmm/status.txt'
`/mmm_backup/_mmm/my.cnf' -> `/mysql/_mmm/my.cnf'
`/mmm_backup/var/relay-log.info' -> `/mysql/var/relay-log.info'
`/mmm_backup/var/andy-relay-bin.000001' -> `/mysql/var/andy-relay-bin.000001'
`/mmm_backup/var/andy.example.com.pid' -> `/mysql/var/andy.example.com.pid'
`/mmm_backup/var/mysql/time_zone_transition_type.frm' -> `/mysql/var/mysql/time_zone_transition_type.frm'
省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
`/mmm_backup/var/test/t.MYD' -> `/mysql/var/test/t.MYD'
`/mmm_backup/var/master.info' -> `/mysql/var/master.info'
`/mmm_backup/var/andy-relay-bin.index' -> `/mysql/var/andy-relay-bin.index'
`/mmm_backup/var/mysql-bin.000001' -> `/mysql/var/mysql-bin.000001'
INFO: Restored backup from '/mmm_backup' to '/mysql'
INFO: Cleaning dump from master.info and binary logs...
INFO: Deleting master binary logs: mysql-bin.*
removed `/mysql/var/mysql-bin.index'
removed `/mysql/var/mysql-bin.000001'
INFO: Deleting relay binary logs: andy-relay-bin.*
removed `/mysql/var/andy-relay-bin.000001'
removed `/mysql/var/andy-relay-bin.000002'
removed `/mysql/var/andy-relay-bin.index'
INFO: Deleting .info and .pid  files...
removed `/mysql/var/master.info'
removed `/mysql/var/relay-log.info'
removed `/mysql/var/andy.example.com.pid'
INFO: Changing permissions on mysql data dir...
INFO: MySQL is not running. Going to start it...
Starting MySQL                                             [  OK  ]
INFO: MySQL has been started!
INFO: Skipping replication setup because destination configuration is 'single'
INFO: Restore operation finished!
[root@andy var]# ls /mysql
_mmm  var

这里并没有演示完整还原,因为还原或导致master-master复制失败,还原完成,db1已经自动把master转向db2,但是db2对db1的复制失败了,应为db1的bin-log发生了变化,需要手工恢复master-master复制。
如果真实情况master数据丢失需要还原的话,就需要先resote到master,然后再mmm_clone生成复制架构。如果只是slave损坏,没必要还原,直接mmm_clone过来就可以了。

B、还原rdiff的备份
[root@andy ~]# mmm_restore  --mode master-slave --version list --src-dir /mmm_backup/ --dest-dir /usr/local/mysql/ --dry-run                               INFO: Checking restore source directory '/mmm_backup/'...
INFO: Directory is ok
Following backup versions are available:
     Version | Date
-------------|---------------------------
  1310222305 | Sat Jul  9 22:38:25 2011
  1310222616 | Sat Jul  9 22:43:36 2011

[root@andy ~]# mmm_restore  --mode master-slave --version 1310222616 --src-dir /mmm_backup/ --dest-dir /usr/local/mysql/ --dry-run   
INFO: Checking restore source directory '/mmm_backup/'...
INFO: Directory is ok
 Source dir            : /mmm_backup/
 Destination dir       : /usr/local/mysql/
 Dirs to restore       : var
 Restore mode          : master-slave
 Incremental version   : 1310222616
 Replication peer      : db2
 Skip mysqld operations: no
 Dry run               : yes

--version //选择version编号或者使用—list显示可用的编号。
--dry-run //测试是否可以运行,不真正运行。
使用还原前关掉mysql数据库,貌似mmm_tools存在一个bug,关于还原前和clone前关闭数据库的命令。
[root@andy var]# mmm_restore  --mode master-slave --version 1310222616 --src-dir /mmm_backup/ --dest-dir /usr/local/mysql/
INFO: Checking restore source directory '/mmm_backup/'...
INFO: Directory is ok
 Source dir            : /mmm_backup/
 Destination dir       : /usr/local/mysql/
 Dirs to restore       : var
 Restore mode          : master-slave
 Incremental version   : 1310222616
 Replication peer      : db2
 Skip mysqld operations: no
 Dry run               : no
WARN: MySQL is not running now, skipping shutdown ...
INFO: Checking destination directory '/usr/local/mysql/'...
INFO: Directory is ok
INFO: Executing command rdiff-backup --force -v 5 -r 1310222616 /mmm_backup/ /usr/local/mysql//.mmm_restore; cp -axvl --remove-destination /usr/local/mysql//.mmm_restore/*  /usr/local/mysql//; rm -r /usr/local/mysql//.mmm_restore/
Using rdiff-backup version 1.2.8
Unable to import module xattr.
Extended attributes not supported on filesystem at /mmm_backup/rdiff-backup-data
Unable to import module posix1e from pylibacl package.
POSIX ACLs not supported on filesystem at /mmm_backup/rdiff-backup-data
Unable to import win32security module. Windows ACLs
not supported by filesystem at /mmm_backup/rdiff-backup-data
escape_dos_devices not required by filesystem at /mmm_backup/rdiff-backup-data
-----------------------------------------------------------------
Detected abilities for rdiff-backup repository (read only) file system:
  Access control lists                         Off
  Extended attributes                          Off
  Windows access control lists                 Off
  Case sensitivity                             On
  Escape DOS devices                           Off
  Escape trailing spaces                       Off
  Mac OS X style resource forks                Off
  Mac OS X Finder information                  Off
-----------------------------------------------------------------
Unable to import module xattr.
Extended attributes not supported on filesystem at /usr/local/mysql/.mmm_restore/rdiff-backup.tmp.0
Unable to import module posix1e from pylibacl package.
POSIX ACLs not supported on filesystem at /usr/local/mysql/.mmm_restore/rdiff-backup.tmp.0
Unable to import win32security module. Windows ACLs
not supported by filesystem at /usr/local/mysql/.mmm_restore/rdiff-backup.tmp.0
escape_dos_devices not required by filesystem at /usr/local/mysql/.mmm_restore/rdiff-backup.tmp.0
-----------------------------------------------------------------
Detected abilities for restore target (read/write) file system:
  Ownership changing                           On
  Hard linking                                 On
  fsync() directories                          On
  Directory inc permissions                    On
  High-bit permissions                         On
  Symlink permissions                          Off
  Extended filenames                           On
  Windows reserved filenames                   Off
  Access control lists                         Off
  Extended attributes                          Off
  Windows access control lists                 Off
  Case sensitivity                             On
  Escape DOS devices                           Off
  Escape trailing spaces                       Off
  Mac OS X style resource forks                Off
  Mac OS X Finder information                  Off
-----------------------------------------------------------------
Restore: must_escape_dos_devices = 0
Starting restore of /mmm_backup to /usr/local/mysql/.mmm_restore as it was as of Sat Jul  9 22:43:36 2011.
Processing changed file .
Processing changed file var
Processing changed file var/master.info
Processing changed file var/relay-log.info
省略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
`/usr/local/mysql//.mmm_restore/var/mysql-bin.000003' -> `/usr/local/mysql/var/mysql-bin.000003'
INFO: Restored backup version '1310222616' from '/mmm_backup/' to '/usr/local/mysql/'
INFO: Cleaning dump from master.info and binary logs...
INFO: Deleting master binary logs: mysql-bin.*
removed `/usr/local/mysql/var/mysql-bin.000001'
removed `/usr/local/mysql/var/mysql-bin.index'
removed `/usr/local/mysql/var/mysql-bin.000002'
removed `/usr/local/mysql/var/mysql-bin.000004'
removed `/usr/local/mysql/var/mysql-bin.000005'
removed `/usr/local/mysql/var/mysql-bin.000003'
INFO: Deleting relay binary logs: test2-relay-bin.*
removed `/usr/local/mysql/var/test2-relay-bin.000001'
removed `/usr/local/mysql/var/test2-relay-bin.index'
removed `/usr/local/mysql/var/test2-relay-bin.000002'
INFO: Deleting .info and .pid  files...
removed `/usr/local/mysql/var/master.info'
removed `/usr/local/mysql/var/relay-log.info'
removed `/usr/local/mysql/var/test2.example.com.pid'
INFO: Changing permissions on mysql data dir...
INFO: MySQL is not running. Going to start it...
Starting MySQL..                                           [  OK  ]
INFO: MySQL has been started!
INFO: Changing master of host db1 to 192.168.199.129 ...
INFO: Successfully changed master.
INFO: Restore operation finished!
还原完成,db1已经自动把master转向db2,但是db2对db1的复制失败了,应为db1的bin-log发生了变化,这个只有手工处理。
八、故障总结
 1、如果当命令执行到一半而由于配置错误退出时,快照已经生成,但是命令未执行完,导致快照没有删除,需要自己使用lvremove –f 快照名 删除快照。
 2、注意mmm_tools.cnf要针对所在主机环境配置,不是所有都一样。
 3、生产环境下,备份好以后,需要转移做测试,看备份是否可以用。还原最好手工操作,避免误伤。
 

你可能感兴趣的:(职场,休闲,mysql-mmm,mmm_tools)