Snapshots
Snapshots are online point-in-time backups. The database remains fully operational during a snapshot, and db.Snapshot() performs an immediate flush before reading the data files — every write up to the snapshot call is included.
A snapshot contains only the fresh (current) version of each record. Stale and deleted history present in the source .db/.map files is not carried over. As a result, a snapshot is effectively a defragged copy of the live dataset — typically smaller than the source when the source has accumulated stale records.
Snapshot files are named htl.{yyyyMMdd.HHmm.ss.fff}.{datasetName}.db and .map. A snapshot is a fully independent database, openable in any mode, and requires the same configuration as the source (serializer, encryption key, and any other settings must match).
ReadOnly access mode — configuring SnapshotTo on a ReadOnly database throws at configuration time. Snapshots are taken from the persisted files, not the in-memory cache; that is why an immediate flush precedes every snapshot.// 1. Configure a snapshot destination
MemDb.ConfigureFor<Person>("people", _dbDirectory)
.SerializeWith(() => new PersonSerializer())
.EncryptWithPassword(() => "SuperSecureEncryptionPasswordGoesHere.")
.SnapshotTo(Path.Combine(_dbDirectory, "snapshots"))
.Register();
// 2. Take a snapshot — returns the timestamp of the snapshot
DateTime snappedAt;
using (var db = MemDb.Open<Person>("people"))
{
// ... operations ...
snappedAt = db.Snapshot();
// changes made after this point will NOT be in the snapshot
}
// 3. Open the snapshot
string snapshotDatasetName = MemDb.GetConfiguration("people").GetSnapshotDatasetName(snappedAt);
MemDb.ConfigureFor<Person>(snapshotDatasetName, Path.Combine(_dbDirectory, "snapshots"))
.SerializeWith(() => new PersonSerializer())
.EncryptWithPassword(() => "SuperSecureEncryptionPasswordGoesHere.")
.Register();
using (var snap = MemDb.Open<Person>(snapshotDatasetName))
{
// snapshot reflects the state of the database at the time db.Snapshot() was called
}