继续照葫芦画瓢,良心云果然还是良心的,之前的配置居然还免费升级了,价格不变,非常不错值得表扬。
个人还是比较推荐良心云轻量云做站使用的,腾讯云轻量云的购买地址如下:
https://curl.qcloud.com/cVUea4hX
但在轻量云的使用过程中发现,快照功能不支持自动进行备份,需要手动进行备份,作为懒人的话非常不方便,而且备份嘛一般习惯在凌晨夜深人静的时候悄悄操作,所以,就有了这个脚本。
需要注意的是,轻量云只支持免费保存两个快照镜像,大于2个估计是要收费的,所以轻量云的快照不要超过两个。
那么我这个脚本也会删除较早的一个,保存一个并生成一个快照这样迭代。
首先还是安装腾讯云API的库
pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python
之后保存代码另存为一个.py文件,另外注意修改最下面你的相关腾讯云参数为自己的,因为是通过API进行操作,所以这个脚本不需要放在你的轻量云上面,脚本可以放到任意地方进行操作,但需要注意不要让你的腾讯云API信息泄露。
2022年9月5日 修复问题
感谢网友使用发现,修复判断快照数量错误导致程序错误的问题。
或自行下载
https://github.com/fungjcode/python_tools
#!/usr/bin/python3
# -*-coding:UTF-8-*-
# 风之翼灵
# www.fungj.com
"""
腾讯云轻量云自动进行快照备份
轻量云免费提供2个快照,所以该脚本只备份两个快照
"""
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):
"""
GOGO
:param SecretId: str 腾讯云账号SecretId
:param SecretKey: str 腾讯云账号SecretKey
:param region: str 实例地域
:param InstanceIds: str 实例ID
"""
get_rest = get_info(SecretId, SecretKey, region, InstanceIds)
if get_rest != False:
TotalCount = get_rest['TotalCount']
# 快照数
if TotalCount == 0:
# 直接备份
CreateInstanceSnapshot(SecretId, SecretKey, region, InstanceIds)
elif TotalCount == 1:
# 直接备份
CreateInstanceSnapshot(SecretId, SecretKey, region,InstanceIds)
elif TotalCount == 2:
# 删除之前较早一个备份,就是列表里的第二个,状态需要正常才能删除
SnapshotState = (get_rest['SnapshotSet'][0]['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 = {
}
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
if __name__ == '__main__':
"""
腾讯云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 = "你的SecretId"
# SecretKey
SecretKey = "你的SecretKey"
# 实例地域
region = "你的实例地域"
# 轻量云实例ID
InstanceIds = "你的轻量云实例ID"
# 执行
nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print('---------' + str(nowtime) + ' 程序开始执行------------')
main(SecretId, SecretKey, region, InstanceIds)
10 条评论
竟然能看懂OωO,还是感谢博主提供的方法,可以每天自动备份咯,谢谢
哈哈,搞定就好...数据无价哇
感谢博主,已解决,是因为地域那里我写的是‘上海’,应该是ap-shanghai,但是我备份成功后再次备份又报错了...line 47, in main
IndexError: list index out of range
pycharm 上面确实是UTF-8呢
不知道哪里出错了
目测是你的编辑环境有问题,编码错误,需要UTF-8,你这个报错是latin-1了
[TencentCloudSDKException] code:ClientNetworkError message:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256) requestId:None
如果能在github上自动运行就好了
能支持删除第一个或者自定义吗,因为最早的备份又是需要一直留着
另外能支持云函数吗
现在就是删除最早的第一个,呃,自定义小改一下就可以,给个快照的ID就可以。云函数的话我没测,如果支持python的话理论应该是支持的