File size: 1,893 Bytes
c0123b2 478dee6 ae6c2a1 c0123b2 ae6c2a1 478dee6 c0123b2 ae6c2a1 c0123b2 ae6c2a1 c0123b2 ae6c2a1 c0123b2 ae6c2a1 c0123b2 ae6c2a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import os
import tarfile
import time
from huggingface_hub import hf_hub_download, upload_file
DATASET_REPO = "cys003/chromium-config" # ← 确认是这个
FILE_NAME = "config.tar.gz"
CONFIG_DIR = "/config"
TOKEN = os.environ.get("HF_TOKEN")
def restore():
print("=== 开始执行 Chromium 配置恢复 ===")
try:
local_file = hf_hub_download(
repo_id=DATASET_REPO,
filename=FILE_NAME,
repo_type="dataset",
token=TOKEN
)
os.makedirs(CONFIG_DIR, exist_ok=True)
with tarfile.open(local_file, "r:gz") as tar:
tar.extractall(CONFIG_DIR, filter='data')
# 清理锁文件
for lock_file in ["SingletonLock", "SingletonSocket", "SingletonCookie", ".lock"]:
lock_path = os.path.join(CONFIG_DIR, lock_file)
if os.path.exists(lock_path):
os.remove(lock_path)
print(f"已清理锁文件: {lock_file}")
print("=== 配置恢复成功! ===")
except Exception as e:
print("首次启动或无备份,跳过恢复:", str(e))
def backup():
print("=== 开始备份 Chromium 配置 ===")
try:
tar_path = "/tmp/config.tar.gz"
with tarfile.open(tar_path, "w:gz") as tar:
tar.add(CONFIG_DIR, arcname=".")
upload_file(
path_or_fileobj=tar_path,
path_in_repo=FILE_NAME,
repo_id=DATASET_REPO,
repo_type="dataset",
token=TOKEN,
commit_message=f"Auto backup {time.strftime('%Y-%m-%d %H:%M:%S')}"
)
print("=== 备份上传成功! ===")
os.remove(tar_path)
except Exception as e:
print("备份失败:", str(e))
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "backup":
backup()
else:
restore() |