lin7zhi commited on
Commit
6eb970f
·
verified ·
1 Parent(s): ac9eca0

Create sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +123 -0
sync_data.sh ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ exec java ${JVM_OPTS} -jar /opt/halo/halo.jar
7
+ exit 0
8
+ fi
9
+
10
+ # 设置备份路径
11
+ WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}
12
+ FULL_WEBDAV_URL="${WEBDAV_URL}"
13
+ if [ -n "$WEBDAV_BACKUP_PATH" ]; then
14
+ FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
15
+ fi
16
+
17
+ # 激活虚拟环境
18
+ source /opt/venv/bin/activate
19
+
20
+ # 下载最新备份并恢复
21
+ restore_backup() {
22
+ echo "开始从 WebDAV 下载最新备份..."
23
+ python3 -c "
24
+ import sys
25
+ import os
26
+ import tarfile
27
+ import requests
28
+ import shutil
29
+ from webdav3.client import Client
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('halo_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
+ if os.path.exists(os.path.expanduser('~/.halo2')):
51
+ shutil.rmtree(os.path.expanduser('~/.halo2'))
52
+ os.makedirs(os.path.expanduser('~/.halo2'), exist_ok=True)
53
+
54
+ # 解压备份文件
55
+ with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
56
+ tar.extractall(os.path.expanduser('~/.halo2'))
57
+
58
+ print(f'成功从 {latest_backup} 恢复备份')
59
+ else:
60
+ print('下载的备份文件不存在')
61
+ else:
62
+ print(f'下载备份失败:{r.status_code}')
63
+ "
64
+ }
65
+
66
+ # 首次启动时下载最新备份
67
+ echo "Downloading latest backup from WebDAV..."
68
+ restore_backup
69
+
70
+ # 同步函数
71
+ sync_data() {
72
+ while true; do
73
+ echo "Starting sync process at $(date)"
74
+
75
+ if [ -d ~/.halo2 ]; then
76
+ timestamp=$(date +%Y%m%d_%H%M%S)
77
+ backup_file="halo_backup_${timestamp}.tar.gz"
78
+
79
+ # 压缩数据目录
80
+ tar -czf "/tmp/${backup_file}" -C ~/.halo2 .
81
+
82
+ # 上传新备份到WebDAV
83
+ curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
84
+ if [ $? -eq 0 ]; then
85
+ echo "Successfully uploaded ${backup_file} to WebDAV"
86
+ else
87
+ echo "Failed to upload ${backup_file} to WebDAV"
88
+ fi
89
+
90
+ # 清理旧备份文件
91
+ python3 -c "
92
+ import sys
93
+ from webdav3.client import Client
94
+ options = {
95
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
96
+ 'webdav_login': '$WEBDAV_USERNAME',
97
+ 'webdav_password': '$WEBDAV_PASSWORD'
98
+ }
99
+ client = Client(options)
100
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('halo_backup_')]
101
+ backups.sort()
102
+ if len(backups) > 5:
103
+ to_delete = len(backups) - 5
104
+ for file in backups[:to_delete]:
105
+ client.clean(file)
106
+ print(f'Successfully deleted {file}.')
107
+ else:
108
+ print('Only {} backups found, no need to clean.'.format(len(backups)))
109
+ " 2>&1
110
+
111
+ rm -f "/tmp/${backup_file}"
112
+ else
113
+ echo "Data directory does not exist yet, waiting for next sync..."
114
+ fi
115
+
116
+ SYNC_INTERVAL=${SYNC_INTERVAL:-600}
117
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
118
+ sleep $SYNC_INTERVAL
119
+ done
120
+ }
121
+
122
+ # 启动同步进程
123
+ sync_data &