M
sqlite.backup
History
Added in: v23.8.0, v22.16.0
v23.8.0, v22.16.0
Introduced in: v22.5.0
v22.5.0
The
path argument now supports Buffer and URL objects.v23.10.0
sqlite.backup(sourceDb, path, options?): Promise
Attributes
sourceDb:
DatabaseSyncThe database to backup. The source database must be open.
The path where the backup will be created. If the file already exists,
the contents will be overwritten.
options:
ObjectOptional configuration for the backup. The
following properties are supported:
source?:
stringName of the source database. This can be
'main' (the default primary database) or any other
database that have been added with ATTACH DATABASE Default: 'main'.target?:
stringName of the target database. This can be
'main' (the default primary database) or any other
database that have been added with ATTACH DATABASE Default: 'main'.rate?:
integerPositive number of pages to be transmitted in each batch of the backup. Default:
100.Returns:
PromiseA promise that fulfills with the total number of backed-up pages upon completion, or rejects if an
error occurs.
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.
const { backup, DatabaseSync } = require('node:sqlite'); (async () => { 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); })();
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);