-
Notifications
You must be signed in to change notification settings - Fork 20
/
backup.go
241 lines (205 loc) · 6.77 KB
/
backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// DejaVu - Data snapshot and sync.
// Copyright (c) 2022-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package dejavu
import (
"github.com/siyuan-note/dejavu/cloud"
"os"
"path"
"strings"
"github.com/88250/gulu"
"github.com/siyuan-note/dejavu/entity"
"github.com/siyuan-note/logging"
)
func (repo *Repo) DownloadIndex(id string, context map[string]interface{}) (downloadFileCount, downloadChunkCount int, downloadBytes int64, err error) {
lock.Lock()
defer lock.Unlock()
downloadFileCount, downloadChunkCount, downloadBytes, err = repo.downloadIndex(id, context)
return
}
func (repo *Repo) DownloadTagIndex(tag, id string, context map[string]interface{}) (downloadFileCount, downloadChunkCount int, downloadBytes int64, err error) {
lock.Lock()
defer lock.Unlock()
downloadFileCount, downloadChunkCount, downloadBytes, err = repo.downloadIndex(id, context)
// 更新本地标签
err = repo.AddTag(id, tag)
if nil != err {
logging.LogErrorf("add tag failed: %s", err)
return
}
return
}
func (repo *Repo) downloadIndex(id string, context map[string]interface{}) (downloadFileCount, downloadChunkCount int, downloadBytes int64, err error) {
// 从云端下载标签指向的索引
length, index, err := repo.downloadCloudIndex(id, context)
if nil != err {
logging.LogErrorf("download cloud index failed: %s", err)
return
}
downloadFileCount++
downloadBytes += length
apiGet := 1
// 计算本地缺失的文件
fetchFileIDs, err := repo.localNotFoundFiles(index.Files)
if nil != err {
logging.LogErrorf("get local not found files failed: %s", err)
return
}
// 从云端下载缺失文件并入库
length, fetchedFiles, err := repo.downloadCloudFilesPut(fetchFileIDs, context)
if nil != err {
logging.LogErrorf("download cloud files put failed: %s", err)
return
}
downloadBytes += length
downloadFileCount = len(fetchFileIDs)
apiGet += downloadFileCount
// 从文件列表中得到去重后的分块列表
cloudChunkIDs := repo.getChunks(fetchedFiles)
// 计算本地缺失的分块
fetchChunkIDs, err := repo.localNotFoundChunks(cloudChunkIDs)
if nil != err {
logging.LogErrorf("get local not found chunks failed: %s", err)
return
}
// 从云端获取分块并入库
length, err = repo.downloadCloudChunksPut(fetchChunkIDs, context)
downloadBytes += length
downloadChunkCount = len(fetchChunkIDs)
apiGet += downloadChunkCount
// 更新本地索引
err = repo.store.PutIndex(index)
if nil != err {
logging.LogErrorf("put index failed: %s", err)
return
}
// 统计流量
go repo.cloud.AddTraffic(&cloud.Traffic{DownloadBytes: downloadBytes, APIGet: apiGet})
return
}
func (repo *Repo) UploadTagIndex(tag, id string, context map[string]interface{}) (uploadFileCount, uploadChunkCount int, uploadBytes int64, err error) {
lock.Lock()
defer lock.Unlock()
uploadFileCount, uploadChunkCount, uploadBytes, err = repo.uploadTagIndex(tag, id, context)
if e, ok := err.(*os.PathError); ok && os.IsNotExist(err) {
p := e.Path
if !strings.Contains(p, "objects") {
return
}
// 索引时正常,但是上传时可能因为外部变更导致对象(文件或者分块)不存在,此时需要告知用户数据仓库已经损坏,需要重置数据仓库
logging.LogErrorf("upload tag index failed: %s", err)
err = ErrRepoFatal
}
return
}
func (repo *Repo) uploadTagIndex(tag, id string, context map[string]interface{}) (uploadFileCount, uploadChunkCount int, uploadBytes int64, err error) {
index, err := repo.store.GetIndex(id)
if nil != err {
logging.LogErrorf("get index failed: %s", err)
return
}
availableSize := repo.cloud.GetAvailableSize()
if availableSize <= index.Size {
err = ErrCloudStorageSizeExceeded
return
}
// 获取云端数据仓库统计信息
cloudRepoSize, cloudBackupCount, err := repo.getCloudRepoStat()
if nil != err {
logging.LogErrorf("get cloud repo stat failed: %s", err)
return
}
if 12 <= cloudBackupCount {
err = ErrCloudBackupCountExceeded
return
}
if availableSize <= cloudRepoSize+index.Size {
err = ErrCloudStorageSizeExceeded
return
}
// 从云端获取文件列表
cloudFileIDs, refs, err := repo.cloud.GetRefsFiles()
if nil != err {
logging.LogErrorf("get cloud repo refs files failed: %s", err)
return
}
apiGet := len(refs) + 1
// 计算云端缺失的文件
var uploadFiles []*entity.File
for _, localFileID := range index.Files {
if !gulu.Str.Contains(localFileID, cloudFileIDs) {
var uploadFile *entity.File
uploadFile, err = repo.store.GetFile(localFileID)
if nil != err {
logging.LogErrorf("get file failed: %s", err)
return
}
uploadFiles = append(uploadFiles, uploadFile)
}
}
// 从文件列表中得到去重后的分块列表
uploadChunkIDs := repo.getChunks(uploadFiles)
// 计算云端缺失的分块
uploadChunkIDs, err = repo.cloud.GetChunks(uploadChunkIDs)
if nil != err {
logging.LogErrorf("get cloud repo upload chunks failed: %s", err)
return
}
apiGet += len(uploadChunkIDs)
// 上传分块
length, err := repo.uploadChunks(uploadChunkIDs, context)
if nil != err {
logging.LogErrorf("upload chunks failed: %s", err)
return
}
uploadChunkCount = len(uploadChunkIDs)
uploadBytes += length
apiPut := uploadChunkCount
// 上传文件
length, err = repo.uploadFiles(uploadFiles, context)
if nil != err {
logging.LogErrorf("upload files failed: %s", err)
return
}
uploadFileCount = len(uploadFiles)
uploadBytes += length
apiPut += uploadFileCount
// 上传索引
length, err = repo.uploadIndex(index, context)
uploadFileCount++
uploadBytes += length
apiPut++
// 上传标签
length, err = repo.updateCloudRef("refs/tags/"+tag, context)
uploadFileCount++
uploadBytes += length
apiPut++
// 统计流量
go repo.cloud.AddTraffic(&cloud.Traffic{UploadBytes: uploadBytes, APIGet: apiGet, APIPut: apiPut})
return
}
func (repo *Repo) getCloudRepoStat() (repoSize int64, backupCount int, err error) {
repoStat, err := repo.cloud.GetStat()
if nil != err {
return
}
repoSize = repoStat.Sync.Size + repoStat.Backup.Size
backupCount = repoStat.Backup.Count
return
}
func (repo *Repo) RemoveCloudRepoTag(tag string) (err error) {
key := path.Join("refs", "tags", tag)
return repo.cloud.RemoveObject(key)
}