Python:腾讯云-轻量应用服务器-实现自动快照 – WhiteNight's Site
先说一下配置情况:轻量应用服务器+一块系统盘。我没钱加盘,所以不知道多块盘的情况下这个脚本还能不能用。
官方文档给的代码已经很齐全了,只需要做点补充就能直接用。
至于为什么要写这么个脚本?主要是轻量应用服务器的快照配额就两个,每次备份快照都是-删快照-新建快照这么个流程。而且如果你和我一样每周甚至每天都要备份个快照,那每次备份快照都要登陆,然后点这点那的,还是蛮麻烦的。所以不如写个自动化脚本,一条命令搞定快照。
密钥
小心使用
API访问密钥链接:https://console.cloud.tencent.com/cam/capi
考虑到密钥实际上相当于“登陆”,泄漏密钥和把账号拱手送人没多大区别,所以这东西还是小心保管比较好。
获取快照列表
传id和key
这块官方文档都给全了,只需要做些小修改就行。
考虑到我们只需要获取快照列表,所以我删去了一些用不上的代码。又考虑到直接在代码中保存id和key不太安全,所以我选择通过外部传参数的方式去给出id和key。
同时或者client的时候需要指定Region,Region参数的说明如下。
用logging纯属个人兴趣了,直接用print也行。
Python
def parse():
parser = argparse.ArgumentParser('TencentCloud_Lighthouse_AutoSnapshot',description='腾讯云lighthouse自动快照脚本')
parser.add_argument('--id',help='密钥ID')
parser.add_argument('--key',help='密钥KEY')
parser.add_argument('--server_id',help='服务器的实例ID')
parser.add_argument('--mode',default='info',help='日志级别,默认info,可选debug')
parser.add_argument('--snapshot_mode',default='None',help='快照模式,默认None,可选delete,create')
return parser.parse_args()
def get_snapshot_list(id,key):
try:
cred = credential.Credential(id, key)
client = lighthouse_client.LighthouseClient(cred, "ap-guangzhou")
req = models.DescribeSnapshotsRequest()
params = {}
req.from_json_string(json.dumps(params))
resp = client.DescribeSnapshots(req)
logging.debug(resp.to_json_string())
logging.info('获取快照列表成功')
return resp,client
except TencentCloudSDKException as err:
logging.error('获取快照列表失败')
logging.error(err)
return None,None
删除快照
根据快照id删除
轻量应用服务器-删除快照:https://cloud.tencent.com/document/product/1207/54389
官方文档也写了,删快照需要传入被删除快照的id,这也是为什么要先去获取快照列表。
一般来说,删快照删的是最老的那个,所以还要给快照的创建时间排个序,再筛选出最老的那个快照。其他的和获取快照列表的代码都差不多。
Python
def choose_snapshot(snapshot_list):
# 选择最老的快照
SnapshotSet=snapshot_list.SnapshotSet
SnapshotSet.sort(key=lambda x:x.CreatedTime)
logging.info('选择最老的快照:{}'.format(SnapshotSet[0].SnapshotId)+' 创建时间:{}'.format(SnapshotSet[0].CreatedTime))
return SnapshotSet[0].SnapshotId
def delete_snapshot(client:lighthouse_client.LighthouseClient,snapshot_id:str):
try:
logging.info('删除快照:{}'.format(snapshot_id))
req=models.DeleteSnapshotsRequest()
params={
'SnapshotIds':[snapshot_id],
}
req.from_json_string(json.dumps(params))
resp=client.DeleteSnapshots(req)
logging.debug(resp.to_json_string())
except TencentCloudSDKException as err:
logging.error('删除快照失败')
logging.error(err)
新建快照
需要服务器实例ID
和删快照不一样,新建快照需要指定在xxx实例(也就是xxx服务器)上新建快照,所以我们还需要服务器的实例id。服务器的实例id在服务器的概要那里就能直接复制。
很可惜,快照的id无法自定义,只能自定义快照的名称。所以代码如下
Python
def create_snapshot(client:lighthouse_client.LighthouseClient,snapshot_list,server_id:str):
try:
logging.info('新建快照')
SnapshotName="Snapshot-"+str(time.strftime("%Y%m%d%H%M%S", time.localtime()))
while SnapshotName in [i.SnapshotName for i in snapshot_list.SnapshotSet]:
SnapshotName="Snapshot-"+str(time.strftime("%Y%m%d%H%M%S", time.localtime()))
logging.info('新建快照:'+' 名称:{}'.format(SnapshotName))
req=models.CreateInstanceSnapshotRequest()
params={
"InstanceId":server_id,
"SnapshotName":SnapshotName
}
req.from_json_string(json.dumps(params))
resp=client.CreateInstanceSnapshot(req)
logging.debug(resp.to_json_string())
logging.info('新建快照成功')
except TencentCloudSDKException as err:
logging.error('新建快照失败')
logging.error(err)
完整代码
使用说明
默认是“删一增一”,如果没快照就只作“新建快照”的操作。当然为了方便使用,也能指定只作“新建”或只作“删除”快照的操作。
Python
import json
import argparse
import logging
import random
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 parse():
parser = argparse.ArgumentParser('TencentCloud_Lighthouse_AutoSnapshot',description='腾讯云lighthouse自动快照脚本')
parser.add_argument('--id',help='密钥ID')
parser.add_argument('--key',help='密钥KEY')
parser.add_argument('--server_id',help='服务器的实例ID')
parser.add_argument('--mode',default='info',help='日志级别,默认info,可选debug')
parser.add_argument('--snapshot_mode',default='None',help='快照模式,默认None,可选delete,create')
return parser.parse_args()
def get_snapshot_list(id,key):
try:
cred = credential.Credential(id, key)
client = lighthouse_client.LighthouseClient(cred, "ap-guangzhou")
req = models.DescribeSnapshotsRequest()
params = {}
req.from_json_string(json.dumps(params))
resp = client.DescribeSnapshots(req)
logging.debug(resp.to_json_string())
logging.info('获取快照列表成功')
return resp,client
except TencentCloudSDKException as err:
logging.error('获取快照列表失败')
logging.error(err)
return None,None
def choose_snapshot(snapshot_list):
# 选择最老的快照
SnapshotSet=snapshot_list.SnapshotSet
SnapshotSet.sort(key=lambda x:x.CreatedTime)
logging.info('选择最老的快照:{}'.format(SnapshotSet[0].SnapshotId)+' 创建时间:{}'.format(SnapshotSet[0].CreatedTime))
return SnapshotSet[0].SnapshotId
def delete_snapshot(client:lighthouse_client.LighthouseClient,snapshot_id:str):
try:
logging.info('删除快照:{}'.format(snapshot_id))
req=models.DeleteSnapshotsRequest()
params={
'SnapshotIds':[snapshot_id],
}
req.from_json_string(json.dumps(params))
resp=client.DeleteSnapshots(req)
logging.debug(resp.to_json_string())
except TencentCloudSDKException as err:
logging.error('删除快照失败')
logging.error(err)
def create_snapshot(client:lighthouse_client.LighthouseClient,snapshot_list,server_id:str):
try:
logging.info('新建快照')
SnapshotName="Snapshot-"+str(time.strftime("%Y%m%d%H%M%S", time.localtime()))
while SnapshotName in [i.SnapshotName for i in snapshot_list.SnapshotSet]:
SnapshotName="Snapshot-"+str(time.strftime("%Y%m%d%H%M%S", time.localtime()))
logging.info('新建快照:'+' 名称:{}'.format(SnapshotName))
req=models.CreateInstanceSnapshotRequest()
params={
"InstanceId":server_id,
"SnapshotName":SnapshotName
}
req.from_json_string(json.dumps(params))
resp=client.CreateInstanceSnapshot(req)
logging.debug(resp.to_json_string())
logging.info('新建快照成功')
except TencentCloudSDKException as err:
logging.error('新建快照失败')
logging.error(err)
def main():
args = parse()
if args.mode == 'debug':
logging_level = logging.DEBUG
else:
logging_level = logging.INFO
logging.basicConfig(level=logging_level,format='%(levelname)s:%(asctime)s %(message)s',datefmt='%Y/%m/%d %H:%M:%S')
if args.snapshot_mode=='delete':
logging.info('删除模式:仅作删除快照操作')
snapshot_list,client=get_snapshot_list(args.id,args.key)
if snapshot_list is None:
return
elif snapshot_list.TotalCount==0:
logging.info('无快照,无法删除')
return
else:
SnapshotId=choose_snapshot(snapshot_list)
delete_snapshot(client,SnapshotId)
return
elif args.snapshot_mode=='create':
logging.info('新建模式:仅作新建快照操作')
snapshot_list,client=get_snapshot_list(args.id,args.key)
if snapshot_list is None:
return
else:
create_snapshot(client,snapshot_list,args.server_id)
return
snapshot_list,client=get_snapshot_list(args.id,args.key)
if snapshot_list is None:
return
elif snapshot_list.TotalCount==0:
logging.info('无快照,新建快照')
create_snapshot(client,snapshot_list,args.server_id)
else:
logging.info('列表中快照数量:{}'.format(snapshot_list.TotalCount))
SnapshotId=choose_snapshot(snapshot_list)
delete_snapshot(client,SnapshotId)
snapshot_list,client=get_snapshot_list(args.id,args.key)
create_snapshot(client,snapshot_list,args.server_id)
if __name__ == '__main__':
main()