当前位置: > 数据库 > MySQL >

LVM快照方式备份Mysql

时间:2014-11-30 15:46来源:linux.it.net.cn 作者:IT

目录:
 

第一章 为什么要用LVM的快照方式备份Mysql

 

第二章 如何使用LVM快照方式备份Mysql

 

(一)先布个场景--创建LVM
(二)给Mysql拍个照--快照备份
(三)自动化一些--Mylvmbackup搭配使用
(四)Crontab调用perl脚本,实现定时备份 

 

 

 

第三章 把数据库备份到网络磁盘

 

(一)测试环境
(二)远程机子创建网络硬盘
(三)映射网络硬盘
(四)Mylvmbackup把数据库备份到网络硬盘 

 

 

第四章 一些需要注意的问题

 

(一)创建LVM时分区的问题
(二)手动创建LVM快照区时必须要先锁定数据库,然后再创建
(三)创建了快照区后必须要文件打包放到安全的地方,否则在移除快照区后 再创建时之前的文件将不复存在
(四)crontab调用perl脚本的问题 

 

附录:
 

参考资料一:鸟哥的Linux私房菜<http://www.vbird.org>

参考资料二:Mysql中文网<http://imysql.cn/?q=node/102>
参考资料三:fedora wiki<http://fedoraproject.org/wiki/Scsi-target- utils_Quickstart_Guide#About>
 
 
 
第一章 为什么要用LVM的快照方式备份Mysql
 
