首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >轻量云4次快照备份,求大神修改?

轻量云4次快照备份,求大神修改?

提问于 2023-09-08 22:05:10
回答 0关注 0查看 25

求大神修改快照函数 让他能备份4次,且每次触发只删除最早那个

目前问题,能备份4次但是每次删除的是第二个

import json

import os

import time

from tencentcloud.common import credential

from tencentcloud.common.profile.client_profile import ClientProfile

from tencentcloud.common.profile.http_profile import HttpProfile

from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException

from tencentcloud.lighthouse.v20200324 import lighthouse_client, models

def main(SecretId, SecretKey, region, InstanceIds, Instanceidx=1):

"""

GOGO

:param SecretId: str 腾讯云账号SecretId

:param SecretKey: str 腾讯云账号SecretKey

:param region: str 实例地域

:param InstanceIds: str 实例ID

param Instanceidx: int 0:删除最新的保留最早的备份,这样可以有一个固定备份,1:删除最早

"""

get_rest = get_info(SecretId, SecretKey, region, InstanceIds)

if get_rest != False:

TotalCount = get_rest['TotalCount']

# 快照数

if TotalCount < 3:

# 直接备份

CreateInstanceSnapshot(SecretId, SecretKey, region, InstanceIds)

elif TotalCount == 3:

# 删除之前较早一个备份,就是列表里的第二个,状态需要正常才能删除

SnapshotState = (get_rest['SnapshotSet'][1]['SnapshotState'])

if SnapshotState == 'NORMAL':

SnapshotId = (get_rest['SnapshotSet'][1]['SnapshotId'])

DeleteSnapshots_re = DeleteSnapshots(SecretId, SecretKey, SnapshotId, region)

if DeleteSnapshots_re != False:

# 删除之前一个后,再进行备份

print('已经删除完成快照ID为{0}的快照,现在准备开始备份实例'.format(SnapshotId))

CreateInstanceSnapshot(SecretId, SecretKey, region, InstanceIds)

else:

print('当前快照数量存在问题,请登录腾讯云后台检查并删除多余的快照后操作')

time.sleep(5)

exit()

def CreateInstanceSnapshot(SecretId, SecretKey, region, InstanceIds):

"""

创建快照

"""

try:

cred = credential.Credential(SecretId, SecretKey)

httpProfile = HttpProfile()

httpProfile.endpoint = "lighthouse.tencentcloudapi.com"

clientProfile = ClientProfile()

clientProfile.httpProfile = httpProfile

client = lighthouse_client.LighthouseClient(cred, region, clientProfile)

req = models.CreateInstanceSnapshotRequest()

params = {

"InstanceId": InstanceIds

}

req.from_json_string(json.dumps(params))

resp = client.CreateInstanceSnapshot(req)

resp_re = json.loads(resp.to_json_string())

SnapshotId = resp_re['SnapshotId']

print('轻量云快照备份完成,快照ID为:{0},程序在5秒钟后关闭'.format(SnapshotId))

time.sleep(5)

#exit()

except TencentCloudSDKException as err:

print(err)

return False

def DeleteSnapshots(SecretId, SecretKey, SnapshotId, region):

"""

删除快照

"""

try:

cred = credential.Credential(SecretId, SecretKey)

httpProfile = HttpProfile()

httpProfile.endpoint = "lighthouse.tencentcloudapi.com"

clientProfile = ClientProfile()

clientProfile.httpProfile = httpProfile

client = lighthouse_client.LighthouseClient(cred, region, clientProfile)

req = models.DeleteSnapshotsRequest()

params = {

"SnapshotIds": [SnapshotId]

}

req.from_json_string(json.dumps(params))

resp = client.DeleteSnapshots(req)

return json.loads(resp.to_json_string())

except TencentCloudSDKException as err:

print(err)

return False

def get_info(SecretId, SecretKey, region, InstanceIds):

"""

获取快照信息

:param SecretId: str 腾讯云账号SecretId

:param SecretKey: str 腾讯云账号SecretKey

:param region: str 实例地域

:param InstanceIds: str 实例ID

:return: json 腾讯云实例流量情况

"""

try:

cred = credential.Credential(SecretId, SecretKey)

httpProfile = HttpProfile()

httpProfile.endpoint = "lighthouse.tencentcloudapi.com"

clientProfile = ClientProfile()

clientProfile.httpProfile = httpProfile

client = lighthouse_client.LighthouseClient(cred, region, clientProfile)

req = models.DescribeSnapshotsRequest()

params = {

"Filters": [

{

"Name": "instance-id",

"Values": [ InstanceIds ]

}

]

}

req.from_json_string(json.dumps(params))

resp = client.DescribeSnapshots(req)

return json.loads((resp.to_json_string()))

except TencentCloudSDKException as err:

print(err)

return False

def main_handler(event, context):

"""

腾讯云API库安装

pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python

腾讯云账号ID获取地址

https://console.cloud.tencent.com/cam/capi

实例地域

"ap-beijing", "ap-chengdu", "ap-guangzhou", "ap-hongkong", "ap-nanjing", "ap-shanghai", "ap-singapore", "ap-tokyo", "eu-moscow", "na-siliconvalley"

"""

# SecretId

SecretId = os.environ.get('SecretId')

# SecretKey

SecretKey = os.environ.get('SecretKey')

# 【格式】实例地域1:轻量云实例ID1,轻量云实例ID2;实例地域2:轻量云实例ID3,轻量云实例ID4

Regions_InstanceIds = os.environ.get('Regions_InstanceIds')

# 0: 删除最新的保留最早的备份,这样可以有一个固定备份,1: 删除最早,默认 1

Instanceidx = os.environ.get('Instanceidx')

# 执行

nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

print('---------' + str(nowtime) + ' 程序开始执行------------')

for r in Regions_InstanceIds.split(";"):

Region = r.split(":")[0]

InstanceIds = r.split(":")[1]

for id in InstanceIds.split(","):

print('Region: '+Region+'InstanceId: '+id+'\n')

main(SecretId, SecretKey, Region, id, int(Instanceidx))

return True

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档