nbugs commited on
Commit
983ba9f
·
verified ·
1 Parent(s): 977645e

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +21 -49
sync_data.sh CHANGED
@@ -1,26 +1,39 @@
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',
@@ -28,72 +41,31 @@ try:
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
 
1
  #!/bin/sh
 
2
  # 检查环境变量
3
  if [ -z "$HF_TOKEN" ] || [ -z "$DATASET_ID" ]; then
4
  echo "Starting without backup functionality - missing HF_TOKEN or DATASET_ID"
5
  exec node ./src/app/app.js
6
  exit 0
7
  fi
 
8
  # 激活虚拟环境
9
  . /opt/venv/bin/activate
10
 
11
+ # 上传备份(新增备份数量管理)
12
  upload_backup() {
13
  file_path="$1"
14
  file_name="$2"
 
15
  python3 -c "
16
  from huggingface_hub import HfApi
17
  import sys
18
  import os
19
+
20
+ def manage_backups(api, repo_id, max_files=50):
21
+ files = api.list_repo_files(repo_id=repo_id, repo_type='dataset')
22
+ # 注意前缀改为electerm_backup_
23
+ backup_files = [f for f in files if f.startswith('electerm_backup_') and f.endswith('.tar.gz')]
24
+ backup_files.sort()
25
+ if len(backup_files) >= max_files:
26
+ files_to_delete = backup_files[:(len(backup_files) - max_files + 1)]
27
+ for file_to_delete in files_to_delete:
28
+ try:
29
+ api.delete_file(path_in_repo=file_to_delete, repo_id=repo_id, repo_type='dataset')
30
+ print(f'Deleted old backup: {file_to_delete}')
31
+ except Exception as e:
32
+ print(f'Error deleting {file_to_delete}: {str(e)}')
33
+
34
  api = HfApi(token='$HF_TOKEN')
35
  try:
36
+ # 先上传新备份
37
  api.upload_file(
38
  path_or_fileobj='$file_path',
39
  path_in_repo='$file_name',
 
41
  repo_type='dataset'
42
  )
43
  print(f'Successfully uploaded $file_name')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ # 上传完成后执行备份数量管理
46
+ manage_backups(api, '$DATASET_ID')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  except Exception as e:
48
+ print(f'Error uploading file: {str(e)}')
49
  "
50
  }
51
 
52
+ # [...] 后续的download_latest_backup和sync_data函数保持不变
 
 
53
 
54
  # 同步函数
55
  sync_data() {
56
  while true; do
57
  echo "Starting sync process at $(date)"
 
58
  if [ -d /app/electerm-web/data ]; then
59
  timestamp=$(date +%Y%m%d_%H%M%S)
60
  backup_file="electerm_backup_${timestamp}.tar.gz"
 
61
  # 压缩数据目录
62
  tar -czf "/tmp/${backup_file}" -C /app/electerm-web/data .
 
63
  echo "Uploading backup to HuggingFace..."
64
  upload_backup "/tmp/${backup_file}" "${backup_file}"
 
65
  rm -f "/tmp/${backup_file}"
66
  else
67
  echo "Data directory does not exist yet, waiting for next sync..."
68
  fi
 
69
  SYNC_INTERVAL=${SYNC_INTERVAL:-7200}
70
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
71
  sleep $SYNC_INTERVAL