cfiles commited on
Commit
a4357b3
·
verified ·
1 Parent(s): d8c6747

Create sync_data.sh

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