Backup & Restore
Create and restore database backups with MSR Core’s backup workflow and the Firebase adapter.
Table of contents
- Create a Backup
- Backup Contents
- Automatic Backup and Rollback
- Selective Backups
- Restore a Backup
- Recovery Around a Migration
Create a Backup
From TypeScript
import { FirebaseConfig, FirebaseRunner } from '@migration-script-runner/firebase';
const config = new FirebaseConfig();
config.folder = './migrations';
config.databaseUrl = process.env.DATABASE_URL;
config.applicationCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
const runner = await FirebaseRunner.getInstance({ config });
const backupPath = await runner.createBackup();
console.log(`Backup created at ${backupPath}`);
From the CLI
Set DATABASE_URL and GOOGLE_APPLICATION_CREDENTIALS in the CLI process environment before using these commands.
npx msr-firebase backup create
The command prints the created file path. Core stores backups in the configured backup folder.
Backup Contents
The Firebase adapter serializes a path-to-data object. By default, the root database is represented under /:
{
"/": {
"users": {
"user1": { "name": "Ada" }
}
}
}
The backup file contains database data, not a metadata envelope. Treat backup files as sensitive because they may contain the full database.
Automatic Backup and Rollback
Core’s backupMode controls automatic backup behavior. With the defaults backupMode: 'full' and rollbackStrategy: 'backup', Core creates a backup before migration, restores it on failure, and deletes it on success. Other modes are create_only, restore_only, and manual; see Backup configuration.
import { FirebaseConfig, FirebaseRunner } from '@migration-script-runner/firebase';
const config = new FirebaseConfig();
config.folder = './migrations';
config.databaseUrl = process.env.DATABASE_URL;
config.applicationCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
config.backupMode = 'full';
const runner = await FirebaseRunner.getInstance({ config });
await runner.migrate();
If you change either option, see Library Configuration for how they work together.
Selective Backups
Set backupPaths to include selected Firebase paths in Core-managed backups. Paths are relative to shift; omit the setting to preserve full-database backups.
const config = new FirebaseConfig();
config.shift = 'production';
config.backupPaths = ['users', 'settings/theme'];
The equivalent CLI option is:
npx msr-firebase migrate --backup-paths users settings/theme
Restoring a selective backup writes the paths present in the backup. A selected path that was absent when the backup was created is removed if it was added later. Data outside the selected paths is left unchanged. Empty, invalid, duplicate, or overlapping path selections are rejected.
Restore a Backup
From TypeScript
const runner = await FirebaseRunner.getInstance({ config });
await runner.restoreFromBackup('./backups/backup-file.bkp');
Omit backupPath only when restoring a backup created earlier by the same runner instance. For a separate CLI invocation, pass the file path explicitly.
From the CLI
npx msr-firebase backup restore ./backups/backup-file.bkp
The CLI runs in a new process, so pass the path printed by backup create:
npx msr-firebase backup restore "<backupPath>"
Restore writes database data immediately. A full backup can replace the full database; a selective backup changes only its listed paths. Confirm the Firebase project,
shift, backup file, and backup mode before restoring.
Recovery Around a Migration
For an application-managed recovery flow, set backupMode to manual so Core does not create an automatic backup as well. Retain the path returned by createBackup() and use that exact path if migration fails:
import { FirebaseConfig, FirebaseRunner } from '@migration-script-runner/firebase';
const config = new FirebaseConfig();
config.folder = './migrations';
config.databaseUrl = process.env.DATABASE_URL;
config.applicationCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
config.backupMode = 'manual';
const runner = await FirebaseRunner.getInstance({ config });
const backupPath = await runner.createBackup();
const result = await runner.migrate();
if (!result.success) {
await runner.restoreFromBackup(backupPath);
throw result.errors?.[0] ?? new Error('Migration failed');
}
The backup file must still be available to the process. For manual storage, retention, cloud uploads, and compression, see Backup Storage and Retention.