CoolFace
Apppublic

pppkkk/kms

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
sync_data.sh125 linesDownload Raw Back to root
1#!/bin/sh2 3# 检查环境变量4if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then5    echo "Starting without backup functionality - missing WEBDAV_URL, WEBDAV_USERNAME, or WEBDAV_PASSWORD"6    exec node server/server.js7    exit 08fi9 10# 设置备份路径11WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}12FULL_WEBDAV_URL="${WEBDAV_URL}"13if [ -n "$WEBDAV_BACKUP_PATH" ]; then14    FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"15fi16 17# 下载最新备份并恢复18restore_backup() {19    echo "开始从 WebDAV 下载最新备份..."20    python3 -c "21import sys22import os23import tarfile24import requests25import shutil26from webdav3.client import Client27options = {28    'webdav_hostname': '$FULL_WEBDAV_URL',29    'webdav_login': '$WEBDAV_USERNAME',30    'webdav_password': '$WEBDAV_PASSWORD'31}32client = Client(options)33backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('uptime_kuma_backup_')]34if not backups:35    print('没有找到备份文件')36    sys.exit()37latest_backup = sorted(backups)[-1]38print(f'最新备份文件:{latest_backup}')39with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:40    if r.status_code == 200:41        with open(f'/tmp/{latest_backup}', 'wb') as f:42            for chunk in r.iter_content(chunk_size=8192):43                f.write(chunk)44        print(f'成功下载备份文件到 /tmp/{latest_backup}')45        if os.path.exists(f'/tmp/{latest_backup}'):46            # 如果data目录已存在,先删除它47            if os.path.exists('/home/app/uptime-kuma/data'):48                print('删除现有的data目录')49                shutil.rmtree('/home/app/uptime-kuma/data')50            51            # 解压备份文件52            with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:53                tar.extractall('/home/app/uptime-kuma/')54            55            print(f'成功从 {latest_backup} 恢复备份')56        else:57            print('下载的备份文件不存在')58    else:59        print(f'下载备份失败:{r.status_code}')60"61}62 63# 首次启动时下载最新备份64echo "Downloading latest backup from WebDAV..."65restore_backup66 67# 同步函数68sync_data() {69    while true; do70        echo "Starting sync process at $(date)"71 72        if [ -d "/home/app/uptime-kuma/data" ]; then73            timestamp=$(date +%Y%m%d_%H%M%S)74            backup_file="uptime_kuma_backup_${timestamp}.tar.gz"75 76            # 备份整个data目录77            cd /home/app/uptime-kuma78            tar -czf "/tmp/${backup_file}" data79 80            # 上传新备份到WebDAV81            curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"82            if [ $? -eq 0 ]; then83                echo "Successfully uploaded ${backup_file} to WebDAV"84            else85                echo "Failed to upload ${backup_file} to WebDAV"86            fi87 88            # 清理旧备份文件89            python3 -c "90import sys91from webdav3.client import Client92options = {93    'webdav_hostname': '$FULL_WEBDAV_URL',94    'webdav_login': '$WEBDAV_USERNAME',95    'webdav_password': '$WEBDAV_PASSWORD'96}97client = Client(options)98backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('uptime_kuma_backup_')]99backups.sort()100if len(backups) > 5:101    to_delete = len(backups) - 5102    for file in backups[:to_delete]:103        client.clean(file)104        print(f'Successfully deleted {file}.')105else:106    print('Only {} backups found, no need to clean.'.format(len(backups)))107" 2>&1108 109            rm -f "/tmp/${backup_file}"110        else111            echo "/home/app/uptime-kuma/data directory does not exist, waiting for next sync..."112        fi113 114        SYNC_INTERVAL=${SYNC_INTERVAL:-600}115        echo "Next sync in ${SYNC_INTERVAL} seconds..."116        sleep $SYNC_INTERVAL117    done118}119 120# 启动同步进程121sync_data &122 123# 启动主应用124sleep 30125exec node server/server.js