gallyga commited on
Commit
cf265e6
·
verified ·
1 Parent(s): 467a8b7

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +53 -53
sync_data.sh CHANGED
@@ -1,8 +1,8 @@
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
  exit 0
7
  fi
8
 
@@ -13,87 +13,87 @@ if [ -n "$WEBDAV_BACKUP_PATH" ]; then
13
  FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
14
  fi
15
 
16
- # 激活虚拟环境
17
- source $HOME/venv/bin/activate
18
-
19
  # 下载最新备份并恢复
20
  restore_backup() {
21
- echo "开始从 WebDAV 下载最新备份..."
22
  python3 -c "
23
  import sys
24
  import os
25
  import tarfile
26
  import requests
27
  import shutil
 
28
  from webdav3.client import Client
 
29
  options = {
30
- 'webdav_hostname': '$FULL_WEBDAV_URL',
31
- 'webdav_login': '$WEBDAV_USERNAME',
32
- 'webdav_password': '$WEBDAV_PASSWORD'
33
  }
34
  client = Client(options)
35
- backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('alist_backup_')]
 
 
 
 
 
36
  if not backups:
37
- print('没有找到备份文件')
38
- sys.exit()
 
39
  latest_backup = sorted(backups)[-1]
40
  print(f'最新备份文件:{latest_backup}')
41
- with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
42
- if r.status_code == 200:
43
- with open(f'/tmp/{latest_backup}', 'wb') as f:
44
- for chunk in r.iter_content(chunk_size=8192):
45
- f.write(chunk)
46
- print(f'成功下载备份文件到 /tmp/{latest_backup}')
47
- if os.path.exists(f'/tmp/{latest_backup}'):
48
- # 如果目录已存在,先删除它
49
- if os.path.exists('$HOME/data'):
50
- shutil.rmtree('$HOME/data')
51
- os.makedirs('$HOME/data', exist_ok=True)
52
-
 
 
 
 
 
53
  # 解压备份文件
54
  with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
55
- tar.extractall('$HOME/data')
56
-
57
  print(f'成功从 {latest_backup} 恢复备份')
58
  else:
59
- print('下载备份文件不存在')
60
- else:
61
- print(f'下载备份失败:{r.status_code}')
62
  "
63
  }
64
 
65
  # 首次启动时下载最新备份
66
- echo "Downloading latest backup from WebDAV..."
67
  restore_backup
68
 
69
- # 等待30秒后启动程序
70
- sleep 30
71
-
72
- # 启动程序
73
- ./app server &
74
-
75
  # 同步函数
