diff --git a/README.md b/README.md index 277d9feb9..dd197448a 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre | Backups | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups.js,samples/README.md) | | Batch Write | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/batch-write.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/batch-write.js,samples/README.md) | | Batch | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/batch.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/batch.js,samples/README.md) | +| Creates a full backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-full-backup-schedule.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/create-full-backup-schedule.js,samples/README.md) | +| Creates an incremental backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-incremental-backup-schedule.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/create-incremental-backup-schedule.js,samples/README.md) | | CRUD | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/crud.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/crud.js,samples/README.md) | | Creates a new database with a specific default leader | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-default-leader.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-default-leader.js,samples/README.md) | | Database-create-with-encryption-key | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-encryption-key.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-encryption-key.js,samples/README.md) | @@ -113,12 +115,14 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre | Updates the default leader of an existing database | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-update-default-leader.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-update-default-leader.js,samples/README.md) | | Updates a Cloud Spanner Database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-update.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-update.js,samples/README.md) | | Datatypes | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/datatypes.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/datatypes.js,samples/README.md) | +| Deletes a backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/delete-backup-schedule.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/delete-backup-schedule.js,samples/README.md) | | Runs an execute sql request with directed read options | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/directed-reads.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/directed-reads.js,samples/README.md) | | Delete using DML returning. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/dml-returning-delete.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/dml-returning-delete.js,samples/README.md) | | Insert using DML returning. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/dml-returning-insert.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/dml-returning-insert.js,samples/README.md) | | Update using DML returning. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/dml-returning-update.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/dml-returning-update.js,samples/README.md) | | DML | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/dml.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/dml.js,samples/README.md) | | Enable fine grained access control | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/enable-fine-grained-access.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/enable-fine-grained-access.js,samples/README.md) | +| Gets a backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/get-backup-schedule.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/get-backup-schedule.js,samples/README.md) | | Get-commit-stats | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/get-commit-stats.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/get-commit-stats.js,samples/README.md) | | List database roles | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/get-database-roles.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/get-database-roles.js,samples/README.md) | | Gets the instance config metadata for the configuration nam6 | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/get-instance-config.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/get-instance-config.js,samples/README.md) | @@ -140,6 +144,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre | Json-add-column | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/json-add-column.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/json-add-column.js,samples/README.md) | | Json-query-parameter | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/json-query-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/json-query-parameter.js,samples/README.md) | | Json-update-data | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/json-update-data.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/json-update-data.js,samples/README.md) | +| Lists backup schedules of a database | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-backup-schedules.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/list-backup-schedules.js,samples/README.md) | | Lists all databases on the selected instance | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-databases.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/list-databases.js,samples/README.md) | | Lists all the available instance configs for the selected project. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-instance-configs.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/list-instance-configs.js,samples/README.md) | | Executes request with max commit delay | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/max-commit-delay.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/max-commit-delay.js,samples/README.md) | @@ -195,6 +200,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre | Timestamp | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/timestamp.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/timestamp.js,samples/README.md) | | Executes a read/write transaction with transaction and request tags | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction-tag.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/transaction-tag.js,samples/README.md) | | Transaction | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/transaction.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/transaction.js,samples/README.md) | +| Updates a backup schedule | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/update-backup-schedule.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/update-backup-schedule.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index 904cf9a5f..bdbd28844 100644 --- a/samples/README.md +++ b/samples/README.md @@ -29,6 +29,8 @@ and automatic, synchronous replication for high availability. * [Backups](#backups) * [Batch Write](#batch-write) * [Batch](#batch) + * [Creates a full backup schedule](#creates-a-full-backup-schedule) + * [Creates an incremental backup schedule](#creates-an-incremental-backup-schedule) * [CRUD](#crud) * [Creates a new database with a specific default leader](#creates-a-new-database-with-a-specific-default-leader) * [Database-create-with-encryption-key](#database-create-with-encryption-key) @@ -38,12 +40,14 @@ and automatic, synchronous replication for high availability. * [Updates the default leader of an existing database](#updates-the-default-leader-of-an-existing-database) * [Updates a Cloud Spanner Database.](#updates-a-cloud-spanner-database.) * [Datatypes](#datatypes) + * [Deletes a backup schedule](#deletes-a-backup-schedule) * [Runs an execute sql request with directed read options](#runs-an-execute-sql-request-with-directed-read-options) * [Delete using DML returning.](#delete-using-dml-returning.) * [Insert using DML returning.](#insert-using-dml-returning.) * [Update using DML returning.](#update-using-dml-returning.) * [DML](#dml) * [Enable fine grained access control](#enable-fine-grained-access-control) + * [Gets a backup schedule](#gets-a-backup-schedule) * [Get-commit-stats](#get-commit-stats) * [List database roles](#list-database-roles) * [Gets the instance config metadata for the configuration nam6](#gets-the-instance-config-metadata-for-the-configuration-nam6) @@ -65,6 +69,7 @@ and automatic, synchronous replication for high availability. * [Json-add-column](#json-add-column) * [Json-query-parameter](#json-query-parameter) * [Json-update-data](#json-update-data) + * [Lists backup schedules of a database](#lists-backup-schedules-of-a-database) * [Lists all databases on the selected instance](#lists-all-databases-on-the-selected-instance) * [Lists all the available instance configs for the selected project.](#lists-all-the-available-instance-configs-for-the-selected-project.) * [Executes request with max commit delay](#executes-request-with-max-commit-delay) @@ -120,6 +125,7 @@ and automatic, synchronous replication for high availability. * [Timestamp](#timestamp) * [Executes a read/write transaction with transaction and request tags](#executes-a-read/write-transaction-with-transaction-and-request-tags) * [Transaction](#transaction) + * [Updates a backup schedule](#updates-a-backup-schedule) ## Before you begin @@ -391,6 +397,40 @@ __Usage:__ +### Creates a full backup schedule + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-full-backup-schedule.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/create-full-backup-schedule.js,samples/README.md) + +__Usage:__ + + +`node create-full-backup-schedule.js ` + + +----- + + + + +### Creates an incremental backup schedule + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/create-incremental-backup-schedule.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/create-incremental-backup-schedule.js,samples/README.md) + +__Usage:__ + + +`node create-incremental-backup-schedule.js ` + + +----- + + + + ### CRUD View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/crud.js). @@ -544,6 +584,23 @@ __Usage:__ +### Deletes a backup schedule + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/delete-backup-schedule.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/delete-backup-schedule.js,samples/README.md) + +__Usage:__ + + +`node delete-backup-schedule.js ` + + +----- + + + + ### Runs an execute sql request with directed read options View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/directed-reads.js). @@ -646,6 +703,23 @@ __Usage:__ +### Gets a backup schedule + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/get-backup-schedule.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/get-backup-schedule.js,samples/README.md) + +__Usage:__ + + +`node get-backup-schedule.js ` + + +----- + + + + ### Get-commit-stats View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/get-commit-stats.js). @@ -1003,6 +1077,23 @@ __Usage:__ +### Lists backup schedules of a database + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-backup-schedules.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/list-backup-schedules.js,samples/README.md) + +__Usage:__ + + +`node list-backup-schedules.js ` + + +----- + + + + ### Lists all databases on the selected instance View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/list-databases.js). @@ -1933,6 +2024,23 @@ __Usage:__ `node samples/transaction.js` +----- + + + + +### Updates a backup schedule + +View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/update-backup-schedule.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/update-backup-schedule.js,samples/README.md) + +__Usage:__ + + +`node update-backup-schedule.js ` + + diff --git a/samples/create-full-backup-schedule.js b/samples/create-full-backup-schedule.js new file mode 100644 index 000000000..4188b2be3 --- /dev/null +++ b/samples/create-full-backup-schedule.js @@ -0,0 +1,80 @@ +/** + * Copyright 2024 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Creates a full backup schedule +// usage: node create-full-backup-schedule.js + +'use strict'; + +function main( + projectId = 'my-project-id', + instanceId = 'my-instance-id', + databaseId = 'my-database-id', + scheduleId = 'my-schedule-id' +) { + async function createFullBackupSchedule() { + // [START spanner_create_full_backup_schedule] + // Import the Google Cloud client library for Spanner. + const {Spanner} = require('@google-cloud/spanner'); + + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance-id'; + // const databaseId = 'my-database-id'; + // const scheduleId = 'my-schedule-id'; + + // Create a Spanner database admin client. + const spanner = new Spanner({projectId}); + const client = spanner.getDatabaseAdminClient(); + + try { + // Create a schedule to create full backups daily at 12:30 AM, using the + // database's encryption config, and retained for 24 hours. + const [response] = await client.createBackupSchedule({ + parent: client.databasePath(projectId, instanceId, databaseId), + backupScheduleId: scheduleId, + backupSchedule: { + spec: { + cronSpec: { + text: '30 12 * * *', + }, + }, + retentionDuration: { + seconds: 86400, + }, + encryptionConfig: { + encryptionType: 'USE_DATABASE_ENCRYPTION', + }, + fullBackupSpec: {}, + }, + }); + console.log('Created full backup schedule:', response); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_create_full_backup_schedule] + } + + createFullBackupSchedule(); +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/samples/create-incremental-backup-schedule.js b/samples/create-incremental-backup-schedule.js new file mode 100644 index 000000000..fc0bc88d8 --- /dev/null +++ b/samples/create-incremental-backup-schedule.js @@ -0,0 +1,80 @@ +/** + * Copyright 2024 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Creates an incremental backup schedule +// usage: node create-incremental-backup-schedule.js + +'use strict'; + +function main( + projectId = 'my-project-id', + instanceId = 'my-instance-id', + databaseId = 'my-database-id', + scheduleId = 'my-schedule-id' +) { + async function createIncrementalBackupSchedule() { + // [START spanner_create_incremental_backup_schedule] + // Import the Google Cloud client library for Spanner. + const {Spanner} = require('@google-cloud/spanner'); + + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance-id'; + // const databaseId = 'my-database-id'; + // const scheduleId = 'my-schedule-id'; + + // Create a Spanner database admin client. + const spanner = new Spanner({projectId}); + const client = spanner.getDatabaseAdminClient(); + + try { + // Create a schedule to create incremental backups daily at 12:30 AM, + // using Google-managed encryption, and retained for 24 hours. + const [response] = await client.createBackupSchedule({ + parent: client.databasePath(projectId, instanceId, databaseId), + backupScheduleId: scheduleId, + backupSchedule: { + spec: { + cronSpec: { + text: '30 12 * * *', + }, + }, + retentionDuration: { + seconds: 86400, + }, + encryptionConfig: { + encryptionType: 'GOOGLE_DEFAULT_ENCRYPTION', + }, + incrementalBackupSpec: {}, + }, + }); + console.log('Created incremental backup schedule:', response); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_create_incremental_backup_schedule] + } + + createIncrementalBackupSchedule(); +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/samples/delete-backup-schedule.js b/samples/delete-backup-schedule.js new file mode 100644 index 000000000..4b48db26a --- /dev/null +++ b/samples/delete-backup-schedule.js @@ -0,0 +1,69 @@ +/** + * Copyright 2024 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Deletes a backup schedule +// usage: node delete-backup-schedule.js + +'use strict'; + +function main( + projectId = 'my-project-id', + instanceId = 'my-instance-id', + databaseId = 'my-database-id', + scheduleId = 'my-schedule-id' +) { + async function deleteBackupSchedule() { + // [START spanner_delete_backup_schedule] + // Import the Google Cloud client library for Spanner. + const {Spanner} = require('@google-cloud/spanner'); + + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance-id'; + // const databaseId = 'my-database-id'; + // const scheduleId = 'my-schedule-id'; + + // Create a Spanner database admin client. + const spanner = new Spanner({projectId}); + const client = spanner.getDatabaseAdminClient(); + + try { + // Delete the backup schedule. + await client.deleteBackupSchedule({ + name: client.backupSchedulePath( + projectId, + instanceId, + databaseId, + scheduleId + ), + }); + console.log('Deleted backup schedule'); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_delete_backup_schedule] + } + + deleteBackupSchedule(); +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/samples/get-backup-schedule.js b/samples/get-backup-schedule.js new file mode 100644 index 000000000..1ad5dced0 --- /dev/null +++ b/samples/get-backup-schedule.js @@ -0,0 +1,69 @@ +/** + * Copyright 2024 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Gets a backup schedule +// usage: node get-backup-schedule.js + +'use strict'; + +function main( + projectId = 'my-project-id', + instanceId = 'my-instance-id', + databaseId = 'my-database-id', + scheduleId = 'my-schedule-id' +) { + async function getBackupSchedule() { + // [START spanner_get_backup_schedule] + // Import the Google Cloud client library for Spanner. + const {Spanner} = require('@google-cloud/spanner'); + + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance-id'; + // const databaseId = 'my-database-id'; + // const scheduleId = 'my-schedule-id'; + + // Create a Spanner database admin client. + const spanner = new Spanner({projectId}); + const client = spanner.getDatabaseAdminClient(); + + try { + // Get the backup schedule. + const [response] = await client.getBackupSchedule({ + name: client.backupSchedulePath( + projectId, + instanceId, + databaseId, + scheduleId + ), + }); + console.log('Backup schedule:', response); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_get_backup_schedule] + } + + getBackupSchedule(); +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/samples/list-backup-schedules.js b/samples/list-backup-schedules.js new file mode 100644 index 000000000..5e972de28 --- /dev/null +++ b/samples/list-backup-schedules.js @@ -0,0 +1,64 @@ +/** + * Copyright 2024 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Lists backup schedules of a database +// usage: node list-backup-schedules.js + +'use strict'; + +function main( + projectId = 'my-project-id', + instanceId = 'my-instance-id', + databaseId = 'my-database-id' +) { + async function listBackupSchedules() { + // [START spanner_list_backup_schedules] + // Import the Google Cloud client library for Spanner. + const {Spanner} = require('@google-cloud/spanner'); + + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance-id'; + // const databaseId = 'my-database-id'; + + // Create a Spanner database admin client. + const spanner = new Spanner({projectId}); + const client = spanner.getDatabaseAdminClient(); + + try { + // List backup schedules of a database. + const [schedules] = await client.listBackupSchedules({ + parent: client.databasePath(projectId, instanceId, databaseId), + }); + schedules.forEach(schedule => { + console.log('Backup schedule:', schedule); + }); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_list_backup_schedules] + } + + listBackupSchedules(); +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 52437c641..384e3fc58 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -1244,6 +1244,82 @@ describe('Autogenerated Admin Clients', () => { ); }); + it('shoud create a full backup schedule', async () => { + const output = execSync( + `node create-full-backup-schedule.js ${PROJECT_ID} ${INSTANCE_ID} ${DATABASE_ID} full-backup-schedule` + ); + assert.match(output, new RegExp('Created full backup schedule')); + assert.match( + output, + new RegExp( + `projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_ID}/backupSchedules/full-backup-schedule` + ) + ); + }); + + it('shoud create an incremental backup schedule', async () => { + const output = execSync( + `node create-incremental-backup-schedule.js ${PROJECT_ID} ${INSTANCE_ID} ${DATABASE_ID} incremental-backup-schedule` + ); + assert.match(output, new RegExp('Created incremental backup schedule')); + assert.match( + output, + new RegExp( + `projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_ID}/backupSchedules/incremental-backup-schedule` + ) + ); + }); + + it('shoud list backup schedules', async () => { + const output = execSync( + `node list-backup-schedules.js ${PROJECT_ID} ${INSTANCE_ID} ${DATABASE_ID}` + ); + assert.match( + output, + new RegExp( + `projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_ID}/backupSchedules/full-backup-schedule` + ) + ); + assert.match( + output, + new RegExp( + `projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_ID}/backupSchedules/incremental-backup-schedule` + ) + ); + }); + + it('shoud get a backup schedule', async () => { + const output = execSync( + `node get-backup-schedule.js ${PROJECT_ID} ${INSTANCE_ID} ${DATABASE_ID} full-backup-schedule` + ); + assert.match( + output, + new RegExp( + `projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_ID}/backupSchedules/full-backup-schedule` + ) + ); + }); + + it('shoud update a backup schedule', async () => { + const output = execSync( + `node update-backup-schedule.js ${PROJECT_ID} ${INSTANCE_ID} ${DATABASE_ID} full-backup-schedule` + ); + assert.match(output, new RegExp('Updated backup schedule')); + assert.match( + output, + new RegExp( + `projects/${PROJECT_ID}/instances/${INSTANCE_ID}/databases/${DATABASE_ID}/backupSchedules/full-backup-schedule` + ) + ); + }); + + it('shoud delete a backup schedule', async () => { + const output = execSync( + `node delete-backup-schedule.js ${PROJECT_ID} ${INSTANCE_ID} ${DATABASE_ID} full-backup-schedule` + ); + assert.match(output, new RegExp('Deleted backup schedule')); + }); + // create_backup it('should create a backup of the database', async () => { const instance = spanner.instance(INSTANCE_ID); diff --git a/samples/update-backup-schedule.js b/samples/update-backup-schedule.js new file mode 100644 index 000000000..c2b6f18e6 --- /dev/null +++ b/samples/update-backup-schedule.js @@ -0,0 +1,90 @@ +/** + * Copyright 2024 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Updates a backup schedule +// usage: node update-backup-schedule.js + +'use strict'; + +function main( + projectId = 'my-project-id', + instanceId = 'my-instance-id', + databaseId = 'my-database-id', + scheduleId = 'my-schedule-id' +) { + async function updateBackupSchedule() { + // [START spanner_update_backup_schedule] + // Import the Google Cloud client library for Spanner. + const {Spanner} = require('@google-cloud/spanner'); + + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project-id'; + // const instanceId = 'my-instance-id'; + // const databaseId = 'my-database-id'; + // const scheduleId = 'my-schedule-id'; + + // Create a Spanner database admin client. + const spanner = new Spanner({projectId}); + const client = spanner.getDatabaseAdminClient(); + + try { + // Update the schedule to create backups daily at 3:45 PM, using the + // database's encryption config, and retained for 48 hours. + const [response] = await client.updateBackupSchedule({ + backupSchedule: { + name: client.backupSchedulePath( + projectId, + instanceId, + databaseId, + scheduleId + ), + spec: { + cronSpec: { + text: '45 15 * * *', + }, + }, + retentionDuration: { + seconds: 172800, + }, + encryptionConfig: { + encryptionType: 'USE_DATABASE_ENCRYPTION', + }, + }, + updateMask: { + paths: [ + 'spec.cron_spec.text', + 'retention_duration', + 'encryption_config', + ], + }, + }); + console.log('Updated backup schedule:', response); + } catch (err) { + console.error('ERROR:', err); + } + // [END spanner_update_backup_schedule] + } + + updateBackupSchedule(); +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2));