Skip to content

Commit ac5c161

Browse files
committed
Implement SQLiteDatabase::Backup
1 parent f6f9cd6 commit ac5c161

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/wallet/sqlite.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,29 @@ bool SQLiteDatabase::Rewrite(const char* skip)
186186

187187
bool SQLiteDatabase::Backup(const std::string& dest) const
188188
{
189-
return false;
189+
sqlite3* db_copy;
190+
int res = sqlite3_open(dest.c_str(), &db_copy);
191+
if (res != SQLITE_OK) {
192+
sqlite3_close(db_copy);
193+
return false;
194+
}
195+
sqlite3_backup* backup = sqlite3_backup_init(db_copy, "main", m_db, "main");
196+
if (!backup) {
197+
LogPrintf("%s: Unable to begin backup: %s\n", __func__, sqlite3_errmsg(m_db));
198+
sqlite3_close(db_copy);
199+
return false;
200+
}
201+
// Specifying -1 will copy all of the pages
202+
res = sqlite3_backup_step(backup, -1);
203+
if (res != SQLITE_DONE) {
204+
LogPrintf("%s: Unable to backup: %s\n", __func__, sqlite3_errstr(res));
205+
sqlite3_backup_finish(backup);
206+
sqlite3_close(db_copy);
207+
return false;
208+
}
209+
res = sqlite3_backup_finish(backup);
210+
sqlite3_close(db_copy);
211+
return res == SQLITE_OK;
190212
}
191213

192214
void SQLiteDatabase::Close()

0 commit comments

Comments
 (0)