|
| 1 | +# coding: utf-8 |
| 2 | +# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | +# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. |
| 4 | + |
| 5 | +# This example demonstrates how to copy boot volume backups to a different region and wait on the copy status. |
| 6 | +# |
| 7 | +# # USAGE: |
| 8 | +# `python examples/copy_boot_volume_backup_example.py \ |
| 9 | +# --volume-backup-id 'foo' \ |
| 10 | +# --destination-region '<destination_region>' \ |
| 11 | +# --display_name 'bar' \ |
| 12 | +# --kms-key-id 'baz'` |
| 13 | +# |
| 14 | +# Example (copying from us-phoenix-1 to eu-frankfurt-1 : |
| 15 | +# `python examples/copy_boot_volume_backup_example.py \ |
| 16 | +# --boot-volume-backup-id 'ocid1.bootvolumebackup.oc1.phx.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' \ |
| 17 | +# --destination-region 'us-ashburn-1' |
| 18 | +# --display-name 'copied backup from phoenix' \ |
| 19 | +# --kms-key-id 'ocid1.key.oc1.iad.aaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'` |
| 20 | +# |
| 21 | +# This script accepts up to for arguments: |
| 22 | +# - boot-volume-backup-id: is the OCID of the boot volume backup to copy. |
| 23 | +# - destination-region: is the name of the region to copy the boot volume backup to. |
| 24 | +# - display_name (optional): is the new display name of the copied boot volume backup. |
| 25 | +# If omitted, the copied boot volume backup will have the same display name as the source backup. |
| 26 | +# - kms-key-id (optional): is the OCID of the kms key to use in the destination region to encrypt |
| 27 | +# the copied backup with. If not specified, a platform ad-master key will be used. |
| 28 | + |
| 29 | + |
| 30 | +import oci |
| 31 | +import argparse |
| 32 | + |
| 33 | +# parse arguments |
| 34 | +parser = argparse.ArgumentParser() |
| 35 | +parser.add_argument('--boot-volume-backup-id', |
| 36 | + help='the OCID of the boot volume backup to copy', |
| 37 | + required=True |
| 38 | + ) |
| 39 | +parser.add_argument('--destination-region', |
| 40 | + help='the name of the destination region to copy the backup to', |
| 41 | + required=True |
| 42 | + ) |
| 43 | + |
| 44 | +parser.add_argument('--display-name', |
| 45 | + help='the display name of the copied boot volume backup. If not specified, ' |
| 46 | + 'defaults to the same as the original backup\'s display name', |
| 47 | + required=False |
| 48 | + ) |
| 49 | + |
| 50 | +parser.add_argument('--kms-key-id', |
| 51 | + help='the OCID of the kms key in the destination region to encrypt the copied boot volume backup', |
| 52 | + required=False |
| 53 | + ) |
| 54 | +args = parser.parse_args() |
| 55 | + |
| 56 | +source_backup_id = args.boot_volume_backup_id |
| 57 | +destination_region = args.destination_region |
| 58 | +kms_key_id = args.kms_key_id |
| 59 | +display_name = args.display_name |
| 60 | + |
| 61 | +# load config and create clients (one for the source region and one for the destination region). |
| 62 | +source_config = oci.config.from_file() |
| 63 | +destination_config = source_config.copy() |
| 64 | +destination_config["region"] = destination_region |
| 65 | +source_blockstorage_client = oci.core.BlockstorageClient(source_config) |
| 66 | +destination_blockstorage_client = oci.core.BlockstorageClient(destination_config) |
| 67 | + |
| 68 | +print('Copying boot volume backup with ID {} from {} to {} using new display name: {} and kms key id: {} \n'.format( |
| 69 | + source_backup_id, source_config["region"], destination_region, display_name, kms_key_id)) |
| 70 | +result = source_blockstorage_client.copy_boot_volume_backup( |
| 71 | + source_backup_id, |
| 72 | + oci.core.models.CopyBootVolumeBackupDetails( |
| 73 | + destination_region=destination_region, |
| 74 | + display_name=display_name, |
| 75 | + kms_key_id=kms_key_id |
| 76 | + ) |
| 77 | +) |
| 78 | + |
| 79 | +print('Copy boot volume backup response status: {}, copied backup: {}\n'.format(result.status, result.data)) |
| 80 | +print('Waiting for the copied backup to be in available state...') |
| 81 | + |
| 82 | +# query the destination region for the copied' backup's status and wait for it to be available. |
| 83 | +copied_backup = oci.wait_until( |
| 84 | + destination_blockstorage_client, |
| 85 | + destination_blockstorage_client.get_boot_volume_backup(result.data.id), |
| 86 | + 'lifecycle_state', |
| 87 | + 'AVAILABLE' |
| 88 | +).data |
| 89 | + |
| 90 | +print('Backup successfully copied: {}'.format(copied_backup)) |
| 91 | +print('Example script done') |
0 commit comments