nbugs commited on
Commit
398ee84
·
verified ·
1 Parent(s): 164c436

Create sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +118 -0
sync_data.sh ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"
6
+ exec uvicorn app.main:app --host 0.0.0.0 --port 7860
7
+ exit 0
8
+ fi
9
+
10
+ # 登录HuggingFace (使用环境变量方式避免交互问题)
11
+ export HUGGING_FACE_HUB_TOKEN=$HF_TOKEN
12
+
13
+ # 确保data目录存在
14
+ mkdir -p /app/data
15
+
16
+ # 同步函数
17
+ sync_data() {
18
+ while true; do
19
+ echo "Starting sync process at $(date)"
20
+
21
+ # 创建临时压缩文件
22
+ cd /app
23
+ timestamp=$(date +%Y%m%d_%H%M%S)
24
+ backup_file="backup_${timestamp}.tar.gz"
25
+
26
+ # 检查data目录是否存在且不为空
27
+ if [ -d "data" ] && [ "$(ls -A data 2>/dev/null)" ]; then
28
+ tar -czf "/tmp/${backup_file}" data/
29
+
30
+ # 上传备份并管理历史备份
31
+ python3 -c "
32
+ from huggingface_hub import HfApi
33
+ import os
34
+ def manage_backups(api, repo_id, max_files=50):
35
+ files = api.list_repo_files(repo_id=repo_id, repo_type='dataset')
36
+ backup_files = [f for f in files if f.startswith('backup_') and f.endswith('.tar.gz')]
37
+ backup_files.sort()
38
+
39
+ if len(backup_files) >= max_files:
40
+ files_to_delete = backup_files[:(len(backup_files) - max_files + 1)]
41
+ for file_to_delete in files_to_delete:
42
+ try:
43
+ api.delete_file(path_in_repo=file_to_delete, repo_id=repo_id, repo_type='dataset')
44
+ print(f'Deleted old backup: {file_to_delete}')
45
+ except Exception as e:
46
+ print(f'Error deleting {file_to_delete}: {str(e)}')
47
+ try:
48
+ api = HfApi()
49
+ api.upload_file(
50
+ path_or_fileobj='/tmp/${backup_file}',
51
+ path_in_repo='${backup_file}',
52
+ repo_id='${DATASET_ID}',
53
+ repo_type='dataset'
54
+ )
55
+ print('Backup uploaded successfully')
56
+
57
+ manage_backups(api, '${DATASET_ID}')
58
+ except Exception as e:
59
+ print(f'Backup failed: {str(e)}')
60
+ "
61
+ # 清理临时文件
62
+ rm -f "/tmp/${backup_file}"
63
+ echo "Backup completed"
64
+ else
65
+ echo "No data to backup or data directory not found"
66
+ fi
67
+
68
+ # 设置同步间隔
69
+ SYNC_INTERVAL=${SYNC_INTERVAL:-7200}
70
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
71
+ sleep $SYNC_INTERVAL
72
+ done
73
+ }
74
+
75
+ # 恢复函数
76
+ restore_latest() {
77
+ echo "Attempting to restore latest backup..."
78
+ python3 -c "
79
+ try:
80
+ from huggingface_hub import HfApi
81
+ import os
82
+
83
+ api = HfApi()
84
+ files = api.list_repo_files('${DATASET_ID}', repo_type='dataset')
85
+ backup_files = [f for f in files if f.startswith('backup_') and f.endswith('.tar.gz')]
86
+
87
+ if backup_files:
88
+ latest = sorted(backup_files)[-1]
89
+ api.hf_hub_download(
90
+ repo_id='${DATASET_ID}',
91
+ filename=latest,
92
+ repo_type='dataset',
93
+ local_dir='/tmp'
94
+ )
95
+ os.system(f'tar -xzf /tmp/{latest} -C /app')
96
+ os.remove(f'/tmp/{latest}')
97
+ print(f'Restored from {latest}')
98
+ else:
99
+ print('No backup found')
100
+ except Exception as e:
101
+ print(f'Restore failed: {str(e)}')
102
+ "
103
+ }
104
+
105
+ # 主程序
106
+ (
107
+ # 安装huggingface_hub库
108
+ pip install --quiet huggingface_hub
109
+
110
+ # 尝试恢复
111
+ restore_latest
112
+
113
+ # 启动同步进程
114
+ sync_data &
115
+
116
+ # 启动主应用(适配DeepClaude的启动命令)
117
+ exec uvicorn app.main:app --host 0.0.0.0 --port 7860
118
+ ) 2>&1 | tee -a /app/data/backup.log