h2数据库数据文件备份(防止异常断电导致的数据库文件无法使用,元数据块损坏等问题)
使用版本:h2数据库h2-1.4.200.jar centos
1.备份数据库前要先停止数据库服务,并且不要直接硬拷贝※
h2官网原文:
The Backup
tool (org.h2.tools.Backup
) can not be used to create a online backup; the database must not be in use while running this program.
备份
工具 (org.h2.tools.Backup
) 不能用于创建联机备份;运行此程序时,不得使用数据库。
Creating a backup by copying the database files while the database is running is not supported, except if the file systems support creating snapshots. With other file systems, it can't be guaranteed that the data is copied in the right order.
不支持在数据库运行时通过复制数据库文件来创建备份,除非文件系统支持创建快照。对于其他文件系统,无法保证以正确的顺序复制数据。
2.运行备份命令
java -cp h2-1.4.200.jar org.h2.tools.Script -url jdbc:h2:/usr/local/dbfile -user aaa -password aaa -script test.zip -options compression zipjdbc:h2:/usr/local/dbfile 数据库文件所在路径
user 用户名
password 密码
test.zip 生成的sql压缩包
3.运行恢复命令
java -cp h2-1.4.200.jar org.h2.tools.RunScript -url jdbc:h2:/usr/local/dbfilebf -user aaa -password aaa -script test.zip -options compression zipjdbc:h2:/usr/local/dbfilebf 新数据库路径
注:经测试生成的数据库文件要比备份的数据库大很多,第一次访问数据库大小会上下浮动,运行一段时间后大小稳定,比原数据库文件大小大几十M左右(原因未知,但是数据是正确的。要是有人知道可以下方评论下)