8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3c082d4 commit 8dc6376Copy full SHA for 8dc6376
doc/api/sqlite.md
@@ -508,6 +508,63 @@ exception.
508
| `TEXT` | {string} |
509
| `BLOB` | {TypedArray} or {DataView} |
510
511
+## `sqlite.backup(sourceDb, destination[, options])`
512
+
513
+<!-- YAML
514
+added: REPLACEME
515
+-->
516
517
+* `sourceDb` {DatabaseSync} The database to backup. The source database must be open.
518
+* `destination` {string} The path where the backup will be created. If the file already exists, the contents will be
519
+ overwritten.
520
+* `options` {Object} Optional configuration for the backup. The
521
+ following properties are supported:
522
+ * `source` {string} Name of the source database. This can be `'main'` (the default primary database) or any other
523
+ database that have been added with [`ATTACH DATABASE`][] **Default:** `'main'`.
524
+ * `target` {string} Name of the target database. This can be `'main'` (the default primary database) or any other
525
526
+ * `rate` {number} Number of pages to be transmitted in each batch of the backup. **Default:** `100`.
527
+ * `progress` {Function} Callback function that will be called with the number of pages copied and the total number of
528
+ pages.
529
+* Returns: {Promise} A promise that resolves when the backup is completed and rejects if an error occurs.
530
531
+This method makes a database backup. This method abstracts the [`sqlite3_backup_init()`][], [`sqlite3_backup_step()`][]
532
+and [`sqlite3_backup_finish()`][] functions.
533
534
+The backed-up database can be used normally during the backup process. Mutations coming from the same connection - same
535
+{DatabaseSync} - object will be reflected in the backup right away. However, mutations from other connections will cause
536
+the backup process to restart.
537
538
+```cjs
539
+const { backup, DatabaseSync } = require('node:sqlite');
540
541
+(async () => {
542
+ const sourceDb = new DatabaseSync('source.db');
543
+ const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
544
+ rate: 1, // Copy one page at a time.
545
+ progress: ({ totalPages, remainingPages }) => {
546
+ console.log('Backup in progress', { totalPages, remainingPages });
547
+ },
548
+ });
549
550
+ console.log('Backup completed', totalPagesTransferred);
551
+})();
552
+```
553
554
+```mjs
555
+import { backup, DatabaseSync } from 'node:sqlite';
556
557
+const sourceDb = new DatabaseSync('source.db');
558
+const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
559
560
561
562
563
+});
564
565
+console.log('Backup completed', totalPagesTransferred);
566
567
568
## `sqlite.constants`
569
570
<!-- YAML
@@ -589,6 +646,9 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
589
646
[`SQLITE_DIRECTONLY`]: https://www.sqlite.org/c3ref/c_deterministic.html
590
647
[`SQLITE_MAX_FUNCTION_ARG`]: https://www.sqlite.org/limits.html#max_function_arg
591
648
[`database.applyChangeset()`]: #databaseapplychangesetchangeset-options
649
+[`sqlite3_backup_finish()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
650
+[`sqlite3_backup_init()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
651
+[`sqlite3_backup_step()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
592
652
[`sqlite3_changes64()`]: https://www.sqlite.org/c3ref/changes.html
593
653
[`sqlite3_close_v2()`]: https://www.sqlite.org/c3ref/close.html
594
654
[`sqlite3_create_function_v2()`]: https://www.sqlite.org/c3ref/create_function.html
src/env_properties.h
@@ -77,6 +77,7 @@
77
V(asn1curve_string, "asn1Curve") \
78
V(async_ids_stack_string, "async_ids_stack") \
79
V(attributes_string, "attributes") \
80
+ V(backup_string, "backup") \
81
V(base_string, "base") \
82
V(base_url_string, "baseURL") \
83
V(bits_string, "bits") \
@@ -302,6 +303,7 @@
302
303
V(primordials_string, "primordials") \
304
V(priority_string, "priority") \
305
V(process_string, "process") \
306
+ V(progress_string, "progress") \
307
V(promise_string, "promise") \
308
V(protocol_string, "protocol") \
309
V(prototype_string, "prototype") \
@@ -316,6 +318,7 @@
316
318
V(reason_string, "reason") \
317
319
V(refresh_string, "refresh") \
320
V(regexp_string, "regexp") \
321
+ V(remaining_pages_string, "remainingPages") \
322
V(rename_string, "rename") \
323
V(replacement_string, "replacement") \
324
V(required_module_facade_url_string, \
@@ -369,6 +372,7 @@
369
372
V(time_to_first_byte_sent_string, "timeToFirstByteSent") \
370
373
V(time_to_first_header_string, "timeToFirstHeader") \
371
374
V(tls_ticket_string, "tlsTicket") \
375
+ V(total_pages_string, "totalPages") \
376
V(transfer_string, "transfer") \
377
V(transfer_unsupported_type_str, \
378
"Cannot transfer object of unsupported type.") \