@@ -551,6 +551,63 @@ exception.
551551| ` TEXT ` | {string} |
552552| ` BLOB ` | {TypedArray} or {DataView} |
553553
554+ ## ` sqlite.backup(sourceDb, destination[, options]) `
555+
556+ <!-- YAML
557+ added: REPLACEME
558+ -->
559+
560+ * ` sourceDb ` {DatabaseSync} The database to backup. The source database must be open.
561+ * ` destination ` {string} The path where the backup will be created. If the file already exists, the contents will be
562+ overwritten.
563+ * ` options ` {Object} Optional configuration for the backup. The
564+ following properties are supported:
565+ * ` source ` {string} Name of the source database. This can be ` 'main' ` (the default primary database) or any other
566+ database that have been added with [ ` ATTACH DATABASE ` ] [ ] ** Default:** ` 'main' ` .
567+ * ` target ` {string} Name of the target database. This can be ` 'main' ` (the default primary database) or any other
568+ database that have been added with [ ` ATTACH DATABASE ` ] [ ] ** Default:** ` 'main' ` .
569+ * ` rate ` {number} Number of pages to be transmitted in each batch of the backup. ** Default:** ` 100 ` .
570+ * ` progress ` {Function} Callback function that will be called with the number of pages copied and the total number of
571+ pages.
572+ * Returns: {Promise} A promise that resolves when the backup is completed and rejects if an error occurs.
573+
574+ This method makes a database backup. This method abstracts the [ ` sqlite3_backup_init() ` ] [ ] , [ ` sqlite3_backup_step() ` ] [ ]
575+ and [ ` sqlite3_backup_finish() ` ] [ ] functions.
576+
577+ The backed-up database can be used normally during the backup process. Mutations coming from the same connection - same
578+ {DatabaseSync} - object will be reflected in the backup right away. However, mutations from other connections will cause
579+ the backup process to restart.
580+
581+ ``` cjs
582+ const { backup , DatabaseSync } = require (' node:sqlite' );
583+
584+ (async () => {
585+ const sourceDb = new DatabaseSync (' source.db' );
586+ const totalPagesTransferred = await backup (sourceDb, ' backup.db' , {
587+ rate: 1 , // Copy one page at a time.
588+ progress : ({ totalPages, remainingPages }) => {
589+ console .log (' Backup in progress' , { totalPages, remainingPages });
590+ },
591+ });
592+
593+ console .log (' Backup completed' , totalPagesTransferred);
594+ })();
595+ ```
596+
597+ ``` mjs
598+ import { backup , DatabaseSync } from ' node:sqlite' ;
599+
600+ const sourceDb = new DatabaseSync (' source.db' );
601+ const totalPagesTransferred = await backup (sourceDb, ' backup.db' , {
602+ rate: 1 , // Copy one page at a time.
603+ progress : ({ totalPages, remainingPages }) => {
604+ console .log (' Backup in progress' , { totalPages, remainingPages });
605+ },
606+ });
607+
608+ console .log (' Backup completed' , totalPagesTransferred);
609+ ```
610+
554611## ` sqlite.constants `
555612
556613<!-- YAML
@@ -632,6 +689,9 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
632689[ `SQLITE_DIRECTONLY` ] : https://www.sqlite.org/c3ref/c_deterministic.html
633690[ `SQLITE_MAX_FUNCTION_ARG` ] : https://www.sqlite.org/limits.html#max_function_arg
634691[ `database.applyChangeset()` ] : #databaseapplychangesetchangeset-options
692+ [ `sqlite3_backup_finish()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
693+ [ `sqlite3_backup_init()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
694+ [ `sqlite3_backup_step()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
635695[ `sqlite3_changes64()` ] : https://www.sqlite.org/c3ref/changes.html
636696[ `sqlite3_close_v2()` ] : https://www.sqlite.org/c3ref/close.html
637697[ `sqlite3_create_function_v2()` ] : https://www.sqlite.org/c3ref/create_function.html
0 commit comments