flzta commited on
Commit
036ea11
·
verified ·
1 Parent(s): daa4fc9

Upload 2 files

Browse files
Files changed (2) hide show
  1. cloudreve.ini.ini +15 -0
  2. sync_data.sh +138 -0
cloudreve.ini.ini ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [Database]
2
+ Type = sqlite3
3
+ Host =
4
+ Port =
5
+ User =
6
+ Password =
7
+ Name =
8
+ TablePrefix = cd
9
+
10
+ [System]
11
+ Listen = :5212
12
+ CacheExpiration = 24
13
+ ForceUseHttps = false
14
+ AllowInstall = true
15
+ Theme = default
sync_data.sh ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ echo "sync_data.sh script started at $(date)"
4
+
5
+ # 设置 app 的默认名称 (与 Dockerfile 中一致)
6
+ APP_NAME=${APP_NAME:-"app"}
7
+
8
+ # 检查环境变量
9
+ if [[ -z "$WEBDAV_URL" ]] || [[ -z "$WEBDAV_USERNAME" ]] || [[ -z "$WEBDAV_PASSWORD" ]]; then
10
+ echo "Starting without backup functionality - missing WEBDAV_URL, WEBDAV_USERNAME, or WEBDAV_PASSWORD"
11
+ exec /opt/app/$APP_NAME -c /opt/app/app.ini
12
+ exit 0
13
+ fi
14
+
15
+ # 设置备份路径
16
+ WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}
17
+ FULL_WEBDAV_URL="${WEBDAV_URL}"
18
+ if [ -n "$WEBDAV_BACKUP_PATH" ]; then
19
+ FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
20
+ fi
21
+
22
+ # 激活虚拟环境
23
+ source /opt/venv/bin/activate
24
+
25
+ # 设置 app 数据目录
26
+ APP_DATA_DIR="/opt/app/app_data"
27
+
28
+ # 下载最新备份并恢复
29
+ restore_backup() {
30
+ echo "开始从 WebDAV 下载最新备份..."
31
+ python3 -c "
32
+ import sys
33
+ import os
34
+ import tarfile
35
+ import requests
36
+ import shutil
37
+ from webdav3.client import Client
38
+ options = {
39
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
40
+ 'webdav_login': '$WEBDAV_USERNAME',
41
+ 'webdav_password': '$WEBDAV_PASSWORD'
42
+ }
43
+ client = Client(options)
44
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('app_backup_')]
45
+ if not backups:
46
+ print('没有找到备份文件')
47
+ sys.exit()
48
+ latest_backup = sorted(backups)[-1]
49
+ print(f'最新备份文件:{latest_backup}')
50
+ with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
51
+ if r.status_code == 200:
52
+ with open(f'/tmp/{latest_backup}', 'wb') as f:
53
+ for chunk in r.iter_content(chunk_size=8192):
54
+ f.write(chunk)
55
+ print(f'成功下载备份文件到 /tmp/{latest_backup}')
56
+ if os.path.exists(f'/tmp/{latest_backup}'):
57
+ # 如果目录已存在,先删除它
58
+ if os.path.exists('$APP_DATA_DIR'):
59
+ shutil.rmtree('$APP_DATA_DIR')
60
+ os.makedirs('$APP_DATA_DIR', exist_ok=True)
61
+
62
+ # 解压备份文件
63
+ with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
64
+ tar.extractall('$APP_DATA_DIR')
65
+
66
+ print(f'成功从 {latest_backup} 恢复备份')
67
+ else:
68
+ print(f'下载备份失败:{r.status_code}')
69
+ "
70
+ }
71
+
72
+ # 首次启动时下载最新备份
73
+ echo "Checking for existing app data directory..."
74
+ if [ ! -d "$APP_DATA_DIR" ]; then
75
+ echo "App data directory does not exist. Downloading latest backup from WebDAV..."
76
+ restore_backup
77
+ else:
78
+ echo "App data directory already exists. Skipping initial backup download."
79
+ fi
80
+
81
+ # 同步函数
82
+ sync_data() {
83
+ while true; do
84
+ echo "Starting sync process at $(date)"
85
+
86
+ if [ -d "$APP_DATA_DIR" ]; then
87
+ timestamp=$(date +%Y%m%d_%H%M%S)
88
+ backup_file="app_backup_${timestamp}.tar.gz"
89
+
90
+ # 压缩数据目录
91
+ tar -czf "/tmp/${backup_file}" -C "$APP_DATA_DIR" .
92
+
93
+ # 上传新备份到WebDAV
94
+ curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
95
+ if [ $? -eq 0 ]; then
96
+ echo "Successfully uploaded ${backup_file} to WebDAV"
97
+ else:
98
+ echo "Failed to upload ${backup_file} to WebDAV"
99
+ fi
100
+
101
+ # 清理旧备份文件
102
+ python3 -c "
103
+ import sys
104
+ from webdav3.client import Client
105
+ options = {
106
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
107
+ 'webdav_login': '$WEBDAV_USERNAME',
108
+ 'webdav_password': '$WEBDAV_PASSWORD'
109
+ }
110
+ client = Client(options)
111
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('app_backup_')]
112
+ backups.sort()
113
+ if len(backups) > 5:
114
+ to_delete = len(backups) - 5
115
+ for file in backups[:to_delete]:
116
+ client.clean(file)
117
+ print(f'Successfully deleted {file}.')
118
+ else:
119
+ print('Only {} backups found, no need to clean.'.format(len(backups)))
120
+ " 2>&1
121
+
122
+ rm -f "/tmp/${backup_file}"
123
+ else:
124
+ echo "App data directory does not exist yet, waiting for next sync..."
125
+ fi
126
+
127
+ SYNC_INTERVAL=${SYNC_INTERVAL:-600}
128
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
129
+ sleep $SYNC_INTERVAL
130
+ done
131
+ }
132
+
133
+ # 启动同步进程
134
+ sync_data &
135
+
136
+ # 启动 app
137
+ sleep 30
138
+ exec /opt/app/$APP_NAME -c /opt/app/app.ini