【发布时间】:2013-04-19 13:22:37
【问题描述】:
我正在尝试从每隔 5 分钟运行的 cronjob 备份我的 sqlite 数据库。数据库是“实时的”,因此在我要执行备份时正在运行查询。
我想确定,当我备份数据库时,它的状态良好,以便我可以依赖备份。
我目前的策略(伪代码):
function backup()
{
#try to acquire the lock for 2 seconds, then check the database integrity
sqlite3 mydb.sqlite '.timeout 2000' 'PRAGMA integrity_check;'
if (integrity is ok and database was not locked)
{
#perform the backup to backup.sqlite
sqlite3 mydb.sqlite '.timeout 2000' '.backup backup.sqlite'
if (backup could be performed)
{
#Check the consistency of the backup database
sqlite3 backup.sqlite 'PRAGMA integrity_check;'
if (ok)
{
return true;
}
}
}
return false;
}
现在,我的策略有一些问题:
- 如果活动数据库被锁定,我会遇到问题,因为那时我无法执行备份。也许交易可以帮助解决?
- 如果
PRAGMA integrity_check;和备份之间出现问题,我就完蛋了。
有什么想法吗?顺便问一下,sqlite3 .backup 和旧的cp mydb.sqlite mybackup.sqlite 有什么区别?
[edit] 我在嵌入式系统上运行 nodejs,所以如果有人建议 sqlite online backup api 使用一些 ruby 包装器 - 没有机会 ;(
【问题讨论】:
标签: sqlite backup database-backups