mysqlhotcopy是mysql数据库中一个备份工具,相对于mysqldump是一个快速文件意义上的COPY,mysqlhotcopy是一个数据库端的SQL语句集合.
mysqlhotcopy用法
使用mysqlhotcopy之前需要安装perl-DBI和DBD-mysql.
1、执行yum install perl-DBI安装perl-DBI.
2、安装DBD-mysql请参考底部资料.
一个常用的备份例子:
/usr/local/mysql/bin/mysqlhotcopy -u root -p Zmh537817 --addtodest 数据库名1 数据库名2 备份目录
–addtodest的意思是当备份存在时,不中断备份,只添加新的文件进去.
更多的选项可以执行/usr/local/mysql/bin/mysqlhotcopy –help查看.
如何还原?
1、停止mysql服务器.
2、复制备份的数据库目录到mysql数据目录下.
3、更改目录所有者为mysql服务器运行的用户(如mysql).
4、启动mysql服务器.
实例:mysqlhotcopy只用于备份MyISAM,它运行在Unix和NetWare中,使用方法见下面的脚本.加入crotab中吧,代码如下:
- #!/bin/sh
- # Name:mysqlbackup.sh
- # PS:MySQL DataBase Backup,Use mysqlhotcopy script.
- # Last Modify:2008-06-12
- # 定义变量,请根据具体情况修改
- # 定义脚本所在目录
- scriptsDir=`pwd`
- # 数据库的数据目录
- dataDir=/var/lib/mysql
- # 数据MySQL备份目录
- tmpBackupDir=/tmp/mysqlblackup
- backupDir=/backup/mysql
- # 用来备份数据库的用户名和密码
- mysqlUser=root
- mysqlPWD='you password'
- # 如果临时备份目录存在,清空它,如果不存在则创建它
- if [[ -e $tmpBackupDir ]]; then
- rm -rf $tmpBackupDir/*
- else
- mkdir $tmpBackupDir
- fi
- # 如果备份目录不存在则创建它
- if [[ ! -e $backupDir ]];then
- mkdir $backupDir
- fi
- # 得到数据库备份列表,在此可以过滤不想备份的数据库
- for databases in `find $dataDir -type d |
- sed -e "s//var/lib/mysql///" |
- sed -e "s/test//"`; do
- if [[ $databases == "" ]]; then
- continue
- else
- # MySQL备份数据库
- /usr/bin/mysqlhotcopy --user=$mysqlUser --password=$mysqlPWD -q "$databases" $tmpBackupDir
- dateTime=`date "+%Y.%m.%d %H:%M:%S"`
- echo "$dateTime Database:$databases backup success!" >>MySQLBackup.log --Vevb.com
- fi
- done
- # 压缩备份文件
- date=`date -I`
- cd $tmpBackupDir
- tar czf $backupDir/mysql-$date.tar.gz ./
- #End完成
加入到crontab中设置每周5运行,代码如下:
0 0 * * 5 /backup/blackup.sh
注意:恢复数据库到备份时的状态.
mysqlhotcopy 备份出来的是整个数据库目录,使用时可以直接拷贝到 mysqld 指定的 datadir (在这里是 /var/lib/mysql/)目录下即可,同时要注意权限的问题,如下例:
- shell> cp -rf db_name /var/lib/mysql/
- shell> chown -R mysql:mysql /var/lib/mysql/ (将 db_name 目录的属主改成 mysqld 运行用户)
本套备份策略只能恢复数据库到最后一次备份时的状态,要想在崩溃时丢失的数据尽量少应该更频繁的进行MySQL备份,要想恢复数据到崩溃时的状态请使用主从复制机制(replication).
mysqlhotcopy备份报错,错误信息:
- /usr/local/mysql/bin/mysqlhotcopy -u root -p --addtodest --noindices --flushlog -q qeedoo /tmp/bakinstall_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at (eval 7) line 3. Perhaps the DBD::mysql perl module hasn't been fully installed, or perhaps the capitalisation of 'mysql' isn't right. Available drivers: DBM, ExampleP, File, Proxy, Sponge.
解决办法,代码如下:
# http://search.cpan.org/~capttofu/DBD-mysql-3.0008/ wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.020.tar.gz tar zxvf DBD-mysql-4.020.tar.gz cd /tmp/DBD-mysql-4.020 perl Makefile.PL --mysql_config=/usr/local/webserver/mysql/bin/mysql_configmakemake install
最后编译安装完之后,发现还是报错:
- ?12345 install_driver(mysql) failed: Can't load '/usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/auto/DBD/mysql/mysql.so' for module DBD::mysql: libmysqlclient.so.16: cannot open shared object file: No such file or directory at /usr/lib/perl5/5.8.8/i386-linux-thread-multi/DynaLoader.pm line 230. at (eval 7) line 3 Compilation failed in require at (eval 7) line 3. Perhaps a required shared library or dll isn't installed where expected at /usr/bin/mysqlhotcopy line 177
是因为tar安装mysql后的路径配置的有问题:vi /etc/ld.so.conf ,把mysql的这个库文件路径添加进去,代码如下:
echo "/usr/local/webserver/mysql/lib/mysql" >> /etc/ld.so.conf ldconfig
再执行mysqlhotcopy的时候,一切正常,有一台服务出现如下问题,以下问题待遇查看资料,先记下另外这个问题:
- Can't locate DBI.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /usr/local/mysql/bin/mysqlhotcopy line 8. BEGIN failed--compilation aborted at /usr/local/mysql/bin/mysqlhotcopy line 8.
原因:系统没有按安装DBI组件.
新闻热点
疑难解答