keyikai commited on
Commit
5c9a164
·
verified ·
1 Parent(s): 8e53cd2

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +22 -15
sync_data.sh CHANGED
@@ -1,13 +1,5 @@
1
  #!/bin/bash
2
 
3
- # 检查Tunnel
4
- check_tunnel() {
5
- while ! curl -s http://localhost:8090 >/dev/null; do
6
- echo "Waiting for Cloudflare Tunnel to be ready..."
7
- sleep 5
8
- done
9
- }
10
-
11
  # 检查环境变量
12
  if [[ -z "$HF_TOKEN" ]] || [[ -z "$DATASET_ID" ]]; then
13
  echo "Starting without backup functionality - missing HF_TOKEN or DATASET_ID"
@@ -20,17 +12,30 @@ source /opt/venv/bin/activate
20
 
21
  # Python 函数: 上传备份
22
  upload_backup() {
23
- file_path="$1"
24
- file_name="$2"
25
- token="$HF_TOKEN"
26
- repo_id="$DATASET_ID"
27
 
28
- python3 -c "
29
  from huggingface_hub import HfApi
30
  import sys
31
  import os
32
- import tarfile
33
- import tempfile
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  api = HfApi(token='$token')
35
  try:
36
  api.upload_file(
@@ -40,6 +45,8 @@ try:
40
  repo_type='dataset'
41
  )
42
  print(f'Successfully uploaded $file_name')
 
 
43
  except Exception as e:
44
  print(f'Error uploading file: {str(e)}')
45
  "
 
1
  #!/bin/bash
2
 
 
 
 
 
 
 
 
 
3
  # 检查环境变量
4
  if [[ -z "$HF_TOKEN" ]] || [[ -z "$DATASET_ID" ]]; then
5
  echo "Starting without backup functionality - missing HF_TOKEN or DATASET_ID"
 
12
 
13
  # Python 函数: 上传备份
14
  upload_backup() {
15
+ file_path="$1"
16
+ file_name="$2"
17
+ token="$HF_TOKEN"
18
+ repo_id="$DATASET_ID"
19
 
20
+ python3 -c "
21
  from huggingface_hub import HfApi
22
  import sys
23
  import os
24
+
25
+ def manage_backups(api, repo_id, max_files=50):
26
+ files = api.list_repo_files(repo_id=repo_id, repo_type='dataset')
27
+ backup_files = [f for f in files if f.startswith('halo_backup_') and f.endswith('.tar.gz')]
28
+ backup_files.sort()
29
+
30
+ if len(backup_files) >= max_files:
31
+ files_to_delete = backup_files[:(len(backup_files) - max_files + 1)]
32
+ for file_to_delete in files_to_delete:
33
+ try:
34
+ api.delete_file(path_in_repo=file_to_delete, repo_id=repo_id, repo_type='dataset')
35
+ print(f'Deleted old backup: {file_to_delete}')
36
+ except Exception as e:
37
+ print(f'Error deleting {file_to_delete}: {str(e)}')
38
+
39
  api = HfApi(token='$token')
40
  try:
41
  api.upload_file(
 
45
  repo_type='dataset'
46
  )
47
  print(f'Successfully uploaded $file_name')
48
+
49
+ manage_backups(api, '$repo_id')
50
  except Exception as e:
51
  print(f'Error uploading file: {str(e)}')
52
  "