
StarRocks提供了将快照备份到oss的能力,但没有提供删除的功能。
下面是使用python实现的删除超过7天的备份的快照脚本:
# -*- coding: utf-8 -*-
import mysql.connector
from datetime import datetime, timedelta
import pytz
import oss2
def get_oss_client():
auth = oss2.AuthV4("xxxxxxxxx", "xxxxxxxxxxxxxx") # 改为自己的AK信息
bucket = oss2.Bucket(auth, 'https://oss-cn-shanghai.aliyuncs.com', 'dbatest', region='cn-shanghai') # oss相关参数,需要根据自己实际情况修改
return bucket
def get_snapshots(cursor):
cursor.execute("SHOW SNAPSHOT ON dbatest_aliyun") # SNAPSHOT名称,需要根据自己实际情况修改
return cursor.fetchall()
def parse_timestamp(timestamp):
# 解析时间戳并转换为datetime对象
date_str = timestamp[:16] # 假设时间戳格式为 "YYYY-MM-DD-HH-MM"
return datetime.strptime(date_str, '%Y-%m-%d-%H-%M').replace(tzinfo=pytz.UTC)
def delete_old_snapshots(snapshots, bucket, days_threshold=7):
current_time = datetime.now(pytz.UTC)
for snapshot_info in snapshots:
Snapshot, Timestamp, Status = snapshot_info
print(Snapshot, Timestamp, Status)
given_time = parse_timestamp(Timestamp)
time_difference = current_time - given_time
print(time_difference.days)
if time_difference.days <= days_threshold:
print(f"{Snapshot} 存储超过{days_threshold}天,即将被自动化删除")
aliyun_oss_path = f"__ss_{Snapshot}"
print(f"阿里云OSS路径: {aliyun_oss_path}")
# prefix这里是我自己的oss的路径,需要根据自己实际情况修改
prefix = f"testsr/__starrocks_repository_dbatest_aliyun/{aliyun_oss_path}"
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
try:
bucket.delete_object(obj.key)
except Exception as e:
print(f"删除文件 {obj.key} 时出错: {e}")
def main():
connection = mysql.connector.connect(
host="192.168.31.181", user="root", password="123456", port=9030
)
cursor = connection.cursor()
try:
bucket = get_oss_client()
snapshots = get_snapshots(cursor)
print(snapshots)
delete_old_snapshots(snapshots, bucket)
except Exception as e:
print(f"发生错误: {e}")
finally:
cursor.close()
connection.close()
if __name__ == "__main__":
main()原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。