Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Backups code samples to Rubric #2082

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
79 changes: 43 additions & 36 deletions samples/backups-cancel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

'use strict';

async function cancelBackup(instanceId, databaseId, backupId, projectId) {
function main(instanceId, databaseId, backupId, projectId) {
// [START spanner_cancel_backup_create]

// Imports the Google Cloud client library and precise date library
Expand All @@ -37,47 +37,54 @@ async function cancelBackup(instanceId, databaseId, backupId, projectId) {
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(
async function cancelBackup() {
try {
console.log(
`Creating backup of database ${databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
expireTime: Spanner.timestamp(expireTime).toStruct(),
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
}),
});
)}.`
);

// Cancel the backup
await operation.cancel();
// 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),
}),
});

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();
// 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();
}
}
cancelBackup();
// [END spanner_cancel_backup_create]
}

module.exports.cancelBackup = cancelBackup;
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
121 changes: 61 additions & 60 deletions samples/backups-create-with-encryption-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@

'use strict';

async function createBackupWithEncryptionKey(
instanceId,
databaseId,
backupId,
projectId,
keyName
) {
function main(instanceId, databaseId, backupId, projectId, keyName) {
// [START spanner_create_backup_with_encryption_key]

// Imports the Google Cloud client library
Expand All @@ -47,68 +41,75 @@ async function createBackupWithEncryptionKey(
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 14 days in the future
const expireTime = Date.now() + 1000 * 60 * 60 * 24 * 14;

// Create a backup of the state of the database at the current time.
const [operation] = await databaseAdminClient.createBackup({
parent: databaseAdminClient.instancePath(projectId, instanceId),
backupId: backupId,
backup: (protos.google.spanner.admin.database.v1.Backup = {
database: databaseAdminClient.databasePath(
async function createBackupWithEncryptionKey() {
try {
console.log(
`Creating backup of database ${databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
expireTime: Spanner.timestamp(expireTime).toStruct(),
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
}),
encryptionConfig: {
encryptionType: 'CUSTOMER_MANAGED_ENCRYPTION',
kmsKeyName: keyName,
},
});
)}.`
);

console.log(
`Waiting for backup ${databaseAdminClient.backupPath(
projectId,
instanceId,
backupId
)} to complete...`
);
await operation.promise();
// Expire backup 14 days in the future
const expireTime = Date.now() + 1000 * 60 * 60 * 24 * 14;

// Create a backup of the state of the database at the current time.
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),
}),
encryptionConfig: {
encryptionType: 'CUSTOMER_MANAGED_ENCRYPTION',
kmsKeyName: keyName,
},
});

// Verify backup is ready
const [backupInfo] = await databaseAdminClient.getBackup({
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
});
if (backupInfo.state === 'READY') {
console.log(
`Backup ${backupInfo.name} of size ` +
`${backupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(backupInfo.createTime).toISOString()} ` +
`using encryption key ${backupInfo.encryptionInfo.kmsKeyVersion}`
`Waiting for backup ${databaseAdminClient.backupPath(
projectId,
instanceId,
backupId
)} to complete...`
);
} else {
console.error('ERROR: Backup is not ready.');
await operation.promise();

// Verify backup is ready
const [backupInfo] = await databaseAdminClient.getBackup({
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
});
if (backupInfo.state === 'READY') {
console.log(
`Backup ${backupInfo.name} of size ` +
`${backupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(backupInfo.createTime).toISOString()} ` +
`using encryption key ${backupInfo.encryptionInfo.kmsKeyVersion}`
);
} else {
console.error('ERROR: Backup is not ready.');
}
} catch (err) {
console.error('ERROR:', err);
} finally {
// 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();
}
} catch (err) {
console.error('ERROR:', err);
} finally {
// 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();
}
createBackupWithEncryptionKey();
// [END spanner_create_backup_with_encryption_key]
}

module.exports.createBackupWithEncryptionKey = createBackupWithEncryptionKey;
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
117 changes: 59 additions & 58 deletions samples/backups-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@

'use strict';

async function createBackup(
instanceId,
databaseId,
backupId,
projectId,
versionTime
) {
function main(instanceId, databaseId, backupId, projectId, versionTime) {
// [START spanner_create_backup]

// Imports the Google Cloud client library and precise date library
Expand All @@ -46,66 +40,73 @@ async function createBackup(
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 14 days in the future
const expireTime = Date.now() + 1000 * 60 * 60 * 24 * 14;

// Create a backup of the state of the database at the current time.
const [operation] = await databaseAdminClient.createBackup({
parent: databaseAdminClient.instancePath(projectId, instanceId),
backupId: backupId,
backup: (protos.google.spanner.admin.database.v1.Backup = {
database: databaseAdminClient.databasePath(
async function createBackup() {
try {
console.log(
`Creating backup of database ${databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
expireTime: Spanner.timestamp(expireTime).toStruct(),
versionTime: Spanner.timestamp(versionTime).toStruct(),
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
}),
});
)}.`
);

console.log(
`Waiting for backup ${databaseAdminClient.backupPath(
projectId,
instanceId,
backupId
)} to complete...`
);
await operation.promise();
// Expire backup 14 days in the future
const expireTime = Date.now() + 1000 * 60 * 60 * 24 * 14;

// Create a backup of the state of the database at the current time.
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(),
versionTime: Spanner.timestamp(versionTime).toStruct(),
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
}),
});

// Verify backup is ready
const [backupInfo] = await databaseAdminClient.getBackup({
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
});
if (backupInfo.state === 'READY') {
console.log(
`Backup ${backupInfo.name} of size ` +
`${backupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(backupInfo.createTime).toISOString()} ` +
'for version of database at ' +
`${new PreciseDate(backupInfo.versionTime).toISOString()}`
`Waiting for backup ${databaseAdminClient.backupPath(
projectId,
instanceId,
backupId
)} to complete...`
);
} else {
console.error('ERROR: Backup is not ready.');
await operation.promise();

// Verify backup is ready
const [backupInfo] = await databaseAdminClient.getBackup({
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
});
if (backupInfo.state === 'READY') {
console.log(
`Backup ${backupInfo.name} of size ` +
`${backupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(backupInfo.createTime).toISOString()} ` +
'for version of database at ' +
`${new PreciseDate(backupInfo.versionTime).toISOString()}`
);
} else {
console.error('ERROR: Backup is not ready.');
}
} catch (err) {
console.error('ERROR:', err);
} finally {
// 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();
}
} catch (err) {
console.error('ERROR:', err);
} finally {
// 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();
}
createBackup();
// [END spanner_create_backup]
}

module.exports.createBackup = createBackup;
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
Loading
Loading