mysqlhotcopy is a perl script that comes with MySQL installation. This locks the table, flush the table and then performs a copy of the database. You can also use the mysqlhotcopy to automatically copy the backup directly to another server using scp.

1. mysqlhotcopy command:

[local-host]# /usr/bin/mysqlhotcopy -u root -p My2Secure$Password sugarcrm /home/backup/database --allowold --keepold

  

The above example, performs a backup of sugarcrm, a MySQL database to the /home/backup/database directory.

  • –allowold: This options renames the old backup to {datbase-name}_old before taking a new backup. In this example, if sugarcrm backup already existing under /home/backup/database, it will move the old backup to /home/backup/database/sugarcrm_old before creating /home/backup/database/sugarcrm.
  • –keepold: This option instructs the mysqlhotcopy to keep the old backup (i.e the renamed _old) after the backup is completed.

View the mysqlhotcopy documentation using perldoc as shown below.

[local-host]# perldoc mysqlhotcopy

  

Following are the available options that can be passed to mysqlhotcopy command.

Option

Description

–addtodest Do not rename target directory (if it exists); merely add files to it
–allowold Do not abort if a target exists; rename it by adding an _old suffix
–checkpoint=db_name.tbl_name Insert checkpoint entries
–chroot=path Base directory of the chroot jail in which mysqld operates
–debug Write a debugging log
–dryrun Report actions without performing them
–flushlogs Flush logs after all tables are locked
–help Display help message and exit
–host=host_name Connect to the MySQL server on the given host
–keepold Do not delete previous (renamed) target when done
–noindices Do not include full index files in the backup
–password[=password] The password to use when connecting to the server
–port=port_num The TCP/IP port number to use for the connection
–quiet Be silent except for errors
–regexp Copy all databases with names that match the given regular expression
–resetmaster Reset the binary log after locking all the tables
–resetslave Reset the master.info file after locking all the tables
–socket=path For connections to localhost
–tmpdir=path The temporary directory
–user=user_name, The MySQL username to use when connecting to the server
–version Display version information and exit

2. mysqlhotcopy command output:

The above mysqlhotcopy command will display an output similar to the following.

 
[local-host]# /usr/bin/mysqlhotcopy -u root -p My2Secure$Password sugarcrm /home/backup/database --allowold --keepold
Locked 98 tables in 0 seconds.
Flushed tables (`sugarcrm`.`accounts`, `sugarcrm`.`accounts_audit`, `sugarcrm`.`accounts_bugs`) in 0 seconds.
Copying 295 files...
Copying indices for 0 files...
Unlocked tables.
mysqlhotcopy copied 98 tables (295 files) in 0 seconds (0 seconds overall).

  

By default, MySQL database is located under /var/lib/mysql/{db-name}. mysqlhotcopy takes a backup of the table files from this default database location, to the backup directory. The backup directory /home/backup/database/sugarcrm,  will contain exact copy of all the files from the real MySQL database /var/lib/mysql/sugarcrm directory.

[local-host]# ls -1 /var/lib/mysql/sugarcrm | wc -l
295
[local-host]# ls -1 /home/backup/database/sugarcrm | wc -l
295

  

Please note that every table has three corresponding files with the extension *.frm, *.MYD and *.MYI.  The database directory also contains a db.opt file that contains the database related parameter.

In the above example, you can see the mysqlhotcopy takes a backup of 98 sugarcrm database tables. So, the total number of files in the backup directory = 98 tables * 3 + 1 db configuration file = 296 files.

3. Restore from mysqlhotcopy

To restore the backup from the mysqlhotcopy backup, simply copy the files from the backup directory to the /var/lib/mysql/{db-name} directory. Just to be on the safe-side, make sure to stop the mysql before you restore (copy) the files. After you copy the files to the /var/lib/mysql/{db-name} start the mysql again.

4. Troubleshooting mysqlhotcopy

How to resolve Can’t locate DBD/mysql.pm issue? mysqlhotcopy is a perl script and it requires the perl-DBD module. You may receive the following error while executing mysqlhotcopy if perl-DBD module is not installed.

[local-host]# /usr/bin/mysqlhotcopy -u root -p My2Secure$Password sugarcrm /home/backup/database --allowold --keepoldinstall_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/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.
at /usr/bin/mysqlhotcopy line 177

  

Make sure to install the perl-DBD package as shown below.

[local-host]# rpm -ivh perl-DBD-MySQL-3.0007-1.fc6.i386.rpm
Preparing... ########################################### [100%]
1:perl-DBD-MySQL ########################################### [100%]

  

How to resolve the issue with perl-DBD installation? While installing the perl-DBD, you may get the following error message.

[local-host]# rpm -ivh perl-DBD-MySQL-3.0007-1.fc6.i386.rpm
rpmdb: Program version 4.3 doesn't match environment version
error: db4 error(-30974) from dbenv->open: DB_VERSION_MISMATCH: Database environment version mismatch
error: cannot open Packages index using db3 - (-30974)
warning: cannot open Solve database in /usr/lib/rpmdb/i386-redhat-linux/redhat
rpmdb: Program version 4.3 doesn't match environment version
error: db4 error(-30974) from dbenv->open: DB_VERSION_MISMATCH: Database environment version mismatch
warning: cannot open Solve database in /usr/lib/rpmdb/i386-redhat-linux/redhat
error: Failed dependencies:
libmysqlclient.so.15 is needed by perl-DBD-MySQL-3.0007-1.fc6.i386
libmysqlclient.so.15(libmysqlclient_15) is needed by perl-DBD-MySQL-3.0007-1.fc6.i386

  

