1 2 3 4 5 6 7 | 1、flush table with read locak; 2、create snapshot 3、show master status; show slave status; [可选] 4、unlock tables; 5、Copy files from the snapshot 6、Unmount the snapshot. 7、Remove snapshot |
1 2 3 4 5 6 7 8 | 快照备份: 1.迁移数据到逻辑卷(不是必须,视情况而定) 2.锁表(时间) 3.给数据库所在的逻辑卷拍快照 4.解锁 5.将快照挂载到临时挂载点上 6.将快照上的所有数据拷贝到相应的备份目录里(不同主机) 7.卸载快照并删除 |
1 2 3 4 | [root@Admin ~]# pvcreate /dev/sdb [root@Admin ~]# vgcreate vg01 /dev/sdb [root@Admin ~]# lvcreate -n lv_mysql -L 4G vg01 [root@Admin ~]# mkfs.ext4 /dev/mapper/vg01-lv_mysql |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1>先停止应用 2>停止mysql服务 [root@Admin ~]# service mysqld stop 3>备份所有的数据文件到指定的地方 [root@Admin ~]# tar -czvf /tmp/backmysql/mysql.tar.gz /data/DB/* 4>挂载逻辑卷到当前mysql的数据目录里 [root@Admin ~]# mount /dev/mapper/vg01-lv_mysql /data/DB/ 5>将刚刚备份的数据解压到数据目录里 [root@Admin ~]# tar xf /tmp/backmysql/mysql.tar.gz -C /data/DB/ [root@Admin ~]# mv /data/DB/data/DB/* /data/DB/ && rm -rf /data/DB/data/ 6>启动数据库 [root@Admin ~]# service mysqld start 此处启动失败原因/data/DB/数据目录的权限变成了root, 更改权限重新启动 [root@Admin ~]# chown mysql. -R /data/DB/ && service mysqld start |
1 | mysql> flush table with read lock; |
1 2 3 4 5 6 7 8 9 10 | [root@Admin ~]# lvcreate -n lv_mysql_s -L 50M -s /dev/vg01/lv_mysql [root@Admin ~]# dmsetup --tree ls vg01-lv_mysql (253:0) └─vg01-lv_mysql-real (253:1) └─ (8:16) vg01-lv_mysql_s (253:3) ├─vg01-lv_mysql_s-cow (253:2) │ └─ (8:16) └─vg01-lv_mysql-real (253:1) └─ (8:16) |
1 | [root@Admin ~]# unlock tables |
1 | [root@Admin ~]# echo "flush tables with read lock; system lvcreate -n lv_mysql_s -L 50M -s /dev/vg01/lv_mysql;unlock tables;" |mysql -p123 |
1 | [root@Admin ~]# mkdir /mnt/mysql && mount /dev/vg01/lv_mysql_s /mnt/mysql/ |
1 2 3 4 5 | [root@Admin ~]# ls /mnt/mysql/ # 可以看到新的挂载目录里面的数据 Admin.pid db01 ib_logfile0 mysql mysql-bin.000003 mysql-bin.000006 mysql-bin.000009 performance_schema auto.cnf db02 ib_logfile1 mysql-bin.000001 mysql-bin.000004 mysql-bin.000007 mysql-bin.000010 test binlog ibdata1 login mysql-bin.000002 mysql-bin.000005 mysql-bin.000008 mysql-bin.index [root@Admin ~]# mkdir /backup && rsync -av /mnt/mysql /backup |
1 | [root@Admin ~]# umount /mnt/mysql/ && lvremove /dev/vg01/lv_mysql_s |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 1>我们来点狠的,直接把mysql的数据目录/data/DB/删除。 [root@Admin ~]# rm -rf /data/DB/* && ls /data/DB/ 2>删除后可以看到重启mysql直接报错了 [root@Admin ~]# service mysqld restart MySQL server PID file could not be found! [失败] Starting MySQL...The server quit without updating PID file [失败]/DB/Admin.pid). 3>根据上面的/backup里面备份的数据进行恢复 [root@Admin ~]# mv /backup/mysql/* /data/DB/ [root@Admin ~]# ls /data/DB/ auto.cnf db02 ib_logfile1 mysql-bin.000001 mysql-bin.000004 mysql-bin.000007 mysql-bin.000010 test binlog ibdata1 login mysql-bin.000002 mysql-bin.000005 mysql-bin.000008 mysql-bin.index db01 ib_logfile0 mysql mysql-bin.000003 mysql-bin.000006 mysql-bin.000009 performance_schema 4>重新启动 [root@Admin ~]# chown mysql. /data/DB/ -R [root@Admin ~]# service mysqld restart 这里权限更改了如果启动还是报错的话,查看下是否mysql进程还存在,如果存在,将其kill掉再重启就OK [root@Admin ~]# mysql -p123 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | binlog | | db01 | | db02 | | login | | mysql | | performance_schema | | test | +--------------------+ rows in set (0.00 sec) |
1 2 3 4 5 6 7 8 9 10 | #!/bin/bash #LVM BackMysql back_dir=/backup/`date +%F` [ -d $back_dir ] mkdir -p $back_dir echo "flush tables with read lock; system lvcreate -n lv_mysql_s -L 50M -s /dev/vg01/lv_mysql;unlock tables;" |mysql -p123 mount /dev/vg01/lv_mysql_s /mnt/mysql/ rsync -a /mnt/mysql/ $back_dir if [ $? -eq 0 ];then umount /mnt/mysql/ && lvremove -f /dev/vg01/lv_mysql_s fi |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 正常安装MySQL: 1. 安装系统 2. 准备LVM,例如 /dev/vg_back/lv-mysql,mount /usr/local/mysql 3. 源码安装MySQL到 /usr/local/mysql 可选操作: 将现在的数据迁移到LVM 1. 准备lvm及文件系统 # lvcreate -L 2G -n lv-mysql vg_back # mkfs.ext4 /dev/vg_back/lv-mysql 2. 将数据迁移到LVM # service mysqld stop # mount /dev/vg_back/lv-mysql /u01/ //临时挂载点 # rsync -va /usr/local/mysql/ /u01/ //将MySQL原数据镜像到临时挂载点 # umount /u01/ # mount /dev/vg_back/lv-mysql /usr/local/mysql //加入fstab开机挂载 # df -Th /dev/mapper/vg_back-lv-mysql ext4 2.0G 274M 1.7G 15% /usr/local/mysql # service mysqld start 手动基于LVM快照实现备份: 1. 加锁 mysql> flush table with read lock; 2.创建快照 # lvcreate -L 500M -s -n lv-mysql-snap /dev/vg_back/lv-mysql # mysql -uroot -p123 -e 'show master status' > /backup/`date +%F`_position.txt 或者 mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | mysqld-bin.00003 | 135 | | | | +-------------------+----------+--------------+------------------+-------------------+ 3. 释放锁 mysql> unlock tables; 4. 从快照中备份 # mount -o ro /dev/vg_back/lv-mysql-snap /u01/ # mkdir /backup/`date +%F` # rsync -a /u01/ /backup/2015-07-02/ 5. 移除快照 # umount /u01/ # lvremove -f /dev/vg_back/lv-mysql-snap 脚本 + Cron完成: #!/bin/bash #LVM backmysql... back_dir=/backup/`date +%F` [ -d $back_dir ] || mkdir -p $back_dir mysql -uroot -p123 -e 'flush table with read lock' lvcreate -L 500M -s -n lv-mysql-snap /dev/vg_back/lv-mysql mysql -uroot -p123 -e 'show master status' |grep mysql > $back_dir/position.txt mysql -uroot -p123 -e 'flush logs' mysql -uroot -p123 -e 'unlock tables' mount -o ro /dev/vg_back/lv-mysql-snap /u01 rsync -a /u01/ $back_dir if [ $? -eq 0 ];then umount /u01/ lvremove -f /dev/vg_back/lv-mysql-snap fi =============================================================== mylvmbackup 功能:利用LVM快照实现物理备份,即LVM快照备份的自动版 安装perl模块 1. 在线安装 http://www.lenzg.net/mylvmbackup 它依赖于perl 模块,可用以下命令安装 perl -MCPAN -e 'install Config::IniFiles' 2. 离线安装 # rpm -ivh mylvmbackup-0.16-0.noarch.rpm warning: mylvmbackup-0.16-0.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID b27291f2: NOKEY error: Failed dependencies: perl(Config::IniFiles) is needed by mylvmbackup-0.16-0.noarch perl(Date::Format) is needed by mylvmbackup-0.16-0.noarch perl(File::Copy::Recursive) is needed by mylvmbackup-0.16-0.noarch 解决: # yum -y localinstall atrpms-77-1.noarch.rpm perl-File-Copy-Recursive-0.38-1.el6.rfx.noarch.rpm perl-IO-stringy-2.110-1.2.el6.rfx.noarch.rpm perl-Config-IniFiles-2.56-1.el6.rf.noarch.rpm 安装mylvmbackup软件包 # yum -y install mylvmbackup-0.15-0.noarch.rpm 解决依赖关系perl-TimeDate 备份方法一: # mylvmbackup --user=root --password=123 --host=localhost --mycnf=/etc/my.cnf --vgname=vg_back --lvname=lv-mysql --backuptype=tar --lvsize=100M --backupdir=/backup # tar xf backup-20140903_000236_mysql.tar.gz # ls backup backup-cnf-20150702_000236_mysql backup-20150702_000236_mysql.tar.gz backup-pos 备份方法二: # vim /etc/mylvmbackup.conf [mysql] #连接数据库配置 user=root password=123456 host=localhost port=3306 socket=/tmp/mysql.sock mycnf=/etc/my.cnf [lvm] #LVM逻辑卷的配置 vgname=vg_server #卷组名称 lvname=lv_mysql #逻辑卷名称 backuplv=mysql_snap #快照卷名称 lvsize=500M [fs] #文件系统配置 xfs=0 mountdir=/var/tmp/mylvmbackup/mnt/ #挂载目录 backupdir=/backup #备份目录,也可以备份到行程主机 [misc] #定义备份选项 backuptype=tar #定义备份的类型 backupretention=0 prefix=backup #定义备份文件名前缀 suffix=_mysql #定义备份文件名后缀 tararg=cvf #定义tar参数,默认为cvf tarfilesuffix=.tar.gz #定义备份文件后缀名格式 datefmt=%Y%m%d_%H%M%S #定义备份文件名时间戳格式 keep_snapshot=0 #是否保留snaphot keep_mount=0 #是否卸载snaphot quiet=0 #定义记录日志类型 注释:其他配置保持输入即可 然后直接执行mylvmbackup即可 mylvmbackup 参考示例 |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |