xjf666 commited on
Commit
7b0d7f1
·
verified ·
1 Parent(s): 29dc51f

Create sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +129 -0
sync_data.sh ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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 下载最新备份..."
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 WebDAV..."
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
+ # 上传新备份到 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
+ # 清理旧备份文件
100
+ python3 -c "
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
113
+ for file in backups[:to_delete]:
114
+ client.clean(file)
115
+ print(f'Successfully deleted {file}.')
116
+ else:
117
+ print('Only {} backups found, no need to clean.'.format(len(backups)))
118
+ " 2>&1
119
+
120
+ rm -f "/tmp/${backup_file}"
121
+
122
+ SYNC_INTERVAL=${SYNC_INTERVAL:-40}
123
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
124
+ sleep $SYNC_INTERVAL
125
+ done
126
+ }
127
+
128
+ # 启动同步进程
129
+ sync_data &