Download and install the MySQL-shared-compat from mysql.com and this should resolve the above mentioned error while installing the perl-DBD package.

[local-host]# rpm -ivh MySQL-shared-compat-5.1.25-0.rhel5.i386.rpm
Preparing... ########################################### [100%]
1:MySQL-shared-compat ########################################### [100%]

参考:

http://www.thegeekstuff.com/2008/07/backup-and-restore-mysql-database-using-mysqlhotcopy/

Backup and Restore MySQL Database using mysqlhotcopy的更多相关文章

  1. Migrate MySQL database using dump and restore

    kaorimatz/mysqldump-loader: Load a MySQL dump file using LOAD DATA INFILEhttps://github.com/kaorimat ...

  2. How to Baskup and Restore a MySQL database

    If you're storing anything in MySQL databases that you do not want to lose, it is very important to ...

  3. csharp: SQL Server 2005 Database Backup and Restore using C#

    1.第一种方式: using SQLDMO;//Microsoft SQLDMO Object Library 8.0 /// <summary> /// 数据库的备份 /// 涂聚文注: ...

  4. SQL Server Database Backup and Restore in C#

    SQL Server Database Backup and Restore in C# Syed Noman Ali Shah,                          7 Feb 201 ...

  5. how to backup and restore database of SQL Server

    Back up 1,右键选中需要备份的数据库,Tasks-->Backup 2.General中,Destination,先remove掉之前的,然后再Add 需要注意的是,add的文件,必须要 ...

  6. MySQL备份之mysqlhotcopy与注意事项

    此文章主要向大家介绍的是MySQL备份之mysqlhotcopy与其在实际操作中应注意事项的描述,我们大家都知道实现MySQL数据库备份的常用方法有三个,但是我们今天主要向大家介绍的是其中的一个比较好 ...

  7. TFS Express backup and restore

    When we setup source control server, we should always make a backup and restore plan for it. This ar ...

  8. 转:db2 backup 及 restore

    db2 backup 及 restore 2011-06-21 18:12:20|  分类: AIX |举报 |字号 订阅     两个问题: db2=>list applications db ...

  9. 第一章、关于SQL Server数据库的备份和还原(sp_addumpdevice、backup、Restore)

    在sql server数据库中,备份和还原都只能在服务器上进行,备份的数据文件在服务器上,还原的数据文件也只能在服务器上,当在非服务器的机器上启动sql server客户端的时候,也可以通过该客户端来 ...

随机推荐

  1. 用状态机表示SFC中的并行分支

    过去一直认为,状态机表示SFC会不会是任务复杂化,这次简单实验了一下,感觉还可以.请看下面的控制. 在SFC中,A和B是一对并行分支,汇合后转移到C分支中,怎么了用状态机表示呢?这里我们在状态机里分别 ...

  2. flask的自定义过滤器

    过滤器的本质是函数.当模板内置的过滤器不能满足需求,可以自定义过滤器.自定义过滤器有两种实现方式: 一种是通过Flask应用对象的 add_template_filter 方法 通过装饰器来实现自定义 ...

  3. Go web表单验证

    开发Web的一个原则就是,不能信任用户输入的任何信息,所以验证和过滤用户的输入信息就变得非常重要 必填字段 if len(r.Form["username"][0])==0{ // ...

  4. 字典树(Trie)的学习笔记

    按照一本通往下学,学到吐血了... 例题1 字典树模板题吗. 先讲讲字典树: 给出代码(太简单了...)! #include<cstdio> #include<cstring> ...

  5. STL——泛型编程

    1.指针的算术运算 对于一个存储int数据的vector,我们要查找某值是否存在于其中,采用下标操作的做法如下: int* find(const vector<int> &vec, ...

  6. fiddler手机抓包配置方法

    一.下载工具包 百度搜索”fiddler 下载“ ,安装最新版本 下载的软件安装包为“fiddler_4.6.20171.26113_setup.exe”格式,双击安装.安装成功,在“开始”-“所有程 ...

  7. 笔记-twisted-adbapi-scrapy

    笔记-twisted-adbapi-scrapy-mysql 1.      异步插入mysql 在爬虫中需要insert到mysql,但有一个问题是在爬虫环境中commit的及时性与性能冲突. 一般 ...

  8. JSON初体验(二):Gson解析

    今天,我们来介绍一下Gson的jar包的用法. JSON解析之Gson 特点:编码简介,谷歌官方推荐 数据之间的转换: 1.将json格式的字符串{}转换成为java对象 API: <T> ...

  9. Ubantu修改主机名详细步骤

    使用vmWare创建的Ubantu虚拟主机,默认的主机名均为Ubantu,通过修改主机名的使得分布式集群操作变得方便.具体步骤如下: 1.查看虚拟机的主机名:打开终端,使用 hostname 命令,回 ...

  10. 关于springboot 打包问题 jar包和 war包

    起因:项目开发完成   需要打包部署了  发现自己不会打包 那么开始网上学习打包? 那么怎么来打包那? 我们以前没有采用springboot 时候我们都是直接将项目打成war包形式  然后放到tomc ...