快照
更新时间:2024-07-02
创建快照
使用以下代码可以从指定的磁盘创建快照,支持创建系统盘快照和CDS磁盘快照。
创建系统盘快照时,实例的状态必须为 Running 或 Stopped 此操作才能正常完成。
创建CDS磁盘快照时,磁盘的状态需要为InUse或 Available 此操作才能正常完成。
可创建快照数量的配额取决于账号下拥有的磁盘总数,当前最多为磁盘数量乘以8。
import uuid
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
client_token = str(uuid.uuid4())
# 设置快照名称
snapshot_name = 'snapshot_' + client_token
# 创建快照的磁盘ID
volume_id = 'v-***'
resp = client.create_snapshot(volume_id, snapshot_name, client_token=client_token)
print(resp)
查询快照列表
使用以下代码以查询所有的快照信息,包括系统盘快照和数据盘快照信息:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 创建快照的磁盘ID
volume_id = 'v-***'
# 批量获取列表的查询的起始位置
marker = "your-marker"
# 每页包含的最大数量
max_keys = 100
resp = client.list_snapshots(marker=marker, max_keys=max_keys, volume_id=volume_id)
print(resp)
查询快照详情
使用以下代码可以通过指定的快照ID查询单个快照的详细信息:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 快照ID
snapshot_id = 's-EJq3B***'
resp = client.get_snapshot(snapshot_id)
print(resp)
删除快照
使用以下代码可以通过指定快照ID删除快照。
只有当快照的状态为 CreatedFailed 或 Available时,操作才能正常完成:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 待删除快照ID
snapshot_id = 's-EJq3B***'
resp = client.delete_snapshot(snapshot_id=snapshot_id)
print(resp)
查询快照链列表
使用以下代码可以查询快照链列表:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 磁盘ID
volume_id = 'v-***'
resp = client.list_snapshot_chain(volume_id)
print(resp)
快照链绑定标签
使用以下代码可以给指定快照链绑定标签:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client, bcc_model
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
tag1 = bcc_model.TagModel(tagKey='TestKey02',
tagValue='TestValue02')
tag2 = bcc_model.TagModel(tagKey='TestKey03',
tagValue='TestValue03')
tags = []
tags.append(tag1)
tags.append(tag2)
chain_id = "sl-i7TLl***"
resp = client.tag_snapshot_chain(chain_id, tags=tags)
print(resp)
快照链解绑标签
使用以下代码可以给指定快照链解绑标签:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client, bcc_model
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
tag1 = bcc_model.TagModel(tagKey='TestKey02',
tagValue='TestValue02')
tag2 = bcc_model.TagModel(tagKey='TestKey03',
tagValue='TestValue03')
tags = []
tags.append(tag1)
tags.append(tag2)
chain_id = "sl-i7TLl***"
resp = client.untag_snapshot_chain(chain_id, tags=tags)
print(resp)
跨地域复制快照
使用以下代码可以将一份磁盘快照从一个地域复制到另一个地域:
# !/usr/bin/env python
# coding=utf-8
from baidubce.services.bcc import bcc_client
from baidubce.services.bcc import bcc_model
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 跨区域复制快照demo
snapshot_id = "s-GTl*****" # 快照ID
dest_region_info_list = list() # 目标区域信息列表
dest_region_info1 = bcc_model.DestRegionInfoModel(destRegion="bd", # 目标区域名称
name="snapshot_name_for_bd") # 快照名称
dest_region_info2 = bcc_model.DestRegionInfoModel(destRegion="gz", # 目标区域名称
name="snapshot_name_for_gz") # 快照名称
dest_region_info_list.append(dest_region_info1)
dest_region_info_list.append(dest_region_info2)
# 请求跨区域复制快照
response = client.create_remote_copy_snapshot(snapshot_id=snapshot_id, # 快照ID
dest_region_infos=dest_region_info_list) # 目标区域信息列表
print(response)