hiwaknvz commited on
Commit
a0b7b53
·
verified ·
1 Parent(s): b0ecf31

Create sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +131 -0
sync_data.sh ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # 检查环境变量
4
+ if [[ -z "$WEBDAV_URL" ]] || [[ -z "$WEBDAV_USERNAME" ]] || [[ -z "$WEBDAV_PASSWORD" ]]; then
5
+ echo "缺少 WEBDAV_URL、WEBDAV_USERNAME 或 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
+ 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
+ }
31
+ client = Client(options)
32
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('webui_backup_')]
33
+ if not backups:
34
+ print('没有找到备份文件')
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):
42
+ f.write(chunk)
43
+ print(f'成功下载备份文件到 /tmp/{latest_backup}')
44
+ if os.path.exists(f'/tmp/{latest_backup}'):
45
+ # 解压备份文件到临时目录
46
+ temp_dir = '/tmp/restore'
47
+ os.makedirs(temp_dir, exist_ok=True)
48
+ tar = tarfile.open(f'/tmp/{latest_backup}', 'r:gz')
49
+ tar.extractall(temp_dir)
50
+ tar.close()
51
+ # 查找并移动 webui.db 文件
52
+ for root, dirs, files in os.walk(temp_dir):
53
+ if 'webui.db' in files:
54
+ db_path = os.path.join(root, 'webui.db')
55
+ os.makedirs('./data', exist_ok=True)
56
+ os.replace(db_path, './data/webui.db')
57
+ print(f'成功从 {latest_backup} 恢复备份')
58
+ break
59
+ else:
60
+ print('备份文件中未找到 webui.db')
61
+ # 删除临时目录
62
+ try:
63
+ shutil.rmtree(temp_dir)
64
+ except Exception as e:
65
+ print(f'删除临时目录时出错:{e}')
66
+ os.remove(f'/tmp/{latest_backup}')
67
+ else:
68
+ print('下载的备份文件不存在')
69
+ else:
70
+ print(f'下载备份失败:{r.status_code}')
71
+ "
72
+ }
73
+
74
+ # 首次启动时下载最新备份
75
+ echo "正在从 WebDAV 下载最新备份..."
76
+ restore_backup
77
+
78
+ # 同步函数
79
+ sync_data() {
80
+ while true; do
81
+ echo "在 $(date) 开始同步进程"
82
+
83
+ if [ -f "./data/webui.db" ]; then
84
+ timestamp=$(date +%Y%m%d_%H%M%S)
85
+ backup_file="webui_backup_${timestamp}.tar.gz"
86
+
87
+ # 打包数据库文件
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
95
+ echo "上传 ${backup_file} 至 WebDAV 失败"
96
+ fi
97
+
98
+ # 清理旧备份文件
99
+ python3 -c "
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
+ }
107
+ client = Client(options)
108
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('webui_backup_')]
109
+ backups.sort()
110
+ if len(backups) > 5:
111
+ to_delete = len(backups) - 5
112
+ for file in backups[:to_delete]:
113
+ client.clean(file)
114
+ print(f'成功删除 {file}。')
115
+ else:
116
+ print('仅找到 {} 个备份,无需清理。'.format(len(backups)))
117
+ " 2>&1
118
+
119
+ rm -f "/tmp/${backup_file}"
120
+ else
121
+ echo "数据库文件尚不存在,等待下次同步..."
122
+ fi
123
+
124
+ SYNC_INTERVAL=${SYNC_INTERVAL:-600}
125
+ echo "下次同步将在 ${SYNC_INTERVAL} 秒后进行..."
126
+ sleep $SYNC_INTERVAL
127
+ done
128
+ }
129
+
130
+ # 后台启动同步进程
131
+ sync_data &