sqlite.backup(sourceDb, path[, options])
-
sourceDb
DatabaseSync 要备份的数据库。源数据库必须处于打开状态。¥
sourceDb
DatabaseSync The database to backup. The source database must be open. -
path
<string> | <Buffer> | <URL> 将创建备份的路径。如果文件已存在,则内容将被覆盖。¥
path
<string> | <Buffer> | <URL> The path where the backup will be created. If the file already exists, the contents will be overwritten. -
options
<Object> 备份的可选配置。支持以下属性:¥
options
<Object> Optional configuration for the backup. The following properties are supported:-
source
<string> 源数据库的名称。这可以是'main'
(默认主数据库)或任何其他已添加ATTACH DATABASE
默认值的数据库:'main'
。¥
source
<string> Name of the source database. This can be'main'
(the default primary database) or any other database that have been added withATTACH DATABASE
Default:'main'
. -
target
<string> 目标数据库的名称。这可以是'main'
(默认主数据库)或任何其他已添加ATTACH DATABASE
默认值的数据库:'main'
。¥
target
<string> Name of the target database. This can be'main'
(the default primary database) or any other database that have been added withATTACH DATABASE
Default:'main'
. -
rate
<number> 每批备份中要传输的页数。默认值:100
。¥
rate
<number> Number of pages to be transmitted in each batch of the backup. Default:100
. -
progress
<Function> 将使用复制的页数和总页数调用的回调函数。¥
progress
<Function> Callback function that will be called with the number of pages copied and the total number of pages.
-
-
返回:<Promise> 备份完成时解析的 promise,发生错误时拒绝的 promise。
¥Returns: <Promise> A promise that resolves when the backup is completed and rejects if an error occurs.
此方法进行数据库备份。此方法抽象了 sqlite3_backup_init()
、sqlite3_backup_step()
和 sqlite3_backup_finish()
函数。
¥This method makes a database backup. This method abstracts the sqlite3_backup_init()
, sqlite3_backup_step()
and sqlite3_backup_finish()
functions.
备份数据库可在备份过程中正常使用。来自同一连接的修改 - 相同的 DatabaseSync - 对象将立即反映在备份中。但是,来自其他连接的突变将导致备份过程重新启动。
¥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);