ggcghh commited on
Commit
461a060
·
verified ·
1 Parent(s): a9a1cbd

Create sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +107 -0
sync_data.sh ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+
3
+ # 检查环境变量
4
+ if [ -z "$HF_TOKEN" ] || [ -z "$DATASET_ID" ]; then
5
+ echo "Starting without backup functionality - missing HF_TOKEN or DATASET_ID"
6
+ exec node ./src/app/app.js
7
+ exit 0
8
+ fi
9
+
10
+ # 激活虚拟环境
11
+ . /opt/venv/bin/activate
12
+
13
+ # 上传备份
14
+ upload_backup() {
15
+ file_path="$1"
16
+ file_name="$2"
17
+
18
+ python3 -c "
19
+ from huggingface_hub import HfApi
20
+ import sys
21
+ import os
22
+ api = HfApi(token='$HF_TOKEN')
23
+ try:
24
+ api.upload_file(
25
+ path_or_fileobj='$file_path',
26
+ path_in_repo='$file_name',
27
+ repo_id='$DATASET_ID',
28
+ repo_type='dataset'
29
+ )
30
+ print(f'Successfully uploaded $file_name')
31
+ except Exception as e:
32
+ print(f'Error uploading file: {str(e)}')
33
+ "
34
+ }
35
+
36
+ # 下载最新备份
37
+ download_latest_backup() {
38
+ python3 -c "
39
+ from huggingface_hub import HfApi
40
+ import sys
41
+ import os
42
+ import tarfile
43
+ import tempfile
44
+ api = HfApi(token='$HF_TOKEN')
45
+ try:
46
+ files = api.list_repo_files(repo_id='$DATASET_ID', repo_type='dataset')
47
+ backup_files = [f for f in files if f.startswith('electerm_backup_') and f.endswith('.tar.gz')]
48
+
49
+ if not backup_files:
50
+ print('No backup files found')
51
+ sys.exit()
52
+
53
+ latest_backup = sorted(backup_files)[-1]
54
+
55
+ with tempfile.TemporaryDirectory() as temp_dir:
56
+ filepath = api.hf_hub_download(
57
+ repo_id='$DATASET_ID',
58
+ filename=latest_backup,
59
+ repo_type='dataset',
60
+ local_dir=temp_dir
61
+ )
62
+
63
+ if filepath and os.path.exists(filepath):
64
+ with tarfile.open(filepath, 'r:gz') as tar:
65
+ tar.extractall('/app/electerm-web/data')
66
+ print(f'Successfully restored backup from {latest_backup}')
67
+
68
+ except Exception as e:
69
+ print(f'Error downloading backup: {str(e)}')
70
+ "
71
+ }
72
+
73
+ # 首次启动时下载最新备份
74
+ echo "Downloading latest backup from HuggingFace..."
75
+ download_latest_backup
76
+
77
+ # 同步函数
78
+ sync_data() {
79
+ while true; do
80
+ echo "Starting sync process at $(date)"
81
+
82
+ if [ -d /app/electerm-web/data ]; then
83
+ timestamp=$(date +%Y%m%d_%H%M%S)
84
+ backup_file="electerm_backup_${timestamp}.tar.gz"
85
+
86
+ # 压缩数据目录
87
+ tar -czf "/tmp/${backup_file}" -C /app/electerm-web/data .
88
+
89
+ echo "Uploading backup to HuggingFace..."
90
+ upload_backup "/tmp/${backup_file}" "${backup_file}"
91
+
92
+ rm -f "/tmp/${backup_file}"
93
+ else
94
+ echo "Data directory does not exist yet, waiting for next sync..."
95
+ fi
96
+
97
+ SYNC_INTERVAL=${SYNC_INTERVAL:-7200}
98
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
99
+ sleep $SYNC_INTERVAL
100
+ done
101
+ }
102
+
103
+ # 后台启动同步进程
104
+ sync_data &
105
+
106
+ # 启动 Electerm
107
+ exec node ./src/app/app.js