目 录CONTENT

文章目录

Halo云备份代码更新(ver>20.0.0)

前言

最近终于有空从秋招中松一口气,可以有时间检查vps后台,这一检查不得了,发现连续几周定时的备份任务都失败了,还好这几周博客没出什么幺蛾子,想想都后怕。

问题查找

原本以为是Halo官方更换了备份的api,但是更新日志中并没有查到相关的变更,在对代码反复调试后,终于发现是认证方式的问题,再一核查,发现Halo自20.0.0版本后默认禁用了basic验证。找到问题,那就好办了。

获取token

这里采用token验证的方式,首先在Halo后台的个人界面获取token:

勾选备份相关的权限:

然后就会显示token,注意token只显示一次,注意保存。

代码更新

import base64
import time
import requests
import json
from datetime import datetime
from datetime import timedelta
# 网站地址
website = "halo网址"
# halo2备份文件夹路径
backup_halo_path = "/opt/1panel/apps/halo/halo/data/backups"
backup_api = website + "/apis/migration.halo.run/v1alpha1/backups"
check_api = website + "/apis/migration.halo.run/v1alpha1/backups?sort=metadata.creationTimestamp%2Cdesc"
# 获取现在的时间 2023-09-24T13:14:18.650Z
now_time = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
ten_days_later = (datetime.now() + timedelta(minutes=2)).strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'

print(now_time)
payload = json.dumps({
    "apiVersion": "migration.halo.run/v1alpha1",
    "kind": "Backup",
    "metadata": {
        "generateName": "backup-",
        "name": ""
    },
    "spec": {
        "expiresAt": ten_days_later,
    }
})
token = "你的token"
headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json'
}
response = requests.request("POST", backup_api, headers=headers, data=payload)
print(response.text)
if response.status_code == 201:
    print("备份请求成功!")
    new_backup_name = ""
    while True:
        check_response = requests.request("GET", check_api, headers=headers)
        if check_response.status_code == 200:
            backup_data = json.loads(check_response.text)
            items = backup_data.get("items", [])
            if items[0]["status"]["phase"] == "SUCCEEDED":
                print("备份完成!")
                new_backup_name = items[0]["status"]["filename"]
                break
            if items[0]["status"]["phase"] == "RUNNING":
                print("正在备份!")
                time.sleep(10)

        else:
            print(f"查询备份请求失败!错误代码:{check_response.status_code}")

else:
    print(f"备份请求失败!错误代码:{response.status_code}")

接下来只要定时执行代码即可,其余的备份细节相关可以看之前的文章

0

评论区