tero 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +321 -0
- package/dist/acid-engine.js +628 -0
- package/dist/backup.js +411 -0
- package/dist/fn.js +192 -0
- package/dist/index.js +450 -0
- package/dist/recovery.js +317 -0
- package/dist/schema.js +425 -0
- package/dist/transaction.js +476 -0
- package/package.json +58 -0
package/dist/recovery.js
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { S3Client, GetObjectCommand, ListObjectsV2Command, HeadObjectCommand } from "@aws-sdk/client-s3";
|
|
2
|
+
import { createWriteStream, existsSync, mkdirSync } from "fs";
|
|
3
|
+
import { join, dirname } from "path";
|
|
4
|
+
import { pipeline } from "stream/promises";
|
|
5
|
+
export class DataRecovery {
|
|
6
|
+
s3Client;
|
|
7
|
+
config;
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.config = config;
|
|
10
|
+
this.initializeCloudStorage();
|
|
11
|
+
}
|
|
12
|
+
initializeCloudStorage() {
|
|
13
|
+
try {
|
|
14
|
+
const clientConfig = {
|
|
15
|
+
region: this.config.cloudStorage.region,
|
|
16
|
+
credentials: {
|
|
17
|
+
accessKeyId: this.config.cloudStorage.accessKeyId,
|
|
18
|
+
secretAccessKey: this.config.cloudStorage.secretAccessKey,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
// Configure for Cloudflare R2 or custom endpoints
|
|
22
|
+
if (this.config.cloudStorage.endpoint) {
|
|
23
|
+
clientConfig.endpoint = this.config.cloudStorage.endpoint;
|
|
24
|
+
clientConfig.forcePathStyle = true; // Required for R2
|
|
25
|
+
}
|
|
26
|
+
this.s3Client = new S3Client(clientConfig);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
throw new Error(`Failed to initialize cloud storage for recovery: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
getCloudKey(filename) {
|
|
33
|
+
const prefix = this.config.cloudStorage.pathPrefix || 'tero-backups';
|
|
34
|
+
const dbName = this.config.localPath.split('/').pop() || 'database';
|
|
35
|
+
return `${prefix}/${dbName}/${filename}`;
|
|
36
|
+
}
|
|
37
|
+
async checkFileInCloud(key) {
|
|
38
|
+
try {
|
|
39
|
+
const cloudKey = this.getCloudKey(`${key}.json`);
|
|
40
|
+
const headCommand = new HeadObjectCommand({
|
|
41
|
+
Bucket: this.config.cloudStorage.bucket,
|
|
42
|
+
Key: cloudKey
|
|
43
|
+
});
|
|
44
|
+
const response = await this.s3Client.send(headCommand);
|
|
45
|
+
return {
|
|
46
|
+
key,
|
|
47
|
+
exists: true,
|
|
48
|
+
size: response.ContentLength,
|
|
49
|
+
lastModified: response.LastModified
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error.name === 'NotFound' || error.$metadata?.httpStatusCode === 404) {
|
|
54
|
+
return {
|
|
55
|
+
key,
|
|
56
|
+
exists: false
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`Failed to check file in cloud: ${error.message}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async recoverSingleFile(key) {
|
|
63
|
+
try {
|
|
64
|
+
const cloudKey = this.getCloudKey(`${key}.json`);
|
|
65
|
+
const localFilePath = join(this.config.localPath, `${key}.json`);
|
|
66
|
+
// Ensure local directory exists
|
|
67
|
+
const localDir = dirname(localFilePath);
|
|
68
|
+
if (!existsSync(localDir)) {
|
|
69
|
+
mkdirSync(localDir, { recursive: true });
|
|
70
|
+
}
|
|
71
|
+
const getCommand = new GetObjectCommand({
|
|
72
|
+
Bucket: this.config.cloudStorage.bucket,
|
|
73
|
+
Key: cloudKey
|
|
74
|
+
});
|
|
75
|
+
const response = await this.s3Client.send(getCommand);
|
|
76
|
+
if (!response.Body) {
|
|
77
|
+
throw new Error('No data received from cloud storage');
|
|
78
|
+
}
|
|
79
|
+
// Stream the file to local storage
|
|
80
|
+
const writeStream = createWriteStream(localFilePath);
|
|
81
|
+
await pipeline(response.Body, writeStream);
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (error.name === 'NoSuchKey' || error.$metadata?.httpStatusCode === 404) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async recoverFromArchive(archiveName) {
|
|
92
|
+
const startTime = Date.now();
|
|
93
|
+
const recovered = [];
|
|
94
|
+
const failed = [];
|
|
95
|
+
try {
|
|
96
|
+
// List available archives if no specific archive is provided
|
|
97
|
+
if (!archiveName) {
|
|
98
|
+
const archives = await this.listAvailableArchives();
|
|
99
|
+
if (archives.length === 0) {
|
|
100
|
+
throw new Error('No backup archives found in cloud storage');
|
|
101
|
+
}
|
|
102
|
+
// Use the most recent archive
|
|
103
|
+
archiveName = archives[0];
|
|
104
|
+
}
|
|
105
|
+
const cloudKey = this.getCloudKey(archiveName);
|
|
106
|
+
const localArchivePath = join(this.config.localPath, archiveName);
|
|
107
|
+
// Download the archive
|
|
108
|
+
const downloadSuccess = await this.downloadFile(cloudKey, localArchivePath);
|
|
109
|
+
if (!downloadSuccess) {
|
|
110
|
+
throw new Error(`Failed to download archive: ${archiveName}`);
|
|
111
|
+
}
|
|
112
|
+
// Extract the archive
|
|
113
|
+
const extractSuccess = await this.extractArchive(localArchivePath);
|
|
114
|
+
if (extractSuccess) {
|
|
115
|
+
recovered.push(archiveName);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
failed.push(archiveName);
|
|
119
|
+
}
|
|
120
|
+
const duration = Date.now() - startTime;
|
|
121
|
+
return {
|
|
122
|
+
success: recovered.length > 0,
|
|
123
|
+
recovered,
|
|
124
|
+
failed,
|
|
125
|
+
totalFiles: 1,
|
|
126
|
+
duration
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
const duration = Date.now() - startTime;
|
|
131
|
+
return {
|
|
132
|
+
success: false,
|
|
133
|
+
recovered,
|
|
134
|
+
failed: [archiveName || 'unknown'],
|
|
135
|
+
totalFiles: 1,
|
|
136
|
+
duration
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async recoverIndividualFiles(keys) {
|
|
141
|
+
const startTime = Date.now();
|
|
142
|
+
const recovered = [];
|
|
143
|
+
const failed = [];
|
|
144
|
+
try {
|
|
145
|
+
// If no keys provided, list all available files
|
|
146
|
+
if (!keys || keys.length === 0) {
|
|
147
|
+
keys = await this.listAvailableFiles();
|
|
148
|
+
if (keys.length === 0) {
|
|
149
|
+
throw new Error('No backup files found in cloud storage');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// Recover each file
|
|
153
|
+
for (const key of keys) {
|
|
154
|
+
const success = await this.recoverSingleFile(key);
|
|
155
|
+
if (success) {
|
|
156
|
+
recovered.push(key);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
failed.push(key);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const duration = Date.now() - startTime;
|
|
163
|
+
return {
|
|
164
|
+
success: recovered.length > 0,
|
|
165
|
+
recovered,
|
|
166
|
+
failed,
|
|
167
|
+
totalFiles: keys.length,
|
|
168
|
+
duration
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
const duration = Date.now() - startTime;
|
|
173
|
+
return {
|
|
174
|
+
success: false,
|
|
175
|
+
recovered,
|
|
176
|
+
failed: keys || [],
|
|
177
|
+
totalFiles: keys?.length || 0,
|
|
178
|
+
duration
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async listAvailableArchives() {
|
|
183
|
+
try {
|
|
184
|
+
const prefix = this.getCloudKey('');
|
|
185
|
+
const listCommand = new ListObjectsV2Command({
|
|
186
|
+
Bucket: this.config.cloudStorage.bucket,
|
|
187
|
+
Prefix: prefix,
|
|
188
|
+
MaxKeys: 100
|
|
189
|
+
});
|
|
190
|
+
const response = await this.s3Client.send(listCommand);
|
|
191
|
+
if (!response.Contents) {
|
|
192
|
+
return [];
|
|
193
|
+
}
|
|
194
|
+
// Filter for archive files and sort by date (newest first)
|
|
195
|
+
const archives = response.Contents
|
|
196
|
+
.filter((obj) => obj.Key && obj.Key.endsWith('.tar.gz'))
|
|
197
|
+
.sort((a, b) => {
|
|
198
|
+
const dateA = a.LastModified?.getTime() || 0;
|
|
199
|
+
const dateB = b.LastModified?.getTime() || 0;
|
|
200
|
+
return dateB - dateA; // Newest first
|
|
201
|
+
})
|
|
202
|
+
.map((obj) => obj.Key.split('/').pop())
|
|
203
|
+
.filter(Boolean);
|
|
204
|
+
return archives;
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
return [];
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async listAvailableFiles() {
|
|
211
|
+
try {
|
|
212
|
+
const prefix = this.getCloudKey('');
|
|
213
|
+
const listCommand = new ListObjectsV2Command({
|
|
214
|
+
Bucket: this.config.cloudStorage.bucket,
|
|
215
|
+
Prefix: prefix,
|
|
216
|
+
MaxKeys: 1000
|
|
217
|
+
});
|
|
218
|
+
const response = await this.s3Client.send(listCommand);
|
|
219
|
+
if (!response.Contents) {
|
|
220
|
+
return [];
|
|
221
|
+
}
|
|
222
|
+
// Filter for JSON files and extract keys
|
|
223
|
+
const files = response.Contents
|
|
224
|
+
.filter((obj) => obj.Key && obj.Key.endsWith('.json'))
|
|
225
|
+
.map((obj) => {
|
|
226
|
+
const filename = obj.Key.split('/').pop();
|
|
227
|
+
return filename.replace('.json', '');
|
|
228
|
+
})
|
|
229
|
+
.filter(Boolean);
|
|
230
|
+
return files;
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
async downloadFile(cloudKey, localPath) {
|
|
237
|
+
try {
|
|
238
|
+
const getCommand = new GetObjectCommand({
|
|
239
|
+
Bucket: this.config.cloudStorage.bucket,
|
|
240
|
+
Key: cloudKey
|
|
241
|
+
});
|
|
242
|
+
const response = await this.s3Client.send(getCommand);
|
|
243
|
+
if (!response.Body) {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
// Ensure local directory exists
|
|
247
|
+
const localDir = dirname(localPath);
|
|
248
|
+
if (!existsSync(localDir)) {
|
|
249
|
+
mkdirSync(localDir, { recursive: true });
|
|
250
|
+
}
|
|
251
|
+
const writeStream = createWriteStream(localPath);
|
|
252
|
+
await pipeline(response.Body, writeStream);
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async extractArchive(archivePath) {
|
|
260
|
+
try {
|
|
261
|
+
const tar = await import('tar');
|
|
262
|
+
await tar.extract({
|
|
263
|
+
file: archivePath,
|
|
264
|
+
cwd: this.config.localPath,
|
|
265
|
+
strip: 1 // Remove the top-level directory from the archive
|
|
266
|
+
});
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
async testCloudConnection() {
|
|
274
|
+
try {
|
|
275
|
+
const listCommand = new ListObjectsV2Command({
|
|
276
|
+
Bucket: this.config.cloudStorage.bucket,
|
|
277
|
+
MaxKeys: 1
|
|
278
|
+
});
|
|
279
|
+
await this.s3Client.send(listCommand);
|
|
280
|
+
return { success: true, message: 'Cloud storage connection successful' };
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
return {
|
|
284
|
+
success: false,
|
|
285
|
+
message: `Cloud storage connection failed: ${error instanceof Error ? error.message : 'Unknown error'}`
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
async getRecoveryInfo() {
|
|
290
|
+
try {
|
|
291
|
+
const cloudFiles = await this.listAvailableFiles();
|
|
292
|
+
const localFiles = [];
|
|
293
|
+
// Check which files exist locally
|
|
294
|
+
for (const key of cloudFiles) {
|
|
295
|
+
const localPath = join(this.config.localPath, `${key}.json`);
|
|
296
|
+
if (existsSync(localPath)) {
|
|
297
|
+
localFiles.push(key);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const missingLocally = cloudFiles.filter(key => !localFiles.includes(key));
|
|
301
|
+
return {
|
|
302
|
+
cloudFiles: cloudFiles.length,
|
|
303
|
+
localFiles: localFiles.length,
|
|
304
|
+
missingLocally,
|
|
305
|
+
availableForRecovery: cloudFiles
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
return {
|
|
310
|
+
cloudFiles: 0,
|
|
311
|
+
localFiles: 0,
|
|
312
|
+
missingLocally: [],
|
|
313
|
+
availableForRecovery: []
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|