-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.js
More file actions
390 lines (336 loc) · 12.5 KB
/
Copy pathbackup.js
File metadata and controls
390 lines (336 loc) · 12.5 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env node
/**
* Scheduler DB Backup -- Ship SQLite snapshots to MinIO
*
* Modes:
* snapshot -- Full DB copy to MinIO (5-min granularity)
* rollup -- Tagged hourly snapshot + prune old 5-min snapshots
* restore -- Pull latest snapshot from MinIO and restore
* status -- Show backup status (latest snapshot, count, size)
* prune -- Remove snapshots older than retention policy
*
* Storage layout on MinIO:
* scheduler-backups/scheduler/snapshots/YYYY-MM-DD/HH-MM.db
* scheduler-backups/scheduler/rollups/YYYY-MM-DD/HH.db
*
* Retention:
* snapshots: 24 hours (288 files max at 5-min intervals)
* rollups: 7 days (168 files max)
*
* Requires: MinIO client (`mc`) binary in PATH or ~/bin/mc.
* Install: https://min.io/docs/minio/linux/reference/minio-mc.html
*
* Usage:
* node backup.js snapshot # Ship current DB
* node backup.js rollup # Hourly rollup + prune old snapshots
* node backup.js restore # Restore from latest
* node backup.js status # Show backup stats
*/
import Database from 'better-sqlite3';
import { execFileSync } from 'child_process';
import { copyFileSync, existsSync, mkdirSync, statSync, unlinkSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
import { resolveBackupStagingDir, resolveSchedulerDbPath } from './paths.js';
const DB_PATH = resolveSchedulerDbPath({ env: process.env });
const STAGING_DIR = resolveBackupStagingDir(process.env);
const MC_ALIAS = process.env.SCHEDULER_BACKUP_MC_ALIAS || 'backupstore';
const BUCKET = process.env.SCHEDULER_BACKUP_BUCKET || 'scheduler-backups';
const PREFIX = process.env.SCHEDULER_BACKUP_PREFIX || 'scheduler';
// Find mc binary -- may be in ~/bin on some hosts
const MC_BIN = existsSync(join(homedir(), 'bin', 'mc'))
? join(homedir(), 'bin', 'mc')
: 'mc';
// Retention
const SNAPSHOT_RETENTION_HOURS = 24;
const ROLLUP_RETENTION_DAYS = 7;
const LOG_PREFIX = '[backup]';
function log(level, msg) {
const ts = new Date().toISOString();
process.stderr.write(`${ts} ${LOG_PREFIX} [${level}] ${msg}\n`);
}
function now() {
return new Date();
}
function mcPath(subpath) {
return `${MC_ALIAS}/${BUCKET}/${PREFIX}/${subpath}`;
}
/**
* Create a consistent backup of the SQLite database. Checkpoints WAL first
* via better-sqlite3 (no external sqlite3 binary needed), then copies the
* main DB file. This is safe for WAL-mode databases.
* Returns true on success, false on failure.
*/
function backupDatabase(srcPath, destPath) {
try {
const src = new Database(srcPath, { readonly: false, fileMustExist: true });
try {
src.pragma('wal_checkpoint(TRUNCATE)');
} finally {
src.close();
}
copyFileSync(srcPath, destPath);
return true;
} catch (err) {
log('warn', `Database backup failed: ${err.message}`);
return false;
}
}
function hasSqlite3() {
try {
execFileSync('sqlite3', ['--version'], { stdio: 'pipe' });
return true;
} catch {
return false;
}
}
function runFile(bin, args, opts = {}) {
try {
return execFileSync(bin, args, { encoding: 'utf8', timeout: 30000, stdio: 'pipe', ...opts }).trim();
} catch (err) {
if (!opts.ignoreError) {
log('error', `Command failed: ${bin} ${args.join(' ')}\n${err.stderr || err.message}`);
}
return null;
}
}
function runSqlite3(dbPath, sqlCmd, opts = {}) {
return runFile('sqlite3', [dbPath, sqlCmd], opts);
}
function runMc(args, opts = {}) {
return runFile(MC_BIN, args, opts);
}
// -- Snapshot (5-min) ----------------------------------------
function snapshot() {
if (!existsSync(DB_PATH)) {
log('error', `DB not found: ${DB_PATH}`);
process.exit(1);
}
// Stage: checkpoint WAL then copy
mkdirSync(STAGING_DIR, { recursive: true });
const stagingFile = join(STAGING_DIR, 'scheduler-snapshot.db');
// Checkpoint WAL and copy the database file
if (!backupDatabase(DB_PATH, stagingFile)) {
log('warn', 'Structured backup failed, falling back to direct file copy');
copyFileSync(DB_PATH, stagingFile);
}
const size = statSync(stagingFile).size;
const d = now();
const dateStr = d.toISOString().slice(0, 10);
const timeStr = `${String(d.getUTCHours()).padStart(2, '0')}-${String(d.getUTCMinutes()).padStart(2, '0')}`;
const remotePath = mcPath(`snapshots/${dateStr}/${timeStr}.db`);
const uploadResult = runMc(['cp', stagingFile, remotePath]);
if (uploadResult !== null) {
log('info', `Snapshot shipped: ${remotePath} (${(size / 1024).toFixed(1)}KB)`);
} else {
log('error', `Failed to upload snapshot to ${remotePath}`);
}
// Cleanup staging
try { unlinkSync(stagingFile); } catch {}
return { remotePath, size };
}
// -- Rollup (hourly) -----------------------------------------
function rollup() {
// First, take a snapshot and save it as a rollup
if (!existsSync(DB_PATH)) {
log('error', `DB not found: ${DB_PATH}`);
process.exit(1);
}
mkdirSync(STAGING_DIR, { recursive: true });
const stagingFile = join(STAGING_DIR, 'scheduler-rollup.db');
if (!backupDatabase(DB_PATH, stagingFile)) {
log('warn', 'Structured backup failed in rollup, falling back to direct file copy');
copyFileSync(DB_PATH, stagingFile);
}
const size = statSync(stagingFile).size;
const d = now();
const dateStr = d.toISOString().slice(0, 10);
const hourStr = String(d.getUTCHours()).padStart(2, '0');
const remotePath = mcPath(`rollups/${dateStr}/${hourStr}.db`);
const rollupResult = runMc(['cp', stagingFile, remotePath]);
if (rollupResult !== null) {
log('info', `Rollup shipped: ${remotePath} (${(size / 1024).toFixed(1)}KB)`);
} else {
log('error', `Failed to upload rollup to ${remotePath}`);
}
try { unlinkSync(stagingFile); } catch {}
// Only prune if the rollup uploaded successfully -- avoid deleting the only copies
if (rollupResult !== null) {
pruneSnapshots();
pruneRollups();
}
}
// -- Prune ---------------------------------------------------
function pruneSnapshots() {
const cutoff = new Date(Date.now() - SNAPSHOT_RETENTION_HOURS * 3600 * 1000);
const cutoffDate = cutoff.toISOString().slice(0, 10);
// List snapshot date directories
const listing = runMc(['ls', mcPath('snapshots/'), '--json'], { ignoreError: true });
if (!listing) return;
let pruned = 0;
for (const line of listing.split('\n').filter(Boolean)) {
try {
const obj = JSON.parse(line);
const dirName = obj.key?.replace(/\/$/, '');
if (dirName && dirName < cutoffDate) {
runMc(['rm', '--recursive', '--force', mcPath(`snapshots/${dirName}/`)], { ignoreError: true });
pruned++;
log('info', `Pruned snapshot dir: ${dirName}`);
}
} catch {}
}
if (pruned > 0) log('info', `Pruned ${pruned} old snapshot dir(s)`);
}
function pruneRollups() {
const cutoff = new Date(Date.now() - ROLLUP_RETENTION_DAYS * 86400 * 1000);
const cutoffDate = cutoff.toISOString().slice(0, 10);
const listing = runMc(['ls', mcPath('rollups/'), '--json'], { ignoreError: true });
if (!listing) return;
let pruned = 0;
for (const line of listing.split('\n').filter(Boolean)) {
try {
const obj = JSON.parse(line);
const dirName = obj.key?.replace(/\/$/, '');
if (dirName && dirName < cutoffDate) {
runMc(['rm', '--recursive', '--force', mcPath(`rollups/${dirName}/`)], { ignoreError: true });
pruned++;
log('info', `Pruned rollup dir: ${dirName}`);
}
} catch {}
}
if (pruned > 0) log('info', `Pruned ${pruned} old rollup dir(s)`);
}
// -- Restore -------------------------------------------------
function restore() {
// Find latest rollup first, then latest snapshot
let latest = null;
// Try rollups first (more reliable)
const rollupDirs = runMc(['ls', mcPath('rollups/'), '--json'], { ignoreError: true });
if (rollupDirs) {
const dirs = rollupDirs.split('\n').filter(Boolean).map(l => {
try { return JSON.parse(l).key?.replace(/\/$/, ''); } catch { return null; }
}).filter(Boolean).sort().reverse();
for (const dir of dirs) {
const files = runMc(['ls', mcPath(`rollups/${dir}/`), '--json'], { ignoreError: true });
if (files) {
const fList = files.split('\n').filter(Boolean).map(l => {
try { return JSON.parse(l).key; } catch { return null; }
}).filter(Boolean).sort().reverse();
if (fList.length > 0) {
latest = { type: 'rollup', path: mcPath(`rollups/${dir}/${fList[0]}`) };
break;
}
}
}
}
// Try snapshots if no rollup found
if (!latest) {
const snapDirs = runMc(['ls', mcPath('snapshots/'), '--json'], { ignoreError: true });
if (snapDirs) {
const dirs = snapDirs.split('\n').filter(Boolean).map(l => {
try { return JSON.parse(l).key?.replace(/\/$/, ''); } catch { return null; }
}).filter(Boolean).sort().reverse();
for (const dir of dirs) {
const files = runMc(['ls', mcPath(`snapshots/${dir}/`), '--json'], { ignoreError: true });
if (files) {
const fList = files.split('\n').filter(Boolean).map(l => {
try { return JSON.parse(l).key; } catch { return null; }
}).filter(Boolean).sort().reverse();
if (fList.length > 0) {
latest = { type: 'snapshot', path: mcPath(`snapshots/${dir}/${fList[0]}`) };
break;
}
}
}
}
}
if (!latest) {
log('error', 'No backups found to restore from');
process.exit(1);
}
log('info', `Restoring from ${latest.type}: ${latest.path}`);
// Backup current DB
if (existsSync(DB_PATH)) {
const backupPath = `${DB_PATH}.pre-restore.${Date.now()}`;
copyFileSync(DB_PATH, backupPath);
log('info', `Current DB backed up to ${backupPath}`);
}
// Download and replace
mkdirSync(STAGING_DIR, { recursive: true });
const downloadPath = join(STAGING_DIR, 'restore.db');
const dlResult = runMc(['cp', latest.path, downloadPath]);
if (dlResult === null) {
log('error', 'Download failed');
process.exit(1);
}
// Verify the downloaded DB
if (!hasSqlite3()) {
log('error', 'sqlite3 not found in PATH; cannot verify downloaded DB integrity');
process.exit(1);
}
const verify = runSqlite3(downloadPath, 'SELECT count(*) FROM jobs');
if (verify === null) {
log('error', 'Downloaded DB failed verification (corrupt or missing expected tables)');
process.exit(1);
}
// Remove WAL/SHM files from current DB
try { unlinkSync(`${DB_PATH}-wal`); } catch {}
try { unlinkSync(`${DB_PATH}-shm`); } catch {}
// Replace
copyFileSync(downloadPath, DB_PATH);
try { unlinkSync(downloadPath); } catch {}
log('info', `Restored: ${verify} jobs from ${latest.type}`);
console.log(`Restored ${verify} jobs from ${latest.path}`);
}
// -- Status --------------------------------------------------
function status() {
console.log('=== Scheduler Backup Status ===\n');
// Current DB
if (existsSync(DB_PATH)) {
const st = statSync(DB_PATH);
console.log(`Local DB: ${(st.size / 1024).toFixed(1)}KB, modified ${st.mtime.toISOString()}`);
if (hasSqlite3()) {
const jobCount = runSqlite3(DB_PATH, 'SELECT count(*) FROM jobs') || '?';
console.log(`Jobs: ${jobCount}`);
} else {
console.log('Jobs: ? (sqlite3 not found in PATH)');
}
}
// Snapshots
console.log('\nSnapshots (last 24h):');
const snapDirs = runMc(['ls', mcPath('snapshots/')], { ignoreError: true });
if (snapDirs) {
console.log(snapDirs);
} else {
console.log(' (none)');
}
// Rollups
console.log('\nRollups (last 7d):');
const rollupDirsOut = runMc(['ls', mcPath('rollups/')], { ignoreError: true });
if (rollupDirsOut) {
console.log(rollupDirsOut);
} else {
console.log(' (none)');
}
// Total size
const du = runMc(['du', mcPath('')], { ignoreError: true });
if (du) console.log(`\nTotal backup size: ${du}`);
}
// -- Exports for programmatic consumers -----------------------
export { snapshot, rollup, restore, status, pruneSnapshots, pruneRollups };
// -- Main (only runs when executed directly, not when imported) --
import { fileURLToPath } from 'url';
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const command = process.argv[2] || 'status';
switch (command) {
case 'snapshot': snapshot(); break;
case 'rollup': rollup(); break;
case 'restore': restore(); break;
case 'status': status(); break;
case 'prune': pruneSnapshots(); pruneRollups(); break;
default:
console.error(`Unknown command: ${command}`);
console.error('Usage: node backup.js [snapshot|rollup|restore|status|prune]');
process.exit(1);
}
}