flzta commited on
Commit
376fe4b
·
verified ·
1 Parent(s): 9de5869

Create sync_data.sh

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