Qilan2 commited on
Commit
f31e385
·
verified ·
1 Parent(s): 40acd97

Update ff.py

Browse files
Files changed (1) hide show
  1. ff.py +21 -11
ff.py CHANGED
@@ -12,7 +12,7 @@ import pytz
12
 
13
 
14
  BACKUP_TIME = os.environ.get('BACKUP_TIME', '1200')# 备份时间 10800秒 2小时
15
-
16
  HF_USER1 = os.environ.get('HF_USER1', '')# HF 备份仓库的用户名
17
  HF_REPO = os.environ.get('HF_REPO', '')#HF 备份的Models仓库名
18
  HF_EMAIL = os.environ.get('HF_EMAIL', '') #HF的邮箱
@@ -44,7 +44,7 @@ def get_latest_local_package(directory, pattern='*.tar.gz'):
44
  except Exception as e:
45
  print(f"获取最新包时发生错误: {e}")
46
  return None
47
- def compress_folder(folder_path, output_dir):
48
  try:
49
  # 确保输出目录存在
50
  os.makedirs(output_dir, exist_ok=True)
@@ -60,7 +60,7 @@ def compress_folder(folder_path, output_dir):
60
  timestamp = str(int(datetime.now(china_tz).timestamp() * 1000))
61
  output_path = os.path.join(output_dir, f'{timestamp}.tar.gz')
62
 
63
- # 获取已存在的压缩包
64
  existing_archives = glob.glob(os.path.join(output_dir, '*.tar.gz'))
65
 
66
  # 安全地提取时间戳
@@ -69,15 +69,22 @@ def compress_folder(folder_path, output_dir):
69
  match = re.search(r'(\d+)\.tar\.gz$', filename)
70
  return int(match.group(1)) if match else 0
71
 
72
- # 如果压缩包数量超过5个,删除最旧的
73
- if len(existing_archives) >= 3:
74
- # 按时间戳排序
75
  existing_archives.sort(key=extract_timestamp)
76
 
 
 
 
77
  # 删除最旧的压缩包
78
- oldest_archive = existing_archives[0]
79
- os.remove(oldest_archive)
80
- print(f"删除最旧的压缩包:{oldest_archive}")
 
 
 
 
81
 
82
  # tar.gz 压缩
83
  result = subprocess.run(
@@ -97,6 +104,7 @@ def compress_folder(folder_path, output_dir):
97
  print(f"压缩成功:{output_path}")
98
  print(f"压缩大小:{file_size:.2f} MB")
99
  print(f"压缩时间:{formatted_time}")
 
100
 
101
  # 返回压缩包名和大小信息
102
  return f"{os.path.basename(output_path)} MB:{file_size:.2f} MB TIME:{formatted_time}"
@@ -134,8 +142,9 @@ def github(type):
134
  print(f"开始备份上传HF仓库:{HF_REPO}")
135
  os.system("mkdir -p /data/f")
136
  os.system("cp -rf /data/ff /data/f")
 
137
  # 备份上传仓库
138
- new_archive_info = compress_folder('/data/f', f'/data/{HF_REPO}')
139
  if new_archive_info:
140
  new_archive, file_size_info = new_archive_info.split(' MB:')
141
  os.chdir(f'/data/{HF_REPO}')
@@ -144,6 +153,7 @@ def github(type):
144
  os.system(f'git commit -m "{file_size_info}"')
145
  # os.system('git push -u origin main')
146
  os.system('git push -f origin main')
 
147
  else:
148
  print("压缩失败,无法提交")
149
 
@@ -200,5 +210,5 @@ if os.path.exists('/data/ff/.mozilla/firefox/profiles.ini') and os.path.isfile('
200
  result = restart_huggingface_space(HF_USER2, HF_ID, HF_TOKON2)
201
  print(result)
202
  # break
203
- github(2)
204
  # nv1_agent()
 
12
 
13
 
14
  BACKUP_TIME = os.environ.get('BACKUP_TIME', '1200')# 备份时间 10800秒 2小时
15
+ BACKUP_TIME = 30
16
  HF_USER1 = os.environ.get('HF_USER1', '')# HF 备份仓库的用户名
17
  HF_REPO = os.environ.get('HF_REPO', '')#HF 备份的Models仓库名
18
  HF_EMAIL = os.environ.get('HF_EMAIL', '') #HF的邮箱
 
44
  except Exception as e:
45
  print(f"获取最新包时发生错误: {e}")
46
  return None
47
+ def compress_folder(folder_path, output_dir, keep_count=3):
48
  try:
49
  # 确保输出目录存在
50
  os.makedirs(output_dir, exist_ok=True)
 
60
  timestamp = str(int(datetime.now(china_tz).timestamp() * 1000))
61
  output_path = os.path.join(output_dir, f'{timestamp}.tar.gz')
62
 
63
+ # 获取所有压缩包
64
  existing_archives = glob.glob(os.path.join(output_dir, '*.tar.gz'))
65
 
66
  # 安全地提取时间戳
 
69
  match = re.search(r'(\d+)\.tar\.gz$', filename)
70
  return int(match.group(1)) if match else 0
71
 
72
+ # 如果压缩包数量超过保留数量,删除最旧的
73
+ if len(existing_archives) >= keep_count:
74
+ # 按时间戳排序(从小到大,最旧的在前面)
75
  existing_archives.sort(key=extract_timestamp)
76
 
77
+ # 计算需要删除的数量
78
+ delete_count = len(existing_archives) - keep_count + 1
79
+
80
  # 删除最旧的压缩包
81
+ for i in range(delete_count):
82
+ oldest_archive = existing_archives[i]
83
+ try:
84
+ os.remove(oldest_archive)
85
+ print(f"删除最旧的压缩包:{os.path.basename(oldest_archive)}")
86
+ except Exception as e:
87
+ print(f"删除失败 {oldest_archive}: {e}")
88
 
89
  # tar.gz 压缩
90
  result = subprocess.run(
 
104
  print(f"压缩成功:{output_path}")
105
  print(f"压缩大小:{file_size:.2f} MB")
106
  print(f"压缩时间:{formatted_time}")
107
+ print(f"保留策略:最多保留 {keep_count} 个备份包")
108
 
109
  # 返回压缩包名和大小信息
110
  return f"{os.path.basename(output_path)} MB:{file_size:.2f} MB TIME:{formatted_time}"
 
142
  print(f"开始备份上传HF仓库:{HF_REPO}")
143
  os.system("mkdir -p /data/f")
144
  os.system("cp -rf /data/ff /data/f")
145
+ os.system('git lfs prune')
146
  # 备份上传仓库
147
+ new_archive_info = compress_folder('/data/f', f'/data/{HF_REPO}', keep_count=3)
148
  if new_archive_info:
149
  new_archive, file_size_info = new_archive_info.split(' MB:')
150
  os.chdir(f'/data/{HF_REPO}')
 
153
  os.system(f'git commit -m "{file_size_info}"')
154
  # os.system('git push -u origin main')
155
  os.system('git push -f origin main')
156
+ os.system('git gc --prune=now')
157
  else:
158
  print("压缩失败,无法提交")
159
 
 
210
  result = restart_huggingface_space(HF_USER2, HF_ID, HF_TOKON2)
211
  print(result)
212
  # break
213
+ # github(2)
214
  # nv1_agent()