Peter Zaitsev在其文章里详细的说明了为什么要用LVM快照的方式来为Mysql制作备份(文章链接:http://www.mysqlperformanceblog.com/2006/08/21/using-lvm-for-mysql-backup-and-replication-setup/)。他说,如果有人问他关于MySQL备份的建议,他会先问操作系统是否安装了LVM或者其他相似功能的软件。
之所以这样问,就是因为用LVM快照试试来备份Mysql有以下几个优点:
1、几乎是热备
在大多数情况下,可以在应用程序仍在运行的时候执行备份。它无需关闭服务,只需要 设置只读或者类似这样的限制。
2、支持所有基于本地磁盘的存储引擎 
它支持MyISAM, Innodb, BDB,还支持 Solid, PrimeXT 和 Falcon
3、 快速备份 
只需拷贝二进制格式的文件,在速度方面无以匹敌。
4、低开销 
只是文件拷贝,因此对服务器的开销很细微。
5、 容易保持完整性 
想要压缩备份文件吗?把它们备份到磁带上,FTP或者网络备份软件 – 十分简单,因为 只需要拷贝文件即可。
6、快速恢复 
恢复的时间和标准的MySQL崩溃恢复或数据拷贝回去那么快,甚至可能更快,将来会更 快。
7、 免费 
无需使用昂贵的商业软件,只需Innodb热备工具来执行备份。
 
当然,每个事物都有其两面性,它也存在一些缺点:
1、 很明显,系统需要支持快照。
2、 在一些公司里,系统管理员和DBA属于不同的团队。而使用快照需要系统root权限。因此,你可能需要做一些团队协调工作或者干脆在DBA Team里安插一个系统管理员。这种事在某些公司很容易,但也可能很麻烦。
3、 无法确切的预计服务停止时间。因为,这种方法到底什么时候算热备什么时候不算,完全取决于FLUSH TABLE WITH READ LOCK命令执行时间的长短。因此,建议在凌晨干这件事情。或者干脆定下一个维护时间段(比如某些网络游戏运营商的做法)。
4、 如果把日志放在独立的设备上或者数据分布在多个卷上,那么就比较麻烦了。因为这样一来就无法得到全部数据的一致性快照,这就是所谓的多卷上的数据问题。不过,有些系统可能自动做到多卷快照。
 
第二章 如何使用LVM快照方式备份Mysql
 
现在,来看看如何使用LVM的快照功能来制作MySQL备份。
当然,首先需要准备好相应的环境。配置一个LVM,并且划分合适大小的LV,并且将其挂载到MySQL的数据文件目录上。
(一)先布个场景--创建LVM
1、创建两个独立分区
[root@nfs-client ~]# fdisk /dev/sda
(步骤就省略了,/dev/sda9和/dev/sda10是我新建的两个分区,/dev/sda10则用做快照区)
2、扫描一下看有没PV在系统上
[root@nfs-client ~]# pvscan
No matching physical volumes found
3、开始创建PV
[root@nfs-client ~]# pvcreate /dev/sda{9,10}
Physical volume “/dev/sda9″ successfully created
Physical volume “/dev/sda10″ successfully created
4、再来就是创建VG
[root@nfs-client ~]# vgcreate -s 16M mysqlvg /dev/sda9
Volume group “mysqlvg” successfully created
5、查看有多少个PE
[root@nfs-client ~]# vgdisplay
— Volume group —
VG Name mysqlvg
…………
VG Size 27.94 GB
PE Size 16.00 MB
Total PE 1788
Alloc PE / Size 0 / 0
Free PE / Size 1788 / 27.94 GB
VG UUID o5SORg-1rGz-X5mV-eXXv-92ds-WbBX-8UnZzd
6、创建LV并将整个VG分配给LV
[root@nfs-client ~]# lvcreate -l 1788 -n mysqllv mysqlvg
Logical volume “mysqllv” created
7、格式化LV
[root@nfs-client ~]# mkfs.ext3 /dev/mysqlvg/mysqllv
8、挂载LV到Mysql目录
[root@nfs-client ~]# mount /dev/mysqlvg/mysqllv /var/lib/mysql/
(二)给Mysql拍个照--快照备份
1、先把/dev/sda10添加到VG
[root@nfs-client ~]# vgextend mysqlvg /dev/sda10
Volume group “mysqlvg” successfully extended
2、再来查看下VG的情况
[root@nfs-client ~]# vgdisplay
— Volume group —
VG Name mysqlvg
……
VG Size 65.19 GB
PE Size 16.00 MB
Total PE 4172
Alloc PE / Size 1788 / 27.94 GB
Free PE / Size 2384 / 37.25 GB
VG UUID o5SORg-1rGz-X5mV-eXXv-92ds-WbBX-8UnZzd
3、把数据库锁定先
mysql> flush tables with read lock;
3、用lvcreate建立快照区,把全部空闲的PE分配给快照区
[root@nfs-client ~]# lvcreate -l 2384 -s -n mysqlbak /dev/mysqlvg/mysqllv
Logical volume “mysqlbak” created
4、给快照区创建个挂载点
[root@nfs-client ~]# mkdir /mnt/snapshot
5、挂载快照区并查看结果
[root@nfs-client ~]# mount /dev/mysqlvg/mysqlbak /mnt/snapshot/ && ll /mnt/snapshot/
总计 21196
-rw-rw—- 1 mysql mysql 10485760 05-26 15:25 ibdata1
-rw-rw—- 1 mysql mysql 5242880 05-26 17:49 ib_logfile0
-rw-rw—- 1 mysql mysql 5242880 05-26 15:24 ib_logfile1
drwx—— 2 mysql mysql 4096 05-26 15:21 lost+found
drwx—— 2 mysql mysql 4096 05-26 15:24 mysql
-rw-rw—- 1 mysql mysql 15206 05-26 15:24 mysql-bin.000001
-rw-rw—- 1 mysql mysql 637054 05-26 15:24 mysql-bin.000002
-rw-rw—- 1 mysql mysql 337 05-26 15:25 mysql-bin.000003
-rw-rw—- 1 mysql mysql 98 05-26 15:25 mysql-bin.000004
-rw-rw—- 1 mysql mysql 98 05-26 15:48 mysql-bin.000005
-rw-rw—- 1 mysql mysql 98 05-26 17:49 mysql-bin.000006
-rw-rw—- 1 mysql mysql 114 05-26 17:49 mysql-bin.index
srwxrwxrwx 1 mysql mysql 0 05-26 17:49 mysql.sock
drwx—— 2 mysql mysql 4096 05-26 15:24 test
剩下的就是把这些文件打包起来放到一个安全的地方了。看看吧,不用1分钟就可以文件完全备份过来,LVM的快照功能很强大吧……
(三)自动化一些--Mylvmbackup搭配使用
利用LVM的快照功能来备份确实很方便,不过,看看操作过程,如果要重新建立快照的话应该怎么做呢?
1、需要卸载
[root@nfs-client ~]# umount /mnt/snapshot/
2、移除快照区
[root@nfs-client ~]# lvremove /dev/mysqlvg/mysqlbak
Do you really want to remove active logical volume mysqlbak? [y/n]: y
Logical volume “mysqlbak” successfully removed
3、再来拍照
[root@nfs-client ~]# lvcreate -l 2384 -s -n mysqlbak /dev/mysqlvg/mysqllv Logical volume “mysqlbak” created
4、继续挂载
[root@nfs-client ~]# mount /dev/mysqlvg/mysqlbak /mnt/snapshot/
然后就是重新打包文件,再放到一个安全的地方……这过程似乎有点烦人,有没有相对自动化一点的方法呢?
有!
Lenz Grimmer开发了一个叫做mylvmbackup的工具,可以让我们快速完成以上工作,可以在http://www.lenzg.net/mylvmbackup/下载到这个工具。
5、下载mylvmbackup软件包
[root@nfs-client ~]# wget http://www.lenzg.net/mylvmbackup/mylvmbackup-0.13.tar.gz
6、将其解压到/tmp
[root@nfs-client ~]# tar -zxvf mylvmbackup-0.13.tar.gz -C /tmp/
7、进行mylvmbackup的安装
[root@nfs-client ~]# cd /tmp/mylvmbackup-0.13/
[root@nfs-client mylvmbackup-0.13]# make install
PS:安装前可以先查看INSTALL文档
Mylvmbackup工具已经安装好了,接下来可以进行备份了吗?还没!
因为用mylvmbackup这个工具时是不需要自己创建LVM快照区的,所以,如果有创建了快照区的话还需要把快照区移除。
8、卸载后移除快照区
[root@nfs-client ~]# umount /mnt/snapshot
[root@nfs-client ~]# lvremove /dev/mysqlvg/mysqlbak
Do you really want to remove active logical volume mysqlbak? [y/n]: y
Logical volume “mysqlbak” successfully removed
9、现在就可以用mylvmbackup进行mysql数据库的备份
[root@nfs-client ~]# mylvmbackup –user=root password=qqing99 –mycnf=/etc/my.cnf –vgname=mysqlvg –lvname=mysqllv –backuptype=tar –lvsize=30G –backupdir=/home/bakup/
20110527 13:46:02 Info: Connecting to database…
20110527 13:46:02 Info: Flushing tables with read lock…
20110527 13:46:02 Info: Taking position record into /tmp/mylvmbackup-backup-20110527_134602_mysql-TJ74OT.pos…
20110527 13:46:02 Info: Running: lvcreate -s –size=30G –name=mysqllv_snapshot /dev/mysqlvg/mysqllv
File descriptor 4 (socket:[18447]) leaked on lvcreate invocation. Parent PID 4983: /usr/bin/perl
Logical volume “mysqllv_snapshot” created
20110527 13:46:02 Info: DONE: taking LVM snapshot
20110527 13:46:02 Info: Unlocking tables…
20110527 13:46:02 Info: Disconnecting from database…
20110527 13:46:02 Info: Mounting snapshot…
20110527 13:46:02 Info: Running: mount -o rw /dev/mysqlvg/mysqllv_snapshot //backup
20110527 13:46:02 Info: DONE: mount snapshot
20110527 13:46:02 Info: Copying /tmp/mylvmbackup-backup-20110527_134602_mysql-TJ74OT.pos to //backup-pos/backup-20110527_134602_mysql.pos…
20110527 13:46:02 Info: Copying /etc/my.cnf to //backup-pos/backup-20110527_134602_mysql_my.cnf…
20110527 13:46:02 Info: Taking actual backup…
20110527 13:46:02 Info: Creating tar archive /home/bakup/backup-20110527_134602_mysql.tar.gz
20110527 13:46:02 Info: Running: cd ‘/’ ;’tar’ cvf – backup/ backup-pos/backup-20110527_134602_mysql.pos backup-pos/backup-20110527_134602_mysql_my.cnf| gzip –stdout –verbose –best -> /home/bakup/backup-20110527_134602_mysql.tar.gz.INCOMPLETE-QXEggMj
……
20110527 13:46:03 Info: DONE: create tar archive
20110527 13:46:03 Info: Cleaning up…
20110527 13:46:03 Info: Running: umount //backup
20110527 13:46:03 Info: DONE: Unmounting //backup
20110527 13:46:03 Info: LVM Usage stats:
20110527 13:46:03 Info: LV VG Attr LSize Origin Snap% Move Log Copy% Convert
20110527 13:46:03 Info: mysqllv_snapshot mysqlvg swi-a- 30.00G mysqllv 0.00
20110527 13:46:03 Info: Running: lvremove -f /dev/mysqlvg/mysqllv_snapshot
Logical volume “mysqllv_snapshot” successfully removed
20110527 13:46:03 Info: DONE: Removing snapshot
利用mylvmbackup工具来进行备份的确很方便吧,不过,每次都要输入那么长的一段命令吗?当然不用,mylvmbackup有个配置文件,即/etc/mylvmbackup.conf,把那些参数都写进配置文件里吧。
10、修改mylvmbackup配置文件
[root@nfs-client ~]# vim /etc/mylvmbackup.conf
#连接数据库的定义
[mysql]
user=root //连接数据库的用户名
password=qqing99 //连接数据库的密码
host=localhost //连接数据库的主机
port=3306 //连接数据库的端口
socket=/var/lib/mysql/mysql.sock //连接数据库的套接字文件
mycnf=/etc/my.cnf //数据库的主配置文件
#
# LVM-specific options
#
[lvm] //LVM参数的定义
vgname=mysqlvg //LVM中VG的名字
lvname=mysqllv //LVM中LV的名字
backuplv=
lvsize=30G //定义快照大小
#
# File system specific options
#
[fs]
xfs=0
#mountdir=/var/tmp/mylvmbackup/mnt/
mountdir=
#backupdir=/var/tmp/mylvmbackup/bakup/
backupdir=/home/bakup //定义备份文件的保存路径
relpath=
其它的默认即可,现在保存退出
11、利用配置文件来备份
[root@nfs-client ~]# mylvmbackup
20110527 14:01:50 Info: Connecting to database…
20110527 14:01:50 Info: Flushing tables with read lock…
20110527 14:01:50 Info: Taking position record into /tmp/mylvmbackup-backup-20110527_140150_mysql-8Qde20.pos…
20110527 14:01:50 Info: Running: lvcreate -s –size=30G –name=mysqllv_snapshot /dev/mysqlvg/mysqllv
File descriptor 4 (socket:[26120]) leaked on lvcreate invocation. Parent PID 5338: /usr/bin/perl
Logical volume “mysqllv_snapshot” created
20110527 14:01:50 Info: DONE: taking LVM snapshot
20110527 14:01:50 Info: Unlocking tables…
20110527 14:01:50 Info: Disconnecting from database…
20110527 14:01:50 Info: Mounting snapshot…
20110527 14:01:50 Info: Running: mount -o rw /dev/mysqlvg/mysqllv_snapshot //backup
20110527 14:01:50 Info: DONE: mount snapshot
20110527 14:01:50 Info: Copying /tmp/mylvmbackup-backup-20110527_140150_mysql-8Qde20.pos to //backup-pos/backup-20110527_140150_mysql.pos…
20110527 14:01:50 Info: Copying /etc/my.cnf to //backup-pos/backup-20110527_140150_mysql_my.cnf…
20110527 14:01:50 Info: Taking actual backup…
20110527 14:01:50 Info: Creating tar archive /home/bakup/backup-20110527_140150_mysql.tar.gz
20110527 14:01:50 Info: Running: cd ‘/’ ;’tar’ cvf – backup/ backup-pos/backup-20110527_140150_mysql.pos backup-pos/backup-20110527_140150_mysql_my.cnf| gzip –stdout –verbose –best -> /home/bakup/backup-20110527_140150_mysql.tar.gz.INCOMPLETE-Sjuozmb
……
20110527 14:01:51 Info: DONE: create tar archive
20110527 14:01:51 Info: Cleaning up…
20110527 14:01:51 Info: Running: umount //backup
20110527 14:01:51 Info: DONE: Unmounting //backup
20110527 14:01:51 Info: LVM Usage stats:
20110527 14:01:51 Info: LV VG Attr LSize Origin Snap% Move Log Copy% Convert
20110527 14:01:51 Info: mysqllv_snapshot mysqlvg swi-a- 30.00G mysqllv 0.00
20110527 14:01:51 Info: Running: lvremove -f /dev/mysqlvg/mysqllv_snapshot
Logical volume “mysqllv_snapshot” successfully removed
20110527 14:01:51 Info: DONE: Removing snapshot
 
PS:
用mylvmbackup工具备份压缩的包解开后会有两个文件夹,一个是backup,里面是数据库数据文件,一个是backup-pos,里面则数据库配置文件和其他相关信息。
(四)Crontab调用perl脚本,实现定时备份
这样是方便多了,那如果调用crontab来设置定时任务呢?
通过crontab调用shell脚本:
[root@nfs-client ~]# crontab -e
 
SHELL=/bin/sh
 
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
 
HOME=/var/log
*/1 * * * * /usr/local/bin/mylvmbackup
在这里为了方便测试,所以设置每隔一分钟就执行一次备份。
 
第三章把数据库备份到网络硬盘
 
担心本地硬盘损坏,备份的数据全部消失?为安全着想,需要把数据备份到远程机子上?
那就按照以下操作来实现吧。
(一)测试环境
用于创建网络硬盘的远程机子环境:
[root@repli4 ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:73:31:B9
inet addr:192.168.113.131 Bcast:192.168.113.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe73:31b9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:112 errors:0 dropped:0 overruns:0 frame:0
TX packets:108 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:11372 (11.1 KiB) TX bytes:17561 (17.1 KiB)
Interrupt:75 Base address:0×2000
 
Mysql服务器的环境:
 
[root@nfs-client ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 1C:6F:65:D4:57:16
inet addr:192.168.42.189 Bcast:192.168.42.255 Mask:255.255.255.0 
inet6 addr: fe80::1e6f:65ff:fed4:5716/64 Scope:Link 
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
RX packets:14140 errors:0 dropped:0 overruns:0 frame:0 
TX packets:7008 errors:0 dropped:0 overruns:0 carrier:0 
collisions:0 txqueuelen:1000 
RX bytes:3851583 (3.6 MiB) TX bytes:784162 (765.7 KiB) 
Interrupt:225 Base address:0xa000 
(二)远程机子创建网络硬盘
1、安装scsi-target-utils
[root@repli4 ~]# yum install scsi-target-utils
2、启动服务
[root@repli4 ~]# service tgtd start
Starting SCSI target daemon: [确定]
3、创建一个id为1,名字为promise的target
[root@repli4 ~]# tgtadm –lld iscsi –op new –mode target –tid 1 -T promise
4、查看空闲的分区或硬盘
[root@repli4 ~]# fdisk -l
 
Disk /dev/sda: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
Device Boot Start End Blocks Id System
/dev/sda1 * 1 10169 81682461 83 Linux
/dev/sda2 10170 10443 2200905 82 Linux swap / Solaris
 
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
Device Boot Start End Blocks Id System
/dev/sdb1 1 1305 10482381 83 Linux
 
在这里,/dev/sdb1可用。
5、添加/dev/sdb到target 1中,供Initiator(即供Mysql服务器使用)
[root@repli4 ~]# tgtadm –lld iscsi –op new –mode logicalunit –tid 1 –lun 1 -b /dev/sdb 1
6、对Mysql服务器进行授权访问
[root@repli4 ~]# tgtadm –lld iscsi –op bind –mode target –tid 1 -I 192.168.42.189
 
PS:
在测试时已经对Mysql服务器进行了授权,可是mysql服务器仍然无法搜索到网络硬盘,猜想可能是由于两台机子不同网段,所以又进行了如下操作:
[root@repli4 ~]# tgtadm –lld iscsi –op bind –mode target –tid 1 -I 192.168.113.1
7、查看tgtadm配置情况
[root@repli4 ~]# tgtadm –lld iscsi –op show –mode target
Target 1: promise
System information:
Driver: iscsi
State: ready
I_T nexus information:
LUN information:
LUN: 0
Type: controller
SCSI ID: IET 00010000
SCSI SN: beaf10
Size: 0 MB
Online: Yes
Removable media: No
Backing store type: null
Backing store path: None
Backing store flags:
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 10737 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: /dev/sdb
Backing store flags:
Account information:
ACL information:
192.168.42.189
192.168.113.1
 
至此,远程机子的网络硬盘已经创建好了。
 
PS:
上面的配置在重启tgtd服务后会失效,如果想保持配置的话可以写入/etc/rc.local文件中。
[root@repli4 ~]# vim /etc/rc.local
tgtadm –lld iscsi –op new –mode target –tid 1 -T promise
tgtadm –lld iscsi –op new –mode logicalunit –tid 1 –lun 1 -b /dev/sdb1
tgtadm –lld iscsi –op bind –mode target –tid 1 -I 192.168.42.189
tgtadm –lld iscsi –op bind –mode target –tid 1 -I 192.168.113.1
保存退出,然后重启机子生效。
(三)映射网络硬盘
现在,在mysql服务器安装相应软件,用来搜索挂载网络硬盘
1、安装iscsi-initiator-utils
[root@nfs-client ~]# yum install iscsi-initiator-utils
2、启动服务
[root@nfs-client ~]# service iscsi start
iscsid 已停
Starting iSCSI daemon: [确定]
[确定]
设置 iSCSI 目标:iscsiadm: No records found!
[确定]
3、搜寻target
[root@nfs-client ~]# iscsiadm -m discovery -t sendtargets -p 192.168.113.131
192.168.113.131:3260,1 promise
4、映射target
[root@nfs-client ~]# iscsiadm -m node -T promise -p 192.168.113.131 -l
Logging in to [iface: default, target: promise, portal: 192.168.113.131,3260]
Login to [iface: default, target: promise, portal: 192.168.113.131,3260] successful.
5、查看磁盘信息
[root@nfs-client ~]# fdisk -l
 
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 63819 512104005 83 Linux
/dev/sda3 63820 68931 41062140 83 Linux
/dev/sda4 68932 121593 423007515 5 Extended
/dev/sda5 68932 72768 30820671 83 Linux
/dev/sda6 72769 75330 20579233+ 83 Linux
/dev/sda7 75331 77892 20579233+ 83 Linux
/dev/sda8 77893 78414 4192933+ 82 Linux swap / Solaris
/dev/sda9 78415 82062 29302528+ 83 Linux
/dev/sda10 82063 86926 39070048+ 83 Linux
 
Disk /dev/sdb: 10.7 GB, 10733958144 bytes
64 heads, 32 sectors/track, 10236 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
 
Disk /dev/sdb doesn’t contain a valid partition table
 
可以看到映射过来的硬盘/dev/sdb1
6、格式化/dev/sdb
 
6、创建挂载点
[root@nfs-client ~]# mkdir mysqlbakup && cd mysqlbakup && pwd
/root/mysqlbakup
7、挂载网络硬盘到/root/mysqlbakup
[root@nfs-client ~]# mount /dev/sdb /root/mysqlbakup/
(四)Mylvmbackup把数据库备份到网络硬盘
1、修改mylvmbackup配置文件
[root@nfs-client ~]# vim /etc/mylvmbackup.conf
将备份路径修改:
backupdir=/root/mysqlbakup
保存退出即可。
2、备份数据库
[root@nfs-client ~]# myvlmbackup
 
第四章 一些需要注意的问题
 
(一)创建LVM时分区的问题
[root@nfs-client ~]# fdisk /dev/sda
 
The number of cylinders for this disk is set to 121601.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
 
Command (m for help): p
 
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 63819 512104005 83 Linux
/dev/sda3 63820 68931 41062140 83 Linux
/dev/sda4 68932 121593 423007515 5 Extended
/dev/sda5 68932 72768 30820671 83 Linux
/dev/sda6 72769 75330 20579233+ 83 Linux
/dev/sda7 75331 77892 20579233+ 83 Linux
/dev/sda8 77893 78414 4192933+ 82 Linux swap / Solaris
Command (m for help): n
First cylinder (78415-121593, default 78415):
Using default value 78415
Last cylinder or +size or +sizeM or +sizeK (78415-121593, default 121593): +10G
 
Command (m for help): t
Partition number (1-9): 9
Hex code (type L to list codes): 83
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
 
[root@nfs-client ~]# mkfs -t ext3 /dev/sda9
mke2fs 1.39 (29-May-2006)
Could not stat /dev/sda9 — 没有那个文件或目录
 
The device apparently does not exist; did you specify it correctly?
解决方法:
[root@nfs-client ~]# partprobe
因为建立新分区后,核心无法重新取得分割表资讯,所以需要重启,但是这样会比较麻烦,所以可以用GNU推出的工具--通过执行partprobe指令--来告知核心必须要读取新的分割表。
(二)手动创建LVM快照区时必须要先锁定数据库,然后再创建
mysql> flush tables with read lock;
[root@nfs-client ~]# lvcreate -l 2384 -s -n mysqlbak /dev/mysqlvg/mysqllv
(三)创建了快照区后必须要文件打包放到安全的地方,否则在移除快照区后再创建时之前的文件将不复存在
(四)crontab调用perl脚本的问题
[root@nfs-client ~]# crontab -e
*/1 * * * * /usr/local/bin/mylvmbackup
可是,问题出现了:
Can’t exec “lvs”: No such file or directory at /usr/local/bin/mylvmbackup line
919 (#1)
(W exec) A system(), exec(), or piped open call could not execute the
named program for the indicated reason. Typical reasons include: the
permissions were wrong on the file, the file wasn’t found in
$ENV{PATH}, the executable in question was compiled for another
architecture, or the #! line in a script points to an interpreter that
can’t be run for similar reasons. (Or maybe your system doesn’t support
#! at all.)
Use of uninitialized value in substitution (s///) at /usr/local/bin/mylvmbackup
line 923 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a “” or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, “that $foo” is
usually optimized into “that ” . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
Use of uninitialized value in substitution (s///) at /usr/local/bin/mylvmbackup
line 924 (#1)
20110527 14:28:01 Error: lvs: No such file or directory
20110527 14:28:01 Info: Connecting to database…
20110527 14:28:01 Info: Flushing tables with read lock…
20110527 14:28:01 Info: Taking position record into /tmp/mylvmbackup-backup-20110527_142801_mysql-FVSHIw.pos…
20110527 14:28:01 Info: Running: lvcreate -s –size=30G –name=mysqllv_snapshot /dev/mysqlvg/mysqllv
 
Can’t exec “lvcreate”: No such file or directory at /usr/local/bin/mylvmbackup
line 862 (#2)
(W exec) A system(), exec(), or piped open call could not execute the
named program for the indicated reason. Typical reasons include: the
permissions were wrong on the file, the file wasn’t found in
$ENV{PATH}, the executable in question was compiled for another
architecture, or the #! line in a script points to an interpreter that
can’t be run for similar reasons. (Or maybe your system doesn’t support
#! at all.)
20110527 14:28:01 Error: FAILED: taking LVM snapshot (received signal 255)
20110527 14:28:01 Info: Unlocking tables…
20110527 14:28:01 Info: Disconnecting from database…
20110527 14:28:01 Info: Cleaning up…
重新设置一下:
[root@nfs-client ~]# crontab -e
*/1 * * * * sh /usr/local/bin/mylvmbackup
还是出现那样的问题
因为这是一个perl脚本,而perl所使用的环境变量为系统的环境变量,crontab在运行时不使用当前系统的环境变量,所以需要在crontab中指定环境变量,即:
[root@nfs-client ~]# crontab -e
 
SHELL=/bin/sh
 
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
 
HOME=/var/log
*/1 * * * * /usr/local/bin/mylvmbackup
 
附录:
参考资料一:鸟哥的Linux私房菜<http://www.vbird.org>
参考资料二:Mysql中文网<http://imysql.cn/?q=node/102>
参考资料三:fedora wikihttp://fedoraproject.org/wiki/Scsi-target- utils_Quickstart_Guide#About


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容