Backups
The database object provides a simple way to snapshot all collections to another directory. You can keep the original part-file layout or pack each collection into a single JSON file.
Basic backup
// Copy every collection’s part files into ./backups
const path = db.backup('./backups');
console.log('Backup written to', path);
// Async version
await db.backupAsync('./backups');
The default target is ./backups if you omit the argument.
The method returns the path on success, or null on error.
What gets copied?
- Only
part-*.jsonfiles index.json, temp files and thecrash/folder are not copied in the default mode- Corrupted parts that cannot be parsed are skipped when packing
Packed backup
Pass { pack: true } to merge every part of a collection into one
readable JSON array file:
db.backup('./exports', { pack: true });
// Result:
// exports/
// users/
// users.json ← all user documents in one array
// orders/
// orders.json
This is convenient for exporting data to another system or for human inspection.
Typical backup layout (default mode)
backups/
├── users/
│ ├── part-0001.json
│ └── part-0002.json
└── orders/
└── part-0001.json
Restoring
There is no dedicated restore method. To restore:
- Stop the application (or unload the collections).
- Replace the collection directories under your database path with the ones from the backup.
- Restart / open the database again. The index will be rebuilt automatically if needed.
# Example restore of the users collection
rm -rf ./data/users
cp -r ./backups/users ./data/users
Events
On success the database emits a backup event:
db.on('backup', (info) => {
console.log('Backup finished at', info.path, 'pack =', info.pack);
});
Tip
For production use, schedule periodic backups (cron, systemd timer,
or an in-app interval) and keep several generations so you can roll back
further than the last snapshot.