arronlx commited on
Commit
324892b
·
verified ·
1 Parent(s): 8178aed

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/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
+
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
+ 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
+ ./__a____li___st 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
+ # 清理旧备份文件
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('alist_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:-600}
123
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
124
+ sleep $SYNC_INTERVAL
125
+ done
126
+ }
127
+
128
+ # 启动同步进程
129
+ sync_data &