flzta commited on
Commit
73ce868
·
verified ·
1 Parent(s): 81c74d7

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +98 -103
sync_data.sh CHANGED
@@ -7,9 +7,8 @@ if [[ -z "$HF_TOKEN" ]] || [[ -z "$DATASET_ID" ]]; then
7
  exit 0
8
  fi
9
 
10
- # 定义 Cloudreve 数据目录和配置文件路径
11
- DATA_DIR="/opt/cloudreve/data"
12
- CONFIG_FILE="/opt/cloudreve/config.ini"
13
  BACKUP_PREFIX="cloudreve_backup"
14
 
15
  # 激活 Python 虚拟环境
@@ -44,106 +43,102 @@ def manage_backups(api, repo_id, max_files=5):
44
  except Exception as e:
45
  print(f'Error deleting {file_to_delete}: {str(e)}')
46
 
47
- api = HfApi(token='$token')
48
- try:
49
- api.upload_file(
50
- path_or_fileobj='$file_path',
51
- path_in_repo='$file_name',
52
- repo_id='$repo_id',
53
- repo_type='dataset'
54
- )
55
- print(f'Successfully uploaded $file_name')
56
-
57
- manage_backups(api, '$repo_id')
58
- except Exception as e:
59
- print(f'Error uploading file: {str(e)}')
60
- "
61
- }
62
-
63
- # Python 函数:下载最新备份
64
- download_latest_backup() {
65
- local token="$HF_TOKEN"
66
- local repo_id="$DATASET_ID"
67
-
68
- python3 -c "
69
- from huggingface_hub import HfApi
70
- import sys
71
- import os
72
- import tarfile
73
- import tempfile
74
- import glob
75
-
76
- api = HfApi(token='$token')
77
- try:
78
- files = api.list_repo_files(repo_id='$repo_id', repo_type='dataset')
79
- backup_files = [f for f in files if f.startswith('$BACKUP_PREFIX') and f.endswith('.tar.gz')]
80
-
81
- if not backup_files:
82
- print('No backup files found.')
83
- sys.exit()
84
-
85
- latest_backup = sorted(backup_files)[-1]
86
-
87
- with tempfile.TemporaryDirectory() as temp_dir:
88
- filepath = api.hf_hub_download(
89
  repo_id='$repo_id',
90
- filename=latest_backup,
91
- repo_type='dataset',
92
- local_dir=temp_dir
93
  )
94
-
95
- if filepath and os.path.exists(filepath):
96
- # 删除现有的数据目录和配置文件
97
- if [ -d \"$DATA_DIR\" ]; then
98
- echo \"Deleting existing data directory: $DATA_DIR\"
99
- rm -rf \"$DATA_DIR\"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  fi
101
- if [ -f \"$CONFIG_FILE\" ]; then
102
- echo \"Deleting existing config file: $CONFIG_FILE\"
103
- rm -rf \"$CONFIG_FILE\"
104
- fi
105
- mkdir -p \"$DATA_DIR\"
106
- with tarfile.open(filepath, 'r:gz') as tar:
107
- tar.extractall(\"/opt/cloudreve\") # 将备份恢复到 /opt/cloudreve 目录
108
- echo f'Successfully restored backup from {latest_backup}'
109
- except Exception as e:
110
- print(f'Error downloading backup: {str(e)}')
111
- "
112
- }
113
-
114
- # 首次启动时下载最新备份
115
- echo "Downloading latest backup from HuggingFace..."
116
- download_latest_backup
117
-
118
- # 后台启动同步进程
119
- sync_data() {
120
- while true; do
121
- echo "Starting sync process at $(date)"
122
-
123
- if [ -d "$DATA_DIR" ]; then
124
- timestamp=$(date +%Y%m%d_%H%M%S)
125
- backup_file="${BACKUP_PREFIX}_${timestamp}.tar.gz"
126
- backup_path="/tmp/${backup_file}"
127
-
128
- echo "Compressing data directory and config file..."
129
- tar -czf "$backup_path" -C /opt/cloudreve cloudreve config.ini
130
-
131
- echo "Uploading backup to HuggingFace..."
132
- upload_backup "$backup_path" "${backup_file}"
133
-
134
- rm -f "$backup_path"
135
- else
136
- echo "Data directory does not exist yet, waiting for next sync..."
137
- fi
138
-
139
- SYNC_INTERVAL=${SYNC_INTERVAL:-3600} # 默认同步间隔为 1 小时
140
- echo "Next sync in ${SYNC_INTERVAL} seconds..."
141
- sleep $SYNC_INTERVAL
142
- done
143
- }
144
-
145
- sync_data &
146
-
147
- # 启动 Cloudreve
148
- echo "Starting Cloudreve..."
149
- exec /opt/cloudreve/cloudreve -c /opt/cloudreve/config.ini
 
7
  exit 0
8
  fi
9
 
10
+ # 定义 Cloudreve 主程序目录
11
+ CLOUDREVE_DIR="/opt/cloudreve"
 
12
  BACKUP_PREFIX="cloudreve_backup"
13
 
14
  # 激活 Python 虚拟环境
 
43
  except Exception as e:
44
  print(f'Error deleting {file_to_delete}: {str(e)}')
45
 
46
+ api = HfApi(token='$token')
47
+ try:
48
+ api.upload_file(
49
+ path_or_fileobj='$file_path',
50
+ path_in_repo='$file_name',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  repo_id='$repo_id',
52
+ repo_type='dataset'
 
 
53
  )
54
+ print(f'Successfully uploaded $file_name')
55
+
56
+ manage_backups(api, '$repo_id')
57
+ except Exception as e:
58
+ print(f'Error uploading file: {str(e)}')
59
+ "
60
+ }
61
+
62
+ # Python 函数:下载最新备份
63
+ download_latest_backup() {
64
+ local token="$HF_TOKEN"
65
+ local repo_id="$DATASET_ID"
66
+
67
+ python3 -c "
68
+ from huggingface_hub import HfApi
69
+ import sys
70
+ import os
71
+ import tarfile
72
+ import tempfile
73
+ import glob
74
+
75
+ api = HfApi(token='$token')
76
+ try:
77
+ files = api.list_repo_files(repo_id='$repo_id', repo_type='dataset')
78
+ backup_files = [f for f in files if f.startswith('$BACKUP_PREFIX') and f.endswith('.tar.gz')]
79
+
80
+ if not backup_files:
81
+ print('No backup files found.')
82
+ sys.exit()
83
+
84
+ latest_backup = sorted(backup_files)[-1]
85
+
86
+ with tempfile.TemporaryDirectory() as temp_dir:
87
+ filepath = api.hf_hub_download(
88
+ repo_id='$repo_id',
89
+ filename=latest_backup,
90
+ repo_type='dataset',
91
+ local_dir=temp_dir
92
+ )
93
+
94
+ if filepath and os.path.exists(filepath):
95
+ # 删除现有的 Cloudreve 目录和配置文件 (除了 data 目录,如果 data 目录在主目录下)
96
+ cd \"$CLOUDREVE_DIR\"
97
+ if [ -f cloudreve ] ; then rm -f cloudreve; fi
98
+ if [ -f cloudreve.db ] ; then rm -f cloudreve.db; fi
99
+ if [ -f config.ini ] ; then rm -f config.ini; fi
100
+
101
+ with tarfile.open(filepath, 'r:gz') as tar:
102
+ tar.extractall(\"$CLOUDREVE_DIR\")
103
+ echo f'Successfully restored backup from {latest_backup}'
104
+ except Exception as e:
105
+ print(f'Error downloading backup: {str(e)}')
106
+ "
107
+ }
108
+
109
+ # 首次启动时下载最新备份
110
+ echo "Downloading latest backup from HuggingFace..."
111
+ download_latest_backup
112
+
113
+ # 后台启动同步进程
114
+ sync_data() {
115
+ while true; do
116
+ echo "Starting sync process at $(date)"
117
+
118
+ if [ -d "$CLOUDREVE_DIR" ]; then
119
+ timestamp=$(date +%Y%m%d_%H%M%S)
120
+ backup_file="${BACKUP_PREFIX}_${timestamp}.tar.gz"
121
+ backup_path="/tmp/${backup_file}"
122
+
123
+ echo "Compressing Cloudreve directory (including database and config)..."
124
+ tar -czf "$backup_path" -C "$CLOUDREVE_DIR" cloudreve cloudreve.db config.ini
125
+
126
+ echo "Uploading backup to HuggingFace..."
127
+ upload_backup "$backup_path" "${backup_file}"
128
+
129
+ rm -f "$backup_path"
130
+ else
131
+ echo "Cloudreve directory does not exist yet, waiting for next sync..."
132
  fi
133
+
134
+ SYNC_INTERVAL=${SYNC_INTERVAL:-3600} # 默认同步间隔为 1 小时
135
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
136
+ sleep $SYNC_INTERVAL
137
+ done
138
+ }
139
+
140
+ sync_data &
141
+
142
+ # 启动 Cloudreve
143
+ echo "Starting Cloudreve..."
144
+ exec /opt/cloudreve/cloudreve -c /opt/cloudreve/config.ini