-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Samples and test for backups admin apis (#2003)
This PR contains samples and its integration tests for admin client backups APIs.
- Loading branch information
1 parent
f34a33a
commit 2d99b5b
Showing
13 changed files
with
1,379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
async function cancelBackup(instanceId, databaseId, backupId, projectId) { | ||
// [START spanner_cancel_backup_create] | ||
|
||
// Imports the Google Cloud client library and precise date library | ||
const {Spanner, protos} = require('@google-cloud/spanner'); | ||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const projectId = 'my-project-id'; | ||
// const instanceId = 'my-instance'; | ||
// const databaseId = 'my-database'; | ||
// const backupId = 'my-backup'; | ||
|
||
// Creates a client | ||
const spanner = new Spanner({ | ||
projectId: projectId, | ||
}); | ||
|
||
// Gets a reference to a Cloud Spanner Database Admin Client object | ||
const databaseAdminClient = spanner.getDatabaseAdminClient(); | ||
|
||
// Creates a new backup of the database | ||
try { | ||
console.log( | ||
`Creating backup of database ${databaseAdminClient.databasePath( | ||
projectId, | ||
instanceId, | ||
databaseId | ||
)}.` | ||
); | ||
|
||
// Expire backup one day in the future | ||
const expireTime = Date.now() + 1000 * 60 * 60 * 24; | ||
const [operation] = await databaseAdminClient.createBackup({ | ||
parent: databaseAdminClient.instancePath(projectId, instanceId), | ||
backupId: backupId, | ||
backup: (protos.google.spanner.admin.database.v1.Backup = { | ||
database: databaseAdminClient.databasePath( | ||
projectId, | ||
instanceId, | ||
databaseId | ||
), | ||
expireTime: Spanner.timestamp(expireTime).toStruct(), | ||
name: databaseAdminClient.backupPath(projectId, instanceId, backupId), | ||
}), | ||
}); | ||
|
||
// Cancel the backup | ||
await operation.cancel(); | ||
|
||
console.log('Backup cancelled.'); | ||
} catch (err) { | ||
console.error('ERROR:', err); | ||
} finally { | ||
// Delete backup in case it got created before the cancel operation | ||
await databaseAdminClient.deleteBackup({ | ||
name: databaseAdminClient.backupPath(projectId, instanceId, backupId), | ||
}); | ||
// Close the spanner client when finished. | ||
// The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient. | ||
spanner.close(); | ||
} | ||
// [END spanner_cancel_backup_create] | ||
} | ||
|
||
module.exports.cancelBackup = cancelBackup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// 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: Copies a source backup | ||
// usage: node spannerCopyBackup <INSTANCE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID> | ||
|
||
'use strict'; | ||
|
||
function main( | ||
instanceId = 'my-instance', | ||
backupId = 'my-backup', | ||
sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup', | ||
projectId = 'my-project-id' | ||
) { | ||
// [START spanner_copy_backup] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const instanceId = 'my-instance'; | ||
// const backupId = 'my-backup', | ||
// const sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup', | ||
// const projectId = 'my-project-id'; | ||
|
||
// Imports the Google Cloud Spanner client library | ||
const {Spanner} = require('@google-cloud/spanner'); | ||
const {PreciseDate} = require('@google-cloud/precise-date'); | ||
|
||
// Creates a client | ||
const spanner = new Spanner({ | ||
projectId: projectId, | ||
}); | ||
|
||
// Gets a reference to a Cloud Spanner Database Admin Client object | ||
const databaseAdminClient = spanner.getDatabaseAdminClient(); | ||
|
||
async function spannerCopyBackup() { | ||
// Expire copy backup 14 days in the future | ||
const expireTime = Spanner.timestamp( | ||
Date.now() + 1000 * 60 * 60 * 24 * 14 | ||
).toStruct(); | ||
|
||
// Copy the source backup | ||
try { | ||
console.log(`Creating copy of the source backup ${sourceBackupPath}.`); | ||
const [operation] = await databaseAdminClient.copyBackup({ | ||
parent: databaseAdminClient.instancePath(projectId, instanceId), | ||
sourceBackup: sourceBackupPath, | ||
backupId: backupId, | ||
expireTime: expireTime, | ||
}); | ||
|
||
console.log( | ||
`Waiting for backup copy ${databaseAdminClient.backupPath( | ||
projectId, | ||
instanceId, | ||
backupId | ||
)} to complete...` | ||
); | ||
await operation.promise(); | ||
|
||
// Verify the copy backup is ready | ||
const [copyBackup] = await databaseAdminClient.getBackup({ | ||
name: databaseAdminClient.backupPath(projectId, instanceId, backupId), | ||
}); | ||
|
||
if (copyBackup.state === 'READY') { | ||
console.log( | ||
`Backup copy ${copyBackup.name} of size ` + | ||
`${copyBackup.sizeBytes} bytes was created at ` + | ||
`${new PreciseDate(copyBackup.createTime).toISOString()} ` + | ||
'with version time ' + | ||
`${new PreciseDate(copyBackup.versionTime).toISOString()}` | ||
); | ||
} else { | ||
console.error('ERROR: Copy of backup is not ready.'); | ||
} | ||
} catch (err) { | ||
console.error('ERROR:', err); | ||
} | ||
} | ||
spannerCopyBackup(); | ||
// [END spanner_copy_backup] | ||
} | ||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
main(...process.argv.slice(2)); |
Oops, something went wrong.