mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.52k stars 1.24k forks source link

[BUG] Rebuild function never deletes the previous database backup #2450

Open michelebastione opened 6 months ago

michelebastione commented 6 months ago

Version 5.0.18, 5.0.19

Describe the bug Every time the LiteEngine.Rebuild method is called the database's contents are copied in a brand new backup file named with an incremental numerical suffix. The previous backup file is never deleted and all the copies just keep accumulating on disk.

Code to Reproduce using var db = new LiteDatabase("Filename=TestDB"); db.Rebuild();

Expected behavior There should always be only one backup copy of the database on disk while the previous one should be deleted during the Rebuild process, otherwise all these redundant copies will end up occupying precious disk storage, especially if the database size is significant.

Screenshots/Stacktrace Result of running the example snippet a few times.

Additional context I believe this happens because of the way the backupFilename is assigned in the RebuildService.Rebuild method: var backupFilename = FileHelper.GetSuffixFile(_settings.Filename, "-backup", true); The checkIfExists parameter sohuld not be set to true, as it will trigger the suffix to increment. Instead a check on the existence of the backup and its potential deletion could be performed before creating the new one. This, at least for me, seemed to solve the issue:

FileHelper.Exec(5, () =>
{
    if (File.Exists(backupFilename))
    {
        File.Delete(backupFilename);
    }
    File.Move(_settings.Filename, backupFilename);
});
mw911 commented 3 months ago

I am faced with the same problem. Using 5.0.20

Frenchy62620 commented 3 months ago

same problem, i prefer to stay in version 5.0.17

mw911 commented 3 months ago

@michelebastione did you only replace the code with FileHelper.Exec(5, () => { if (File.Exists(backupFilename)) { File.Delete(backupFilename); } File.Move(_settings.Filename, backupFilename); });

or also set this to false?

var backupFilename = FileHelper.GetSuffixFile(_settings.Filename, "-backup", true);

michelebastione commented 3 months ago

@mw911 Yes, you need to set the checkIfExists parameter to false or the function will keep adding increasing numerical suffixes to backupName, that's the root of the problem. The second part is to ensure that the backup file doesn't already exists when we rename the original file, or an IOException is thrown. Though you can avoid to check if the file already exists before calling File.Delete(backupFilename), as it doesn't raise any exception when you do so.

sugu0620 commented 6 days ago

is this issue resolved in latest build 5.0.21?