76
  sync_data() {
77
  while true; do
78
  echo "Starting sync process at $(date)"
79
 
80
- if [ ! -d $HOME/data ]; then
81
- mkdir -p $HOME/data
82
- echo "Data directory created."
83
- fi
84
-
85
  timestamp=$(date +%Y%m%d_%H%M%S)
86
- backup_file="alist_backup_${timestamp}.tar.gz"
87
 
88
- # 压缩数据目录
89
- tar -czf "/tmp/${backup_file}" -C $HOME/data .
 
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
  else
96
- echo "Failed to upload ${backup_file} to WebDAV"
97
  fi
98
 
99
  # 清理旧备份文件
@@ -101,12 +101,12 @@ sync_data() {
101
  import sys
102
  from webdav3.client import Client
103
  options = {
104
- 'webdav_hostname': '$FULL_WEBDAV_URL',
105
- 'webdav_login': '$WEBDAV_USERNAME',
106
- 'webdav_password': '$WEBDAV_PASSWORD'
107
  }
108
  client = Client(options)
109
- backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('alist_backup_')]
110
  backups.sort()
111
  if len(backups) > 5:
112
  to_delete = len(backups) - 5
@@ -118,7 +118,7 @@ else:
118
  " 2>&1
119
 
120
  rm -f "/tmp/${backup_file}"
121
-
122
  SYNC_INTERVAL=${SYNC_INTERVAL:-600}
123
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
124
  sleep $SYNC_INTERVAL
 
1
+ #!/bin/sh
2
 
3
  # 检查环境变量
4
+ if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
5
+ echo "Starting without sync functionality - missing WEBDAV_URL, WEBDAV_USERNAME, or WEBDAV_PASSWORD"
6
  exit 0
7
  fi
8
 
 
13
  FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
14
  fi
15
 
 
 
 
16
  # 下载最新备份并恢复
17
  restore_backup() {
18
+ echo "开始从 网页DAV 下载最新备份..."
19
  python3 -c "
20
  import sys
21
  import os
22
  import tarfile
23
  import requests
24
  import shutil
25
+ import subprocess
26
  from webdav3.client import Client
27
+ # 设置 WebDAV 客户端
28
  options = {
29
+ 'webdav_hostname': '${FULL_WEBDAV_URL}',
30
+ 'webdav_login': '${WEBDAV_USERNAME}',
31
+ 'webdav_password': '${WEBDAV_PASSWORD}'
32
  }
33
  client = Client(options)
34
+ # 列出备份文件
35
+ try:
36
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('qexo_backup_')]
37
+ except Exception as e:
38
+ print(f'连接 WebDAV 服务器出错: {e}')
39
+ sys.exit(1)
40
  if not backups:
41
+ print('没有找到备份文件,跳过恢复步骤。')
42
+ sys.exit(0)
43
+ # 获取最新备份文件
44
  latest_backup = sorted(backups)[-1]
45
  print(f'最新备份文件:{latest_backup}')
46
+ # 下载最新备份
47
+ try:
48
+ with requests.get(f'${FULL_WEBDAV_URL}/{latest_backup}', auth=('${WEBDAV_USERNAME}', '${WEBDAV_PASSWORD}'), stream=True) as r:
49
+ if r.status_code == 200:
50
+ with open(f'/tmp/{latest_backup}', 'wb') as f:
51
+ for chunk in r.iter_content(chunk_size=8192):
52
+ f.write(chunk)
53
+ print(f'成功下载备份文件到 /tmp/{latest_backup}')
54
+ # 检查数据库文件是否被占用
55
+ try:
56
+ pids = subprocess.check_output(['lsof', '-t', '/app/db/db.sqlite3']).decode().strip()
57
+ if pids:
58
+ print('数据库文件被占用,无法恢复备份。')
59
+ sys.exit(1)
60
+ except subprocess.CalledProcessError:
61
+ # 没有进程占用数据库文件
62
+ pass
63
  # 解压备份文件
64
  with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
65
+ tar.extractall('/app/')
 
66
  print(f'成功从 {latest_backup} 恢复备份')
67
  else:
68
+ print(f'下载备份失败:{r.status_code}')
69
+ except Exception as e:
70
+ print(f'恢复备份过程中出错: {e}')
71
  "
72
  }
73
 
74
  # 首次启动时下载最新备份
75
+ echo "Downloading latest backup from 网页DAV..."
76
  restore_backup
77
 
 
 
 
 
 
 
78
  # 同步函数
79
  sync_data() {
80
  while true; do
81
  echo "Starting sync process at $(date)"
82
 
83
+ # 直接备份数据库文件
 
 
 
 
84
  timestamp=$(date +%Y%m%d_%H%M%S)
85
+ backup_file="qexo_backup_${timestamp}.tar.gz"
86
 
87
+ # 备份数据库和配置文件
88
+ cd /app
89
+ tar -czf "/tmp/${backup_file}" db/db.sqlite3 configs.py
90
 
91
+ # 上传新备份到 网页DAV
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 网页DAV"
95
  else
96
+ echo "Failed to upload ${backup_file} to 网页DAV"
97
  fi
98
 
99
  # 清理旧备份文件
 
101
  import sys
102
  from webdav3.client import Client
103
  options = {
104
+ 'webdav_hostname': '${FULL_WEBDAV_URL}',
105
+ 'webdav_login': '${WEBDAV_USERNAME}',
106
+ 'webdav_password': '${WEBDAV_PASSWORD}'
107
  }
108
  client = Client(options)
109
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('qexo_backup_')]
110
  backups.sort()
111
  if len(backups) > 5:
112
  to_delete = len(backups) - 5
 
118
  " 2>&1
119
 
120
  rm -f "/tmp/${backup_file}"
121
+
122
  SYNC_INTERVAL=${SYNC_INTERVAL:-600}
123
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
124
  sleep $SYNC_INTERVAL