This method makes a database backup. This method abstracts the sqlite3_backup_init()
, sqlite3_backup_step()
and sqlite3_backup_finish()
functions.
The backed-up database can be used normally during the backup process. Mutations coming from the same connection - same DatabaseSync
- object will be reflected in the backup right away. However, mutations from other connections will cause the backup process to restart.
import { backup, DatabaseSync } from 'node:sqlite';
const sourceDb = new DatabaseSync('source.db');
const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
rate: 1, // Copy one page at a time.
progress: ({ totalPages, remainingPages }) => {
console.log('Backup in progress', { totalPages, remainingPages });
},
});
console.log('Backup completed', totalPagesTransferred);