cfiles commited on
Commit
a1940af
·
verified ·
1 Parent(s): 5775884

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +27 -16
sync_data.sh CHANGED
@@ -50,20 +50,28 @@ print(f'最新备份文件:{latest_backup}')
50
  download_path_in_webdav = backup_path_in_webdav + latest_backup
51
  backup_path = f'/tmp/{latest_backup}'
52
  try:
53
- client.download_file(download_path_in_webdav, backup_path)
54
- print(f'成功下载备份文件到 {backup_path}')
55
- if os.path.exists(backup_path):
56
- # 如果data目录已存在,先删除它
57
- if os.path.exists('$CLOUDREVE_DATA_DIR'):
58
- print('删除现有的data目录')
59
- shutil.rmtree('$CLOUDREVE_DATA_DIR')
60
- os.makedirs('$CLOUDREVE_DATA_DIR', exist_ok=True)
61
- # 解压备份文件
62
- with tarfile.open(backup_path, 'r:gz') as tar:
63
- tar.extractall('$CLOUDREVE_DATA_DIR')
64
- print(f'成功从 {latest_backup} 恢复备份')
65
- else:
66
- print('下载的备份文件不存在')
 
 
 
 
 
 
 
 
67
  except Exception as e:
68
  print(f'恢复备份失败:{e}')
69
  sys.exit(1)
@@ -117,8 +125,11 @@ backups.sort()
117
  if len(backups) > 5:
118
  to_delete = len(backups) - 5
119
  for file in backups[:to_delete]:
120
- client.remove(backup_path_in_webdav + file)
121
- print(f'Successfully deleted {file}.')
 
 
 
122
  else:
123
  print('Only {} backups found, no need to clean.'.format(len(backups)))
124
  " 2>&1
 
50
  download_path_in_webdav = backup_path_in_webdav + latest_backup
51
  backup_path = f'/tmp/{latest_backup}'
52
  try:
53
+ with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
54
+ if r.status_code == 200:
55
+ with open(backup_path, 'wb') as f:
56
+ for chunk in r.iter_content(chunk_size=8192):
57
+ f.write(chunk)
58
+ print(f'成功下载备份文件到 {backup_path}')
59
+ if os.path.exists(backup_path):
60
+ # 如果data目录已存在,先删除它
61
+ if os.path.exists('$CLOUDREVE_DATA_DIR'):
62
+ print('删除现有的data目录')
63
+ shutil.rmtree('$CLOUDREVE_DATA_DIR')
64
+ os.makedirs('$CLOUDREVE_DATA_DIR', exist_ok=True)
65
+
66
+ # 解压备份文件
67
+ with tarfile.open(backup_path, 'r:gz') as tar:
68
+ tar.extractall('$CLOUDREVE_DATA_DIR')
69
+
70
+ print(f'成功从 {latest_backup} 恢复备份')
71
+ else:
72
+ print('下载的备份文件不存在')
73
+ else:
74
+ print(f'下载备份失败:{r.status_code}')
75
  except Exception as e:
76
  print(f'恢复备份失败:{e}')
77
  sys.exit(1)
 
125
  if len(backups) > 5:
126
  to_delete = len(backups) - 5
127
  for file in backups[:to_delete]:
128
+ try:
129
+ client.remove(backup_path_in_webdav + file)
130
+ print(f'Successfully deleted {file}.')
131
+ except Exception as e:
132
+ print(f'删除 {file} 失败: {e}')
133
  else:
134
  print('Only {} backups found, no need to clean.'.format(len(backups)))
135
  " 2>&1