nbugs commited on
Commit
fb29999
·
verified ·
1 Parent(s): c9a47ab

Create sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +112 -0
sync_data.sh ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 /start.sh
7
+ exit 0
8
+ fi
9
+
10
+ # 激活虚拟环境
11
+ source /opt/venv/bin/activate
12
+
13
+ # 上传备份
14
+ upload_backup() {
15
+ file_path="$1"
16
+ file_name="$2"
17
+ token="$HF_TOKEN"
18
+ repo_id="$DATASET_ID"
19
+
20
+ python3 -c "
21
+ from huggingface_hub import HfApi
22
+ import sys
23
+ import os
24
+ api = HfApi(token='$token')
25
+ try:
26
+ api.upload_file(
27
+ path_or_fileobj='$file_path',
28
+ path_in_repo='$file_name',
29
+ repo_id='$repo_id',
30
+ repo_type='dataset'
31
+ )
32
+ print(f'Successfully uploaded $file_name')
33
+ except Exception as e:
34
+ print(f'Error uploading file: {str(e)}')
35
+ "
36
+ }
37
+
38
+ # 下载最新备份
39
+ download_latest_backup() {
40
+ token="$HF_TOKEN"
41
+ repo_id="$DATASET_ID"
42
+
43
+ python3 -c "
44
+ from huggingface_hub import HfApi
45
+ import sys
46
+ import os
47
+ import tarfile
48
+ import tempfile
49
+ api = HfApi(token='$token')
50
+ try:
51
+ files = api.list_repo_files(repo_id='$repo_id', repo_type='dataset')
52
+ backup_files = [f for f in files if f.startswith('vaultwarden_backup_') and f.endswith('.tar.gz')]
53
+
54
+ if not backup_files:
55
+ print('No backup files found')
56
+ sys.exit()
57
+
58
+ latest_backup = sorted(backup_files)[-1]
59
+
60
+ with tempfile.TemporaryDirectory() as temp_dir:
61
+ filepath = api.hf_hub_download(
62
+ repo_id='$repo_id',
63
+ filename=latest_backup,
64
+ repo_type='dataset',
65
+ local_dir=temp_dir
66
+ )
67
+
68
+ if filepath and os.path.exists(filepath):
69
+ with tarfile.open(filepath, 'r:gz') as tar:
70
+ tar.extractall('/data')
71
+ print(f'Successfully restored backup from {latest_backup}')
72
+
73
+ except Exception as e:
74
+ print(f'Error downloading backup: {str(e)}')
75
+ "
76
+ }
77
+
78
+ # 首次启动时下载最新备份
79
+ echo "Downloading latest backup from HuggingFace..."
80
+ download_latest_backup
81
+
82
+ # 同步函数
83
+ sync_data() {
84
+ while true; do
85
+ echo "Starting sync process at $(date)"
86
+
87
+ if [ -d /data ]; then
88
+ timestamp=$(date +%Y%m%d_%H%M%S)
89
+ backup_file="vaultwarden_backup_${timestamp}.tar.gz"
90
+
91
+ # 压缩数据目录
92
+ tar -czf "/tmp/${backup_file}" -C /data .
93
+
94
+ echo "Uploading backup to HuggingFace..."
95
+ upload_backup "/tmp/${backup_file}" "${backup_file}"
96
+
97
+ rm -f "/tmp/${backup_file}"
98
+ else
99
+ echo "Data directory does not exist yet, waiting for next sync..."
100
+ fi
101
+
102
+ SYNC_INTERVAL=${SYNC_INTERVAL:-7200}
103
+ echo "Next sync in ${SYNC_INTERVAL} seconds..."
104
+ sleep $SYNC_INTERVAL
105
+ done
106
+ }
107
+
108
+ # 后台启动同步进程
109
+ sync_data &
110
+
111
+ # 启动 Vaultwarden
112
+ exec /start.sh