mc6 commited on
Commit
262fbe7
·
verified ·
1 Parent(s): 9c6e9c9

Create sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +125 -0
sync_data.sh ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
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 node server/server.js
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
+ # 下载最新备份并恢复
18
+ restore_backup() {
19
+ echo "开始从 WebDAV 下载最新备份..."
20
+ python3 -c "
21
+ import sys
22
+ import os
23
+ import tarfile
24
+ import requests
25
+ import shutil
26
+ from webdav3.client import Client
27
+ options = {
28
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
29
+ 'webdav_login': '$WEBDAV_USERNAME',
30
+ 'webdav_password': '$WEBDAV_PASSWORD'
31
+ }
32
+ client = Client(options)
33
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('uptime_kuma_backup_')]
34
+ if not backups:
35
+ print('没有找到备份文件')
36
+ sys.exit()
37
+ latest_backup = sorted(backups)[-1]
38
+ print(f'最新备份文件:{latest_backup}')
39
+ with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
40
+ if r.status_code == 200:
41
+ with open(f'/tmp/{latest_backup}', 'wb') as f:
42
+ for chunk in r.iter_content(chunk_size=8192):
43
+ f.write(chunk)
44
+ print(f'成功下载备份文件到 /tmp/{latest_backup}')
45
+ if os.path.exists(f'/tmp/{latest_backup}'):
46
+ # 如果data目录已存在,先删除它
47
+ if os.path.exists('/home/app/uptime-kuma/data'):
48
+ print('删除现有的data目录')
49
+ shutil.rmtree('/home/app/uptime-kuma/data')
50
+
51
+ # 解压备份文件
52
+ with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
53
+ tar.extractall('/home/app/uptime-kuma/')
54
+
55
+ print(f'成功从 {latest_backup} 恢复备份')
56
+ else:
57
+ print('下载的备份文件不存在')
58
+ else:
59
+ print(f'下载备份失败:{r.status_code}')
60
+ "
61
+ }
62
+
63
+ # 首次启动时下载最新备份
64
+ echo "Downloading latest backup from WebDAV..."
65
+ restore_backup
66
+
67
+ # 同步函数
68
+ sync_data() {
69
+ while true; do
70
+ echo "Starting sync process at $(date)"
71
+
72
+ if [ -d "/home/app/uptime-kuma/data" ]; then
73
+ timestamp=$(date +%Y%m%d_%H%M%S)
74
+ backup_file="uptime_kuma_backup_${timestamp}.tar.gz"
75
+
76
+ # 备份整个data目录
77
+ cd /home/app/uptime-kuma
78
+ tar -czf "/tmp/${backup_file}" data
79
+
80
+ # 上传新备份到WebDAV
81
+ curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
82
+ if [ $? -eq 0 ]; then
83
+ echo "Successfully uploaded ${backup_file} to WebDAV"
84
+ else
85
+ echo "Failed to upload ${backup_file} to WebDAV"
86
+ fi
87
+
88
+ # 清理旧备份文件
89
+ python3 -c "
90
+ import sys
91
+ from webdav3.client import Client
92
+ options = {
93
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
94
+ 'webdav_login': '$WEBDAV_USERNAME',
95
+ 'webdav_password': '$WEBDAV_PASSWORD'
96
+ }
97
+ client = Client(options)
98
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('uptime_kuma_backup_')]
99
+ backups.sort()
100
+ if len(backups) > 5:
101
+ to_delete = len(backups) - 5
102
+ for file in backups[:to_delete]:
103
+ client.clean(file)
104
+ print(f'Successfully deleted {file}.')
105
+ else:
106
+ print('Only {} backups found, no need to clean.'.format(len(backups)))
107
+ " 2>&1
108
+
109
+ rm -f "/tmp/${backup_file}"
110
+ else
111
+ echo "/home/app/uptime-kuma/data directory does not exist, waiting for next sync..."
112
+ fi
113
+
114
+ SYNC_INTERVAL=${SYNC_INTERVAL:-600}
115
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
116
+ sleep $SYNC_INTERVAL
117
+ done
118
+ }
119
+
120
+ # 启动同步进程
121
+ sync_data &
122
+
123
+ # 启动主应用
124
+ sleep 30
125
+ exec node server/server.js