cfjy888 commited on
Commit
888bb3e
·
verified ·
1 Parent(s): 822018b

Create sync_data.sh

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