Orion-zhen commited on
Commit
223e428
·
verified ·
1 Parent(s): 308ef8e

Upload sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +12 -5
sync_data.sh CHANGED
@@ -6,6 +6,13 @@ if [[ -z "$WEBDAV_URL" ]] || [[ -z "$WEBDAV_USERNAME" ]] || [[ -z "$WEBDAV_PASSW
6
  exit 0
7
  fi
8
 
 
 
 
 
 
 
 
9
  # 下载最新备份并恢复
10
  restore_backup() {
11
  echo "开始从 WebDAV 下载最新备份..."
@@ -17,7 +24,7 @@ import requests
17
  from webdav3.client import Client
18
  import shutil
19
  options = {
20
- 'webdav_hostname': '$WEBDAV_URL',
21
  'webdav_login': '$WEBDAV_USERNAME',
22
  'webdav_password': '$WEBDAV_PASSWORD'
23
  }
@@ -28,7 +35,7 @@ if not backups:
28
  sys.exit()
29
  latest_backup = sorted(backups)[-1]
30
  print(f'最新备份文件:{latest_backup}')
31
- with requests.get(f'$WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
32
  if r.status_code == 200:
33
  with open(f'/tmp/{latest_backup}', 'wb') as f:
34
  for chunk in r.iter_content(chunk_size=8192):
@@ -81,7 +88,7 @@ sync_data() {
81
  tar -czf "/tmp/${backup_file}" ./data/webui.db
82
 
83
  # 上传新备份到WebDAV
84
- curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$WEBDAV_URL/${backup_file}"
85
  if [ $? -eq 0 ]; then
86
  echo "成功将 ${backup_file} 上传至 WebDAV"
87
  else
@@ -93,7 +100,7 @@ sync_data() {
93
  import sys
94
  from webdav3.client import Client
95
  options = {
96
- 'webdav_hostname': '$WEBDAV_URL',
97
  'webdav_login': '$WEBDAV_USERNAME',
98
  'webdav_password': '$WEBDAV_PASSWORD'
99
  }
@@ -121,4 +128,4 @@ else:
121
  }
122
 
123
  # 后台启动同步进程
124
- sync_data &
 
6
  exit 0
7
  fi
8
 
9
+ # 设置备份路径
10
+ WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}
11
+ FULL_WEBDAV_URL="${WEBDAV_URL}"
12
+ if [ -n "$WEBDAV_BACKUP_PATH" ]; then
13
+ FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
14
+ fi
15
+
16
  # 下载最新备份并恢复
17
  restore_backup() {
18
  echo "开始从 WebDAV 下载最新备份..."
 
24
  from webdav3.client import Client
25
  import shutil
26
  options = {
27
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
28
  'webdav_login': '$WEBDAV_USERNAME',
29
  'webdav_password': '$WEBDAV_PASSWORD'
30
  }
 
35
  sys.exit()
36
  latest_backup = sorted(backups)[-1]
37
  print(f'最新备份文件:{latest_backup}')
38
+ with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
39
  if r.status_code == 200:
40
  with open(f'/tmp/{latest_backup}', 'wb') as f:
41
  for chunk in r.iter_content(chunk_size=8192):
 
88
  tar -czf "/tmp/${backup_file}" ./data/webui.db
89
 
90
  # 上传新备份到WebDAV
91
+ curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
92
  if [ $? -eq 0 ]; then
93
  echo "成功将 ${backup_file} 上传至 WebDAV"
94
  else
 
100
  import sys
101
  from webdav3.client import Client
102
  options = {
103
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
104
  'webdav_login': '$WEBDAV_USERNAME',
105
  'webdav_password': '$WEBDAV_PASSWORD'
106
  }
 
128
  }
129
 
130
  # 后台启动同步进程
131
+ sync_data &