forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackupService.js
More file actions
295 lines (251 loc) · 8.58 KB
/
Copy pathbackupService.js
File metadata and controls
295 lines (251 loc) · 8.58 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const { pipeline } = require('stream/promises');
const DEFAULT_BACKUP_DIR = path.resolve(__dirname, '../../backups/base');
const DEFAULT_WAL_ARCHIVE_DIR = path.resolve(__dirname, '../../backups/wal');
const DEFAULT_RETENTION_DAYS = 14;
const DEFAULT_SCHEDULE_TIME = '02:00';
function parseBoolean(value, fallback = false) {
if (value == null) return fallback;
return String(value).toLowerCase() === 'true';
}
function resolveScheduleTime(value = DEFAULT_SCHEDULE_TIME) {
const match = /^(\d{2}):(\d{2})$/.exec(value);
if (!match) {
throw new Error('BACKUP_SCHEDULE_UTC must be in HH:MM format');
}
const hour = Number(match[1]);
const minute = Number(match[2]);
if (hour > 23 || minute > 59) {
throw new Error('BACKUP_SCHEDULE_UTC contains an invalid time');
}
return { hour, minute, value: `${match[1]}:${match[2]}` };
}
function getBackupConfig() {
const retentionDays = Number(process.env.BACKUP_RETENTION_DAYS || DEFAULT_RETENTION_DAYS);
if (!Number.isFinite(retentionDays) || retentionDays < 1) {
throw new Error('BACKUP_RETENTION_DAYS must be a positive integer');
}
return {
enabled: parseBoolean(process.env.BACKUP_ENABLED, false),
backupDir: path.resolve(process.env.BACKUP_DIR || DEFAULT_BACKUP_DIR),
walArchiveDir: path.resolve(process.env.BACKUP_WAL_ARCHIVE_DIR || DEFAULT_WAL_ARCHIVE_DIR),
passphrase: process.env.BACKUP_PASSPHRASE || '',
retentionDays,
schedule: resolveScheduleTime(process.env.BACKUP_SCHEDULE_UTC || DEFAULT_SCHEDULE_TIME),
databaseUrl: process.env.DATABASE_URL,
};
}
async function ensureDir(dirPath) {
await fs.promises.mkdir(dirPath, { recursive: true });
}
async function ensureBackupDirectories(config = getBackupConfig()) {
await Promise.all([
ensureDir(config.backupDir),
ensureDir(config.walArchiveDir),
]);
}
function backupIdFromDate(now = new Date()) {
return now.toISOString().replace(/[:.]/g, '-');
}
function runCommand(command, args, options = {}) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, options);
let stderr = '';
child.stderr?.on('data', (chunk) => {
stderr += chunk.toString();
});
child.on('error', reject);
child.on('close', (code) => {
if (code === 0) {
resolve({ stderr });
return;
}
reject(new Error(`${command} exited with code ${code}: ${stderr.trim()}`));
});
});
}
async function createEncryptedBackup({ now = new Date(), reason = 'scheduled' } = {}) {
const config = getBackupConfig();
if (!config.enabled) {
throw new Error('Backups are disabled');
}
if (!config.passphrase) {
throw new Error('BACKUP_PASSPHRASE is required when backups are enabled');
}
if (!config.databaseUrl) {
throw new Error('DATABASE_URL is required to create backups');
}
await ensureBackupDirectories(config);
const backupId = backupIdFromDate(now);
const fileName = `base-${backupId}.tar.gz.enc`;
const backupPath = path.join(config.backupDir, fileName);
const manifestPath = path.join(config.backupDir, `base-${backupId}.manifest.json`);
const tempPath = `${backupPath}.tmp`;
const baseBackup = spawn(
'pg_basebackup',
[
'--dbname',
config.databaseUrl,
'--pgdata',
'-',
'--format',
'tar',
'--gzip',
'--checkpoint=fast',
'--wal-method=none',
'--label',
`nova-rewards-${backupId}`,
],
{ stdio: ['ignore', 'pipe', 'pipe'] }
);
let baseBackupError = '';
baseBackup.stderr.on('data', (chunk) => {
baseBackupError += chunk.toString();
});
const openssl = spawn(
'openssl',
['enc', '-aes-256-cbc', '-pbkdf2', '-salt', '-pass', `pass:${config.passphrase}`, '-out', tempPath],
{ stdio: ['pipe', 'pipe', 'pipe'] }
);
let opensslError = '';
openssl.stderr.on('data', (chunk) => {
opensslError += chunk.toString();
});
await pipeline(baseBackup.stdout, openssl.stdin);
const [baseBackupResult, opensslResult] = await Promise.allSettled([
new Promise((resolve, reject) => {
baseBackup.on('error', reject);
baseBackup.on('close', (code) => {
if (code === 0) {
resolve();
return;
}
reject(new Error(`pg_basebackup exited with code ${code}: ${baseBackupError.trim()}`));
});
}),
new Promise((resolve, reject) => {
openssl.on('error', reject);
openssl.on('close', (code) => {
if (code === 0) {
resolve();
return;
}
reject(new Error(`openssl exited with code ${code}: ${opensslError.trim()}`));
});
}),
]);
if (baseBackupResult.status === 'rejected' || opensslResult.status === 'rejected') {
await fs.promises.rm(tempPath, { force: true });
throw baseBackupResult.reason || opensslResult.reason;
}
await fs.promises.rename(tempPath, backupPath);
const stats = await fs.promises.stat(backupPath);
const manifest = {
backupId,
createdAt: now.toISOString(),
reason,
fileName,
path: backupPath,
walArchiveDir: config.walArchiveDir,
sizeBytes: stats.size,
encryption: {
algorithm: 'aes-256-cbc',
kdf: 'pbkdf2',
},
retentionExpiresAt: new Date(
now.getTime() + config.retentionDays * 24 * 60 * 60 * 1000
).toISOString(),
};
await fs.promises.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
return manifest;
}
async function listBackups(config = getBackupConfig()) {
await ensureBackupDirectories(config);
const names = await fs.promises.readdir(config.backupDir);
const manifests = await Promise.all(
names
.filter((name) => name.endsWith('.manifest.json'))
.map(async (name) => {
const fullPath = path.join(config.backupDir, name);
const raw = await fs.promises.readFile(fullPath, 'utf8');
return JSON.parse(raw);
})
);
return manifests.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
}
async function pruneExpiredBackups(now = new Date(), config = getBackupConfig()) {
const backups = await listBackups(config);
const removed = [];
for (const backup of backups) {
if (new Date(backup.retentionExpiresAt) > now) continue;
await fs.promises.rm(backup.path, { force: true });
await fs.promises.rm(path.join(config.backupDir, `base-${backup.backupId}.manifest.json`), { force: true });
removed.push(backup.backupId);
}
return removed;
}
async function listArchivedWalSegments(config = getBackupConfig()) {
await ensureBackupDirectories(config);
const names = await fs.promises.readdir(config.walArchiveDir);
const metadataFiles = names.filter((name) => name.endsWith('.json'));
const entries = await Promise.all(metadataFiles.map(async (name) => {
const raw = await fs.promises.readFile(path.join(config.walArchiveDir, name), 'utf8');
return JSON.parse(raw);
}));
return entries.sort((a, b) => new Date(a.archivedAt) - new Date(b.archivedAt));
}
function selectBackupForTimestamp(backups, targetTime) {
const targetMs = new Date(targetTime).getTime();
if (Number.isNaN(targetMs)) {
throw new Error('targetTime must be a valid ISO-8601 timestamp');
}
const eligible = backups
.filter((backup) => new Date(backup.createdAt).getTime() <= targetMs)
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
return eligible[0] || null;
}
async function buildRecoveryPlan({ targetTime } = {}) {
if (!targetTime) {
throw new Error('targetTime is required to build a recovery plan');
}
const config = getBackupConfig();
const backups = await listBackups(config);
const backup = selectBackupForTimestamp(backups, targetTime);
if (!backup) {
throw new Error('No backup exists at or before the requested recovery point');
}
const walSegments = await listArchivedWalSegments(config);
const targetMs = new Date(targetTime).getTime();
const applicableWal = walSegments.filter(
(segment) => new Date(segment.archivedAt).getTime() <= targetMs
);
return {
targetTime,
backup,
walSegmentCount: applicableWal.length,
walSegments: applicableWal,
restoreCommand: [
'node',
'scripts/restore-pitr.js',
'--backup-manifest',
path.join(config.backupDir, `base-${backup.backupId}.manifest.json`),
'--target-time',
targetTime,
].join(' '),
};
}
module.exports = {
backupIdFromDate,
buildRecoveryPlan,
createEncryptedBackup,
ensureBackupDirectories,
getBackupConfig,
listArchivedWalSegments,
listBackups,
pruneExpiredBackups,
resolveScheduleTime,
selectBackupForTimestamp,
runCommand,
};