[BUG]Cannot backup database because copied file is truncated
Version current commit from 15 March, https://github.com/mbdavid/LiteDB/commit/5ec64e5bd2453e506bd135ace1b1c0ab5717d3f5 same for 5.0.10
Describe the bug Trying to make a database backup using File.Copy();
I am calling:
db.Checkpoint() //to merge the log file into the main file db.Rebuild(); db.Dispose(); FileInfo().Length; //only reports the file as being 8192 bytes long.. File.Copy();
However FileInfo().Length reports the file as being only 8K long, so File.Copy() indeed only copies 8K bytes. Tried measuring the length before opening a database connection and the file length was reported correctly.
Looked into the documentation but I think this is the correct way to do it, still it fails.
Thanks for the report. We ran your sequence (Checkpoint(), Rebuild(), Dispose(), then FileInfo.Length and File.Copy) on 5.0.10 and on the current version, and the file kept its full rebuilt size and the copy opened with all documents. 8192 bytes is exactly one page, the size of a new, empty database, so we suspect a different file was opened than the one measured.
Could you share:
- the full code, including the connection string and the paths passed to
FileInfoandFile.Copy - the sizes of
x.dbandx-log.dbbefore and after - whether the original still has its data when you reopen it
For a backup, Checkpoint() then Dispose() before copying is enough; Rebuild() isn't needed.
We ran your sequence on the current version: a file with 3,000 documents, 1,000 of them deleted, then Checkpoint(), Rebuild(), Dispose(), new FileInfo(path).Length and File.Copy. The file kept its full rebuilt size (827,392 bytes, not 8,192), and the copy opened with all 2,000 documents. 5.0.10 behaved the same. 8,192 bytes is the size of a new, empty database, so the code probably opened a different file than the one it measured, for example through a relative path. For a backup, calling Checkpoint() and Dispose() before copying is enough; Rebuild() isn't needed.
You can try the latest prerelease with dotnet add package LiteDB --prerelease. If it still happens on the prerelease, comment here with the details and we'll reopen it.