linux中利用mysqlhotcopy备份数据脚本示例
发布:smiling 来源: PHP粉丝网 添加日期:2015-04-22 22:42:40 浏览: 评论:0
mysqlhotcopy备份比起系统自带的备份可强大了些,它是采用SQL级别的备份机制,备份成的文件是sql了,这个都差不多,mysqlhotcopy备份支持的存储引擎常用的有MyISAM,innodb,下面一起来看看.
mysqlhotcopy只是简单的缓存写入和文件复制的过程,占用资源和备份速度比mysqldump快很多很多,特别适合大的数据库,但需要注意的是:mysqlhotcopy只支持MyISAM 引擎
1.安装perl,代码如下:
#yum -y install perl perl-DBI
使用mysqlhotcopy需要安装perl支持,因为mysqlhotcopy是perl写的.
2.安装DBD-mysql,代码如下:
- #wget http://www.phpfensi.com/upimages/soft/DBD-mysql-4.020.tar.gz
- #tar xzvf DBD-mysql-4.020.tar.gz
- #cd DBD-mysql-4.020
- #perl Makefile.PL --mysql_config=mysql_config
- #make
- #make install
3.备份示例,代码如下:
#mysqlhotcopy -u root -p 密码 --addtodest 数据库名1 数据库名2 备份目录
4.恢复
停止mysql服务,复制备份的数据库目录到相应的mysql数据目录下,用chown更改目录所有者为mysql服务器运行的用户,启动mysql服务.
5.执行perl Makefile.PL --mysql_config=mysql_config编译的时候如果出现如下错误:
- Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at Makefile.PL line 20.
- BEGIN failed--compilation aborted at Makefile.PL line 20.
解决方法:
#yum -y install perl-ExtUtils-MakeMaker
如果还是不行就用下面两条:
- #yum -y install perl-ExtUtils-MakeMaker*
- #yum -y install perl-Time-HiRes*
6.mysqldump和mysqlhotcopy备份shell脚本示例,mysqlbackup.sh,代码如下:
- #!/bin/bash
- # Enviroment
- PATH=/usr/local/sbin:/usr/bin:/bin
- # Configure The Directory of Backup
- BACKUPDIR=/data/backup/mysql # mysql backup dir
- SAVE=4 #how many days to save lastest
- HOST=192.168.10.32 # mysql host
- ROOT=root # mysql account
- ROOTPASS='XXXXXX' # mysql root password
- DUMPLIST='mysql database1 database2 database3' # backup with mysqldump
- HOTCOPYLIST='database4' # backup with mysqlhotcopy
- # define
- SQLDIR=$BACKUPDIR/sql/ # mysqldump to sql save path
- HOTCOPYDIR=$BACKUPDIR/hotcopy/ # mysqlhotcopy copy files path
- TMPDIR=$BACKUPDIR/tmp # temporary dir
- DATETIME=`date -d now +%Y-%m-%d_%H-%M` # create date dir
- # Create Directory
- if [ ! -d $SQLDIR ]; then
- mkdir -p $SQLDIR
- fi
- if [ ! -d $HOTCOPYDIR ]; then
- mkdir -p $HOTCOPYDIR
- fi
- rm -rf $TMPDIR
- mkdir -p $TMPDIR
- # ----- Backup MySQL -----
- # Backup with Database
- for dbname in $DUMPLIST
- do
- mysqldump -h$HOST -u$ROOT -p$ROOTPASS $dbname > $TMPDIR/$dbname.sql
- done
- for dbname in $HOTCOPYLIST
- do
- mysqlhotcopy -u $ROOT -p $ROOTPASS $dbname $TMPDIR
- done
- # move hotcopy dir
- cd $HOTCOPYDIR
- mkdir -p $DATETIME
- cd $DATETIME
- for dbname in $HOTCOPYLIST
- do //phpfensi.com
- mv ../../tmp/$dbname ./
- done
- # create MySQL tar
- cd $SQLDIR
- tar -czf $SQLDIR/mysql_backup.$DATETIME.tar.gz ../tmp
- rm -fr $TMPDIR
- find $SQLDIR -type f -mtime +$SAVE | xargs rm -f
- find $HOTCOPYDIR -type d -mtime +$SAVE | xargs rm -rf
Tags: mysqlhotcopy linux备份数据
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)