Snapshot
Create a snapshot
The following code enables the creation of snapshots from specified disks, supporting both system disks and CDS disks.
To successfully create a snapshot of a system disk, the instance must be in either the Running or Stopped state.
When creating a snapshot for a CDS disk, the disk must be in the "InUse" or "Available" state for the operation to succeed.
The number of snapshots an account can create depends on the total number of disks, with the current limit set at 8 times the total number of disks.
1import uuid
2from baidubce.auth.bce_credentials import BceCredentials
3from baidubce.bce_client_configuration import BceClientConfiguration
4from baidubce.services.bcc import bcc_client
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15 client_token = str(uuid.uuid4())
16# Set snapshot name
17 snapshot_name = 'snapshot_' + client_token
18# Disk ID for Snapshot Creation
19 volume_id = 'v-***'
20 resp = client.create_snapshot(volume_id, snapshot_name, client_token=client_token)
21 print(resp)
Query the snapshot list
The following code can be used to query all snapshot information, including snapshots of system disks and data disks:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Disk ID for Snapshot Creation
15 volume_id = 'v-***'
16# Starting position of the batch list query
17 marker = "your-marker"
18# Maximum number of items included per page
19 max_keys = 100
20 resp = client.list_snapshots(marker=marker, max_keys=max_keys, volume_id=volume_id)
21 print(resp)
Query snapshot details
The following code can be used to query the detailed information of a single snapshot by specified snapshot ID:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Snapshot ID
15 snapshot_id = 's-EJq3B***'
16 resp = client.get_snapshot(snapshot_id)
17 print(resp)
Delete a snapshot
The following code allows you to delete a snapshot using a specified snapshot ID.
This operation can only be completed normally when the snapshot status is CreatedFailed or Available:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# ID of snapshot to be deleted
15 snapshot_id = 's-EJq3B***'
16 resp = client.delete_snapshot(snapshot_id=snapshot_id)
17 print(resp)
Query snapshot chain list
You can query the snapshot chain list with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Disk ID
15 volume_id = 'v-***'
16 resp = client.list_snapshot_chain(volume_id)
17 print(resp)
Bind a tag to a snapshot chain
You can bind a tag to a snapshot chain with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 tag1 = bcc_model.TagModel(tagKey='TestKey02',
15 tagValue='TestValue02')
16 tag2 = bcc_model.TagModel(tagKey='TestKey03',
17 tagValue='TestValue03')
18 tags = []
19 tags.append(tag1)
20 tags.append(tag2)
21 chain_id = "sl-i7TLl***"
22 resp = client.tag_snapshot_chain(chain_id, tags=tags)
23 print(resp)
Unbind a tag from a snapshot chain
You can unbind a tag from a specified snapshot chain with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 tag1 = bcc_model.TagModel(tagKey='TestKey02',
15 tagValue='TestValue02')
16 tag2 = bcc_model.TagModel(tagKey='TestKey03',
17 tagValue='TestValue03')
18 tags = []
19 tags.append(tag1)
20 tags.append(tag2)
21 chain_id = "sl-i7TLl***"
22 resp = client.untag_snapshot_chain(chain_id, tags=tags)
23 print(resp)
Replicate a snapshot across regions
You can replicate a disk snapshot from one region to another with the following code:
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.services.bcc import bcc_model
5from baidubce.auth.bce_credentials import BceCredentials
6from baidubce.bce_client_configuration import BceClientConfiguration
7if __name__ == '__main__':
8# Set your AK, SK, and the region to be accessed
9 HOST = b'http://bcc.bj.baidubce.com'
10 AK = b'ak'
11 SK = b'sk'
12# Set default configuration
13 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
14 endpoint=HOST)
15# Create a BCC client
16 client = bcc_client.BccClient(config)
17
18# Cross-region replication of snapshot demo
19 snapshot_id = "s-GTl*****" # Snapshot ID
20 dest_region_info_list = list() # Target region information list
21 dest_region_info1 = bcc_model.DestRegionInfoModel(destRegion="bd", # Target region name
22 name="snapshot_name_for_bd") # Snapshot name
23 dest_region_info2 = bcc_model.DestRegionInfoModel(destRegion="gz", # Target region name
24 name="snapshot_name_for_gz") # Snapshot name
25 dest_region_info_list.append(dest_region_info1)
26 dest_region_info_list.append(dest_region_info2)
27# Request to replicate a snapshot across regions
28 response = client.create_remote_copy_snapshot(snapshot_id=snapshot_id, # Snapshot ID
29 dest_region_infos=dest_region_info_list) # Target region information list
30 print(response)
