CoolFace
Apppublic

sem6688/openui

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
sync_data.sh131 linesDownload Raw Back to root
1#!/bin/bash2 3# 检查环境变量4if [[ -z "$WEBDAV_URL" ]] || [[ -z "$WEBDAV_USERNAME" ]] || [[ -z "$WEBDAV_PASSWORD" ]]; then5    echo "缺少 WEBDAV_URL、WEBDAV_USERNAME 或 WEBDAV_PASSWORD,启动时将不包含备份功能"6    exit 07fi8 9# 设置备份路径10WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}11FULL_WEBDAV_URL="${WEBDAV_URL}"12if [ -n "$WEBDAV_BACKUP_PATH" ]; then13    FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"14fi15 16# 下载最新备份并恢复17restore_backup() {18    echo "开始从 WebDAV 下载最新备份..."19    python3 -c "20import sys21import os22import tarfile23import requests24from webdav3.client import Client25import shutil26options = {27    'webdav_hostname': '$FULL_WEBDAV_URL',28    'webdav_login': '$WEBDAV_USERNAME',29    'webdav_password': '$WEBDAV_PASSWORD'30}31client = Client(options)32backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('webui_backup_')]33if not backups:34    print('没有找到备份文件')35    sys.exit()36latest_backup = sorted(backups)[-1]37print(f'最新备份文件:{latest_backup}')38with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:39    if r.status_code == 200:40        with open(f'/tmp/{latest_backup}', 'wb') as f:41            for chunk in r.iter_content(chunk_size=8192):42                f.write(chunk)43        print(f'成功下载备份文件到 /tmp/{latest_backup}')44        if os.path.exists(f'/tmp/{latest_backup}'):45            # 解压备份文件到临时目录46            temp_dir = '/tmp/restore'47            os.makedirs(temp_dir, exist_ok=True)48            tar = tarfile.open(f'/tmp/{latest_backup}', 'r:gz')49            tar.extractall(temp_dir)50            tar.close()51            # 查找并移动 webui.db 文件52            for root, dirs, files in os.walk(temp_dir):53                if 'webui.db' in files:54                    db_path = os.path.join(root, 'webui.db')55                    os.makedirs('./data', exist_ok=True)56                    os.replace(db_path, './data/webui.db')57                    print(f'成功从 {latest_backup} 恢复备份')58                    break59            else:60                print('备份文件中未找到 webui.db')61            # 删除临时目录62            try:63                shutil.rmtree(temp_dir)64            except Exception as e:65                print(f'删除临时目录时出错:{e}')66            os.remove(f'/tmp/{latest_backup}')67        else:68            print('下载的备份文件不存在')69    else:70        print(f'下载备份失败:{r.status_code}')71"72}73 74# 首次启动时下载最新备份75echo "正在从 WebDAV 下载最新备份..."76restore_backup77 78# 同步函数79sync_data() {80    while true; do81        echo "在 $(date) 开始同步进程"82 83        if [ -f "./data/webui.db" ]; then84            timestamp=$(date +%Y%m%d_%H%M%S)85            backup_file="webui_backup_${timestamp}.tar.gz"86 87            # 打包数据库文件88            tar -czf "/tmp/${backup_file}" ./data/webui.db89 90            # 上传新备份到WebDAV91            curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"92            if [ $? -eq 0 ]; then93                echo "成功将 ${backup_file} 上传至 WebDAV"94            else95                echo "上传 ${backup_file} 至 WebDAV 失败"96            fi97 98            # 清理旧备份文件99            python3 -c "100import sys101from webdav3.client import Client102options = {103    'webdav_hostname': '$FULL_WEBDAV_URL',104    'webdav_login': '$WEBDAV_USERNAME',105    'webdav_password': '$WEBDAV_PASSWORD'106}107client = Client(options)108backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('webui_backup_')]109backups.sort()110if len(backups) > 5:111    to_delete = len(backups) - 5112    for file in backups[:to_delete]:113        client.clean(file)114        print(f'成功删除 {file}。')115else:116    print('仅找到 {} 个备份,无需清理。'.format(len(backups)))117" 2>&1118 119            rm -f "/tmp/${backup_file}"120        else121            echo "数据库文件尚不存在,等待下次同步..."122        fi123 124        SYNC_INTERVAL=${SYNC_INTERVAL:-600}125        echo "下次同步将在 ${SYNC_INTERVAL} 秒后进行..."126        sleep $SYNC_INTERVAL127    done128}129 130# 后台启动同步进程131sync_data &