docs/backup.md
Easily back up your RxDB database to JSON files and attachments on the filesystem with the Backup Plugin - ensuring reliable Node.js data protection.
With the backup plugin you can write the current database state and ongoing changes into folders on the filesystem. The files are written in plain json together with their attachments so that you can read them out with any software or tools, without being bound to RxDB.
This is useful to:
The backup plugin works only in node.js, not in a browser. It is intended to have a backup strategy when using RxDB on the server side like with the RxServer. To run backups on the client side, you should use one of the replication plugins instead.
import { addRxPlugin } from 'rxdb';
import { RxDBBackupPlugin } from 'rxdb/plugins/backup';
addRxPlugin(RxDBBackupPlugin);
Write the whole database to the filesystem once. When called multiple times, it will continue from the last checkpoint and not start all over again.
const backupOptions = {
// if false, a one-time backup will be written
live: false,
// the folder where the backup will be stored
directory: '/my-backup-folder/',
// if true, attachments will also be saved
attachments: true
}
const backupState = myDatabase.backup(backupOptions);
await backupState.awaitInitialBackup();
// call again to run from the last checkpoint
const backupState2 = myDatabase.backup(backupOptions);
await backupState2.awaitInitialBackup();
When live: true is set, the backup will write all ongoing changes to the backup directory.
const backupOptions = {
// set live: true to have an ongoing backup
live: true,
directory: '/my-backup-folder/',
attachments: true
}
const backupState = myDatabase.backup(backupOptions);
// you can still await the initial backup write,
// but further changes will still be processed.
await backupState.awaitInitialBackup();
You can listen to the writeEvents$ Observable to get notified about written backup files.
const backupOptions = {
live: false,
directory: '/my-backup-folder/',
attachments: true
}
const backupState = myDatabase.backup(backupOptions);
const subscription = backupState.writeEvents$
.subscribe(writeEvent =>
console.dir(writeEvent)
);
/*
> {
collectionName: 'humans',
documentId: 'foobar',
files: [
'/my-backup-folder/foobar/document.json'
],
deleted: false
}
*/