linux中利用mysqlhotcopy备份数据脚本示例
2019/10/10/17:46:07 阅读:1905 来源:谷歌SEO算法 标签:
SEO培训
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,代码如下:
- #wgethttp://www.phpfensi.com/upimages/soft/DBD-mysql-4.020.tar.gz
- #tarxzvfDBD-mysql-4.020.tar.gz
- #cdDBD-mysql-4.020
- #perlMakefile.PL--mysql_config=mysql_config
- #make
- #makeinstall
3.备份示例,代码如下:
#mysqlhotcopy -u root -p 密码 --addtodest 数据库名1 数据库名2 备份目录
4.恢复
停止mysql服务,复制备份的数据库目录到相应的mysql数据目录下,用chown更改目录所有者为mysql服务器运行的用户,启动mysql服务.
5.执行perl Makefile.PL --mysql_config=mysql_config编译的时候如果出现如下错误:
- Can'tlocateExtUtils/MakeMaker.pmin@INC(@INCcontains:/usr/local/lib64/perl5/usr/local/share/perl5/usr/lib64/perl5/vendor_perl/usr/share/perl5/vendor_perl/usr/lib64/perl5/usr/share/perl5.)atMakefile.PLline20.
- BEGINfailed--compilationabortedatMakefile.PLline20.
解决方法:
#yum -y install perl-ExtUtils-MakeMaker
如果还是不行就用下面两条:
- #yum-yinstallperl-ExtUtils-MakeMaker*
- #yum-yinstallperl-Time-HiRes*
6.mysqldump和mysqlhotcopy备份shell脚本示例,mysqlbackup.sh,代码如下:
- #!/bin/bash
- #Enviroment
- PATH=/usr/local/sbin:/usr/bin:/bin
- #ConfigureTheDirectoryofBackup
- BACKUPDIR=/data/backup/mysql#mysqlbackupdir
- SAVE=4#howmanydaystosavelastest
- HOST=192.168.10.32#mysqlhost
- ROOT=root#mysqlaccount
- ROOTPASS='XXXXXX'#mysqlrootpassword
- DUMPLIST='mysqldatabase1database2database3'#backupwithmysqldump
- HOTCOPYLIST='database4'#backupwithmysqlhotcopy
- #define
- SQLDIR=$BACKUPDIR/sql/#mysqldumptosqlsavepath
- HOTCOPYDIR=$BACKUPDIR/hotcopy/#mysqlhotcopycopyfilespath
- TMPDIR=$BACKUPDIR/tmp#temporarydir
- DATETIME=`date-dnow+%Y-%m-%d_%H-%M`#createdatedir
- #CreateDirectory
- if[!-d$SQLDIR];then
- mkdir-p$SQLDIR
- fi
- if[!-d$HOTCOPYDIR];then
- mkdir-p$HOTCOPYDIR
- fi
- rm-rf$TMPDIR
- mkdir-p$TMPDIR
- #-----BackupMySQL-----
- #BackupwithDatabase
- fordbnamein$DUMPLIST
- do
- mysqldump-h$HOST-u$ROOT-p$ROOTPASS$dbname>$TMPDIR/$dbname.sql
- done
- fordbnamein$HOTCOPYLIST
- do
- mysqlhotcopy-u$ROOT-p$ROOTPASS$dbname$TMPDIR
- done
- #movehotcopydir
- cd$HOTCOPYDIR
- mkdir-p$DATETIME
- cd$DATETIME
- fordbnamein$HOTCOPYLIST
- do//phpfensi.com
- mv../../tmp/$dbname./
- done
- #createMySQLtar
- cd$SQLDIR
- tar-czf$SQLDIR/mysql_backup.$DATETIME.tar.gz../tmp
- rm-fr$TMPDIR
- find$SQLDIR-typef-mtime+$SAVE|xargsrm-f
- find$HOTCOPYDIR-typed-mtime+$SAVE|xargsrm-rf
热门评论