flzta commited on
Commit
0033348
·
verified ·
1 Parent(s): 6139425

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +63 -35
sync_data.sh CHANGED
@@ -13,6 +13,39 @@ source /opt/venv/bin/activate
13
  # 定义 Cloudreve 主程序目录
14
  CLOUDREVE_DIR="/opt/cloudreve"
15
  BACKUP_PREFIX="cloudreve_backup"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # Python 函数: 上传备份
18
  upload_backup() {
@@ -109,41 +142,25 @@ try:
109
 
110
  if filepath and os.path.exists(filepath):
111
  print(f'Successfully downloaded backup to temporary directory: {filepath}')
112
- print(\"Before restoring backup:\")
113
- import subprocess
114
- subprocess.run(['ls', '-l', \"$CLOUDREVE_DIR\"], shell=True, check=False)
115
 
116
  # 删除现有的 Cloudreve 目录和配置文件
117
- import shutil
118
- cloudreve_path = os.path.join(\"$CLOUDREVE_DIR\", \"cloudreve\")
119
- cloudreve_db_path = os.path.join(\"$CLOUDREVE_DIR\", \"cloudreve.db\")
120
- config_ini_path = os.path.join(\"$CLOUDREVE_DIR\", \"config.ini\")
121
- data_path = os.path.join(\"$CLOUDREVE_DIR\", \"data\")
122
-
123
- if os.path.exists(cloudreve_path):
124
- print(f'Deleting: {cloudreve_path}')
125
- shutil.rmtree(cloudreve_path, ignore_errors=True)
126
- if os.path.exists(cloudreve_db_path):
127
- print(f'Deleting: {cloudreve_db_path}')
128
- os.remove(cloudreve_db_path)
129
- if os.path.exists(config_ini_path):
130
- print(f'Deleting: {config_ini_path}')
131
- os.remove(config_ini_path)
132
- if os.path.exists(data_path):
133
- print(f'Deleting: {data_path}')
134
- shutil.rmtree(data_path, ignore_errors=True)
135
- print(\"Deletion complete.\")
136
-
137
- print(f'Extracting backup archive: {filepath} to $CLOUDREVE_DIR')
138
- import tarfile
139
- with tarfile.open(filepath, 'r:gz') as tar:
140
- tar.extractall(\"$CLOUDREVE_DIR\")
141
- print(f'Successfully restored backup from {latest_backup}')
142
-
143
- print(\"After restoring backup:\")
144
- subprocess.run(['ls', '-l', \"$CLOUDREVE_DIR\"], shell=True, check=False)
145
  else:
146
- print('Error during file download.')
147
 
148
  except Exception as e:
149
  print(f'Error downloading backup: {str(e)}')
@@ -156,10 +173,21 @@ download_latest_backup
156
 
157
  # 同步函数
158
  sync_data() {
159
- echo "SYNC_DATA FUNCTION IS RUNNING" # 添加了这一行
160
  while true; do
161
  echo "Starting sync process at $(date)"
162
 
 
 
 
 
 
 
 
 
 
 
 
163
  if [ -d "$CLOUDREVE_DIR" ]; then
164
  echo "Before compression:"
165
  ls -l \"$CLOUDREVE_DIR\"
@@ -183,14 +211,14 @@ sync_data() {
183
  echo "Cloudreve directory does not exist yet, waiting for next sync..."
184
  fi
185
 
186
- SYNC_INTERVAL=${SYNC_INTERVAL:-60} # 默认同步间隔改为 60
187
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
188
  sleep $SYNC_INTERVAL
189
  done
190
  }
191
 
192
  # 延迟启动同步脚本,给 Cloudreve 一些启动时间
193
- sleep 10
194
 
195
  # 后台启动同步进程
196
  sync_data &
 
13
  # 定义 Cloudreve 主程序目录
14
  CLOUDREVE_DIR="/opt/cloudreve"
15
  BACKUP_PREFIX="cloudreve_backup"
16
+ HF_DATA_DIR="/opt/cloudreve/hf_uploaded" # 用于记录已上传到 HF 的文件
17
+
18
+ # 创建 HF 上传记录目录
19
+ mkdir -p "$HF_DATA_DIR"
20
+
21
+ # Python 函数: 上传文件到 Hugging Face Dataset
22
+ upload_file_to_dataset() {
23
+ local_file_path="$1"
24
+ relative_path=$(echo "$local_file_path" | sed "s|$CLOUDREVE_DIR/data/||")
25
+ token="$HF_TOKEN"
26
+ repo_id="$DATASET_ID"
27
+
28
+ echo "Preparing to upload file: $local_file_path to Dataset: $repo_id at path: $relative_path"
29
+
30
+ python3 -c "
31
+ from huggingface_hub import HfApi
32
+ import os
33
+
34
+ api = HfApi(token='$token')
35
+ try:
36
+ repo_id_val = os.environ.get('DATASET_ID')
37
+ print(f'Uploading file: '$local_file_path' to {repo_id_val} as '$relative_path'')
38
+ api.upload_file(
39
+ path_or_fileobj='$local_file_path',
40
+ path_in_repo='$relative_path',
41
+ repo_id=repo_id_val,
42
+ repo_type='dataset'
43
+ )
44
+ print(f'Successfully uploaded '$relative_path'')
45
+ except Exception as e:
46
+ print(f'Error uploading file: {str(e)}')
47
+ "
48
+ }
49
 
50
  # Python 函数: 上传备份
51
  upload_backup() {
 
142
 
143
  if filepath and os.path.exists(filepath):
144
  print(f'Successfully downloaded backup to temporary directory: {filepath}')
145
+ echo \"Before restoring backup:\"
146
+ ls -l \"$CLOUDREVE_DIR\"
 
147
 
148
  # 删除现有的 Cloudreve 目录和配置文件
149
+ rm -rf \"$CLOUDREVE_DIR/cloudreve\"
150
+ rm -f \"$CLOUDREVE_DIR/cloudreve.db\"
151
+ rm -f \"$CLOUDREVE_DIR/config.ini\"
152
+ rm -rf \"$CLOUDREVE_DIR/data\" # 也删除数据目录
153
+
154
+ echo \"Deletion complete.\"
155
+
156
+ echo \"Extracting backup archive: $filepath to $CLOUDREVE_DIR\"
157
+ tar -xzf \"$filepath\" -C \"$CLOUDREVE_DIR\"
158
+ echo \"Successfully restored backup from $latest_backup\"
159
+
160
+ echo \"After restoring backup:\"
161
+ ls -l \"$CLOUDREVE_DIR\"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  else:
163
+ echo 'Error during file download.'
164
 
165
  except Exception as e:
166
  print(f'Error downloading backup: {str(e)}')
 
173
 
174
  # 同步函数
175
  sync_data() {
176
+ echo "SYNC_DATA FUNCTION IS RUNNING"
177
  while true; do
178
  echo "Starting sync process at $(date)"
179
 
180
+ # 检查 Cloudreve data 目录并上传新文件
181
+ if [ -d "$CLOUDREVE_DIR/data" ]; then
182
+ find "$CLOUDREVE_DIR/data" -type f -print0 | while IFS= read -r -d $'\0' file; do
183
+ if [ ! -f "$HF_DATA_DIR/$(basename "$file")" ]; then
184
+ echo "New file found: $file"
185
+ upload_file_to_dataset "$file"
186
+ touch "$HF_DATA_DIR/$(basename "$file")" # 创建已上传标记
187
+ fi
188
+ done
189
+ fi
190
+
191
  if [ -d "$CLOUDREVE_DIR" ]; then
192
  echo "Before compression:"
193
  ls -l \"$CLOUDREVE_DIR\"
 
211
  echo "Cloudreve directory does not exist yet, waiting for next sync..."
212
  fi
213
 
214
+ SYNC_INTERVAL=${SYNC_INTERVAL:-300} # 默认同步间隔改为 5 分钟
215
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
216
  sleep $SYNC_INTERVAL
217
  done
218
  }
219
 
220
  # 延迟启动同步脚本,给 Cloudreve 一些启动时间
221
+ sleep 30
222
 
223
  # 后台启动同步进程
224
  sync_data &