kpinquan commited on
Commit
21bf11b
·
verified ·
1 Parent(s): 9a0bb83

Update sync_data.sh

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