Spaces:
Paused
Paused
File size: 3,570 Bytes
b394a06 7443979 b394a06 7443979 b394a06 7443979 e3a0fc0 7443979 e3a0fc0 7443979 e3a0fc0 b394a06 7443979 b394a06 7443979 e3a0fc0 7443979 e3a0fc0 b394a06 7443979 e3a0fc0 7443979 e3a0fc0 b394a06 7443979 e3a0fc0 7443979 b394a06 7443979 |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#!/bin/bash
set -eo pipefail
# 检查环境变量
required_vars=("WEBDAV_URL" "WEBDAV_USERNAME" "WEBDAV_PASSWORD")
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo "警告: 缺少必要环境变量 $var,将禁用备份功能"
exec ./alist server --data /app/config
exit 0
fi
done
# 设置完整WebDAV路径
FULL_WEBDAV_URL="${WEBDAV_URL}${WEBDAV_BACKUP_PATH:+/$WEBDAV_BACKUP_PATH}"
# 恢复备份函数
restore_backup() {
echo "尝试从WebDAV恢复最新备份..."
python3 <<EOF
import os
import sys
import tarfile
import requests
from webdav3.client import Client
from tenacity import retry, stop_after_attempt, wait_fixed
# 配置WebDAV客户端
options = {
'webdav_hostname': '$FULL_WEBDAV_URL',
'webdav_login': '$WEBDAV_USERNAME',
'webdav_password': '$WEBDAV_PASSWORD'
}
@retry(stop=stop_after_attempt(3), wait=wait_fixed(5))
def download_latest_backup():
client = Client(options)
try:
backups = [f for f in client.list() if f.endswith('.tar.gz') and f.startswith('alist_backup_')]
if not backups:
print("未找到备份文件")
return False
latest = sorted(backups)[-1]
print(f"找到最新备份: {latest}")
# 下载备份
temp_file = f"/tmp/{latest}"
client.download_sync(remote_path=latest, local_path=temp_file)
# 解压备份
if os.path.exists("$DATA_DIR"):
import shutil
shutil.rmtree("$DATA_DIR")
os.makedirs("$DATA_DIR", exist_ok=True)
with tarfile.open(temp_file, "r:gz") as tar:
tar.extractall("$DATA_DIR")
os.remove(temp_file)
print("备份恢复成功")
return True
except Exception as e:
print(f"备份恢复失败: {str(e)}")
raise
download_latest_backup()
EOF
}
# 首次启动恢复备份
restore_backup || echo "使用空数据目录启动"
# 启动AList和Aria2
./alist server --data /app/config &
aria2c --enable-rpc --rpc-listen-all --rpc-allow-origin-all --rpc-listen-port=6800 --daemon
# 同步函数
sync_data() {
while true; do
echo "$(date) 开始同步数据..."
# 创建备份
backup_file="/tmp/alist_backup_$(date +%Y%m%d_%H%M%S).tar.gz"
tar -czf "$backup_file" -C "$DATA_DIR" .
# 上传备份
python3 <<EOF
import os
from webdav3.client import Client
from tenacity import retry, stop_after_attempt, wait_fixed
options = {
'webdav_hostname': '$FULL_WEBDAV_URL',
'webdav_login': '$WEBDAV_USERNAME',
'webdav_password': '$WEBDAV_PASSWORD'
}
@retry(stop=stop_after_attempt(3), wait=wait_fixed(10))
def upload_and_clean():
client = Client(options)
try:
# 上传新备份
client.upload_sync(local_path='$backup_file', remote_path='${backup_file##*/}')
print("备份上传成功")
# 清理旧备份(保留最近5个)
backups = [f for f in client.list() if f.endswith('.tar.gz') and f.startswith('alist_backup_')]
if len(backups) > 5:
for old_backup in sorted(backups)[:-5]:
client.clean(old_backup)
print(f"已删除旧备份: {old_backup}")
except Exception as e:
print(f"备份操作失败: {str(e)}")
raise
upload_and_clean()
EOF
rm -f "$backup_file"
echo "下次同步将在 $SYNC_INTERVAL 秒后..."
sleep "$SYNC_INTERVAL"
done
}
# 启动同步进程
sync_data |