zzl668 commited on
Commit
b7ee5b8
·
verified ·
1 Parent(s): 08012a9

Update start.sh

Browse files
Files changed (1) hide show
  1. start.sh +20 -7
start.sh CHANGED
@@ -36,7 +36,7 @@ else
36
  fi
37
 
38
  # ==============================================
39
- # 2. WebDAV 自动备份模块 (修复了持久化目录覆盖解压)
40
  # ==============================================
41
  if [[ -n "$WEBDAV_URL" && -n "$WEBDAV_USERNAME" && -n "$WEBDAV_PASSWORD" ]]; then
42
  echo "✅ WebDAV backup enabled"
@@ -45,6 +45,7 @@ if [[ -n "$WEBDAV_URL" && -n "$WEBDAV_USERNAME" && -n "$WEBDAV_PASSWORD" ]]; the
45
  FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
46
  BACKUP_PREFIX="hermes_snapshot_"
47
  SYNC_INTERVAL=${SYNC_INTERVAL:-1800}
 
48
 
49
  restore_backup() {
50
  "$PYTHON_BIN" -c "
@@ -64,8 +65,6 @@ try:
64
  if r.status_code == 200:
65
  with open(local_tmp, 'wb') as f:
66
  for chunk in r.iter_content(8192): f.write(chunk)
67
-
68
- # 修复:直接覆盖解压,不要尝试删除被系统保护的挂载目录
69
  with tarfile.open(local_tmp, 'r:gz') as tar: tar.extractall('/opt/data')
70
  print('[Backup] ✅ State restored successfully')
71
  os.remove(local_tmp)
@@ -76,7 +75,7 @@ except Exception as e:
76
 
77
  backup_daemon() {
78
  sleep 15
79
- echo "[Backup] Daemon started, interval: ${SYNC_INTERVAL}s"
80
  while true; do
81
  sleep $SYNC_INTERVAL
82
  TS=$(date +%Y%m%d_%H%M%S)
@@ -86,6 +85,23 @@ except Exception as e:
86
  curl -f -s -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "$TMP_FILE" "$FULL_WEBDAV_URL/$FNAME"
87
  if [ $? -eq 0 ]; then
88
  echo "[Backup] ✅ Snapshot created: $FNAME"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  fi
90
  rm -f "$TMP_FILE"
91
  done
@@ -128,7 +144,6 @@ import socket, threading
128
 
129
  def handle(client):
130
  try:
131
- # 读取第一包数据来判断流量类型
132
  first_chunk = client.recv(8192)
133
  if not first_chunk:
134
  client.close()
@@ -139,11 +154,9 @@ def handle(client):
139
  except:
140
  req_line = ''
141
 
142
- # 如果是 OpenAI 标准 API 调用,转给 8642 (Gateway)
143
  if '/v1/' in req_line or '/chat/' in req_line or '/models' in req_line:
144
  target_port = 8642
145
  else:
146
- # 否则 (普通浏览器访问),转给 9119 (Dashboard)
147
  target_port = 9119
148
 
149
  srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
36
  fi
37
 
38
  # ==============================================
39
+ # 2. WebDAV 自动备份与轮转模块
40
  # ==============================================
41
  if [[ -n "$WEBDAV_URL" && -n "$WEBDAV_USERNAME" && -n "$WEBDAV_PASSWORD" ]]; then
42
  echo "✅ WebDAV backup enabled"
 
45
  FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
46
  BACKUP_PREFIX="hermes_snapshot_"
47
  SYNC_INTERVAL=${SYNC_INTERVAL:-1800}
48
+ MAX_BACKUPS=${MAX_BACKUPS:-7}
49
 
50
  restore_backup() {
51
  "$PYTHON_BIN" -c "
 
65
  if r.status_code == 200:
66
  with open(local_tmp, 'wb') as f:
67
  for chunk in r.iter_content(8192): f.write(chunk)
 
 
68
  with tarfile.open(local_tmp, 'r:gz') as tar: tar.extractall('/opt/data')
69
  print('[Backup] ✅ State restored successfully')
70
  os.remove(local_tmp)
 
75
 
76
  backup_daemon() {
77
  sleep 15
78
+ echo "[Backup] Daemon started, interval: ${SYNC_INTERVAL}s, max backups: ${MAX_BACKUPS}"
79
  while true; do
80
  sleep $SYNC_INTERVAL
81
  TS=$(date +%Y%m%d_%H%M%S)
 
85
  curl -f -s -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "$TMP_FILE" "$FULL_WEBDAV_URL/$FNAME"
86
  if [ $? -eq 0 ]; then
87
  echo "[Backup] ✅ Snapshot created: $FNAME"
88
+
89
+ # 🔥 重新加回来的旧备份自动删除模块
90
+ "$PYTHON_BIN" -c "
91
+ from webdav3.client import Client
92
+ opts = {'webdav_hostname': '$FULL_WEBDAV_URL', 'webdav_login': '$WEBDAV_USERNAME', 'webdav_password': '$WEBDAV_PASSWORD', 'disable_check': True}
93
+ try:
94
+ client = Client(opts)
95
+ backups = [f for f in client.list() if f.endswith('.tar.gz') and f.startswith('$BACKUP_PREFIX')]
96
+ backups.sort()
97
+ limit = int('$MAX_BACKUPS')
98
+ if len(backups) > limit:
99
+ for f in backups[:-limit]:
100
+ client.clean(f)
101
+ print(f'[Backup] 🗑️ Cleaned up old snapshot: {f}')
102
+ except Exception as e:
103
+ print(f'[Backup] ⚠️ Rotation error: {e}')
104
+ "
105
  fi
106
  rm -f "$TMP_FILE"
107
  done
 
144
 
145
  def handle(client):
146
  try:
 
147
  first_chunk = client.recv(8192)
148
  if not first_chunk:
149
  client.close()
 
154
  except:
155
  req_line = ''
156
 
 
157
  if '/v1/' in req_line or '/chat/' in req_line or '/models' in req_line:
158
  target_port = 8642
159
  else:
 
160
  target_port = 9119
161
 
162
  srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)