ghuser1 commited on
Commit
4afcd55
·
verified ·
1 Parent(s): c70e382

Upload backup-sqlite.mjs

Browse files
Files changed (1) hide show
  1. scripts/backup-sqlite.mjs +23 -1
scripts/backup-sqlite.mjs CHANGED
@@ -42,9 +42,10 @@ async function restoreBackup() {
42
 
43
  assertUsableSqlite(restorePath);
44
  const restoredSummary = inspectDatabase(restorePath);
 
45
  renameSync(restorePath, dbPath);
46
  rmSync(workDir, { recursive: true, force: true });
47
- console.log(JSON.stringify({ ok: true, mode: "restore", restored: true, ...restoredSummary }));
48
  }
49
 
50
  async function uploadBackup() {
@@ -68,6 +69,7 @@ async function uploadBackup() {
68
  await rclone(["copyto", snapshotPath, uploading]);
69
  await rclone(["moveto", uploading, target]);
70
  await rclone(["touch", target], { allowFailure: true });
 
71
  rmSync(workDir, { recursive: true, force: true });
72
  console.log(JSON.stringify({
73
  ok: true,
@@ -75,10 +77,30 @@ async function uploadBackup() {
75
  remote: target,
76
  bytes,
77
  ...snapshotSummary,
 
 
 
 
78
  updatedAt: Date.now()
79
  }));
80
  }
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  function createSnapshot(snapshotPath) {
83
  const db = new DatabaseSync(dbPath);
84
  try {
 
42
 
43
  assertUsableSqlite(restorePath);
44
  const restoredSummary = inspectDatabase(restorePath);
45
+ const bytes = statSync(restorePath).size;
46
  renameSync(restorePath, dbPath);
47
  rmSync(workDir, { recursive: true, force: true });
48
+ console.log(JSON.stringify({ ok: true, mode: "restore", restored: true, remote, bytes, ...restoredSummary }));
49
  }
50
 
51
  async function uploadBackup() {
 
69
  await rclone(["copyto", snapshotPath, uploading]);
70
  await rclone(["moveto", uploading, target]);
71
  await rclone(["touch", target], { allowFailure: true });
72
+ const remoteSummary = await verifyRemoteBackup(target, snapshotSummary);
73
  rmSync(workDir, { recursive: true, force: true });
74
  console.log(JSON.stringify({
75
  ok: true,
 
77
  remote: target,
78
  bytes,
79
  ...snapshotSummary,
80
+ remoteVerified: true,
81
+ remoteUsers: remoteSummary.users,
82
+ remoteNotes: remoteSummary.notes,
83
+ remoteFolders: remoteSummary.folders,
84
  updatedAt: Date.now()
85
  }));
86
  }
87
 
88
+ async function verifyRemoteBackup(target, expected) {
89
+ const workDir = mkdtempSync(join(tmpdir(), "notes-verify-"));
90
+ const verifyPath = join(workDir, "notes.db");
91
+ try {
92
+ await rclone(["copyto", target, verifyPath]);
93
+ assertUsableSqlite(verifyPath);
94
+ const actual = inspectDatabase(verifyPath);
95
+ if (actual.users !== expected.users || actual.notes !== expected.notes || actual.folders !== expected.folders) {
96
+ throw new Error("remote_backup_verify_mismatch");
97
+ }
98
+ return actual;
99
+ } finally {
100
+ rmSync(workDir, { recursive: true, force: true });
101
+ }
102
+ }
103
+
104
  function createSnapshot(snapshotPath) {
105
  const db = new DatabaseSync(dbPath);
106
  try {