dragggl25 commited on
Commit
5ab404f
·
verified ·
1 Parent(s): a1b22be

Create sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +166 -0
sync_data.sh ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ mkdir -p /data
18
+
19
+ # 下载最新备份并恢复
20
+ restore_backup() {
21
+ echo "开始从 WebDAV 下载最新备份..."
22
+ # 显式使用虚拟环境中的 python3,确保找到安装的库
23
+ /app/venv/bin/python3 -c "
24
+ import sys
25
+ import os
26
+ import tarfile
27
+ import requests
28
+ from webdav3.client import Client
29
+ import shutil
30
+ options = {
31
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
32
+ 'webdav_login': '$WEBDAV_USERNAME',
33
+ 'webdav_password': '$WEBDAV_PASSWORD'
34
+ }
35
+ client = Client(options)
36
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('cloudpaste_backup_')]
37
+ if not backups:
38
+ print('没有找到备份文件')
39
+ sys.exit()
40
+ latest_backup = sorted(backups)[-1]
41
+ print(f'最新备份文件:{latest_backup}')
42
+ with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
43
+ if r.status_code == 200:
44
+ with open(f'/tmp/{latest_backup}', 'wb') as f:
45
+ for chunk in r.iter_content(chunk_size=8192):
46
+ f.write(chunk)
47
+ print(f'成功下载备份文件到 /tmp/{latest_backup}')
48
+ if os.path.exists(f'/tmp/{latest_backup}'):
49
+ # 解压备份文件到临时目录
50
+ temp_dir = '/tmp/restore'
51
+ os.makedirs(temp_dir, exist_ok=True)
52
+ tar = tarfile.open(f'/tmp/{latest_backup}', 'r:gz')
53
+ tar.extractall(temp_dir)
54
+ tar.close()
55
+ # 查找并移动 cloudpaste.db 文件
56
+ for root, dirs, files in os.walk(temp_dir):
57
+ for file in files:
58
+ if file.endswith('.db'): # 查找任何.db文件
59
+ db_path = os.path.join(root, file)
60
+ os.makedirs('/data', exist_ok=True)
61
+ target_path = os.path.join('/data', 'cloudpaste.db')
62
+ os.replace(db_path, target_path)
63
+ print(f'成功从 {latest_backup} 恢复备份到 {target_path}')
64
+ break
65
+ else:
66
+ continue
67
+ break
68
+ else:
69
+ print('备份文件中未找到数据库文件')
70
+ # 删除临时目录
71
+ try:
72
+ shutil.rmtree(temp_dir)
73
+ except Exception as e:
74
+ print(f'删除临时目录时出错:{e}')
75
+ os.remove(f'/tmp/{latest_backup}')
76
+ else:
77
+ print('下载的备份文件不存在')
78
+ else:
79
+ print(f'下载备份失败:{r.status_code}')
80
+ "
81
+ }
82
+
83
+ # 首次启动时下载最新备份
84
+ echo "正在从 WebDAV 下载最新备份..."
85
+ restore_backup
86
+
87
+ #pip install webdav3-client requests >/dev/null 2>&1
88
+
89
+ # 同步函数
90
+ sync_data() {
91
+ while true; do
92
+ echo "在 $(date) 开始同步进程"
93
+
94
+ # 检查数据库文件是否存在
95
+ DB_FILE="/data/cloudpaste.db"
96
+ if [ -f "$DB_FILE" ]; then
97
+ timestamp=$(date +%Y%m%d_%H%M%S)
98
+ backup_file="cloudpaste_backup_${timestamp}.tar.gz"
99
+
100
+ # 确保备份目录存在
101
+ mkdir -p /tmp/backup
102
+
103
+ # 创建备份目录结构
104
+ cp "$DB_FILE" "/tmp/backup/cloudpaste.db"
105
+
106
+ # 打包数据库文件
107
+ tar -czf "/tmp/${backup_file}" -C /tmp/backup .
108
+
109
+ # 上传新备份到WebDAV
110
+ curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
111
+ if [ $? -eq 0 ]; then
112
+ echo "成功将 ${backup_file} 上传至 WebDAV"
113
+
114
+ # 同时上传一个固定名称的最新备份
115
+ curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/cloudpaste_latest.tar.gz"
116
+ if [ $? -eq 0 ]; then
117
+ echo "成功将最新备份上传至 WebDAV"
118
+ else
119
+ echo "上传最新备份至 WebDAV 失败"
120
+ fi
121
+ else
122
+ echo "上传 ${backup_file} 至 WebDAV 失败"
123
+ fi
124
+
125
+ # 清理临时文件
126
+ rm -rf /tmp/backup
127
+ rm -f "/tmp/${backup_file}"
128
+
129
+ # 清理旧备份文件,保留最近10个
130
+ # 显式使用虚拟环境中的 python3,确保找到安装的库
131
+ # python3 -c "
132
+ /app/venv/bin/python3 -c "
133
+ import sys
134
+ from webdav3.client import Client
135
+ options = {
136
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
137
+ 'webdav_login': '$WEBDAV_USERNAME',
138
+ 'webdav_password': '$WEBDAV_PASSWORD'
139
+ }
140
+ client = Client(options)
141
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('cloudpaste_backup_')]
142
+ backups.sort()
143
+ if len(backups) > 5: # 保留最近5个备份
144
+ to_delete = len(backups) - 10
145
+ for file in backups[:to_delete]:
146
+ try:
147
+ client.clean(file)
148
+ print(f'成功删除旧备份: {file}')
149
+ except Exception as e:
150
+ print(f'删除 {file} 时出错: {e}')
151
+ else:
152
+ print(f'仅找到 {len(backups)} 个备份,无需清理')
153
+ " 2>&1
154
+ else
155
+ echo "数据库文件 $DB_FILE 尚不存在,等待下次同步..."
156
+ fi
157
+
158
+ # 设置同步间隔,默认20分钟
159
+ SYNC_INTERVAL=${SYNC_INTERVAL:-1200}
160
+ echo "下次同步将在 ${SYNC_INTERVAL} 秒后进行..."
161
+ sleep $SYNC_INTERVAL
162
+ done
163
+ }
164
+
165
+ # 后台启动同步进程
166
+ sync_data &