ghuser1 commited on
Commit
29ea797
·
verified ·
1 Parent(s): 40a984b

Upload backup-sqlite.mjs

Browse files
Files changed (1) hide show
  1. scripts/backup-sqlite.mjs +8 -12
scripts/backup-sqlite.mjs CHANGED
@@ -30,15 +30,16 @@ try {
30
  async function restoreBackup() {
31
  mkdirSync(dataDir, { recursive: true });
32
  const remote = remotePath(backupFile);
33
- const exists = await remoteFileExists();
34
- if (!exists) {
35
- console.log(JSON.stringify({ ok: true, mode: "restore", restored: false, reason: "remote_backup_missing" }));
 
 
 
 
36
  return;
37
  }
38
 
39
- const workDir = mkdtempSync(join(tmpdir(), "notes-restore-"));
40
- const restorePath = join(workDir, "notes.db");
41
- await rclone(["copyto", remote, restorePath]);
42
  assertUsableSqlite(restorePath);
43
  renameSync(restorePath, dbPath);
44
  rmSync(workDir, { recursive: true, force: true });
@@ -69,11 +70,6 @@ async function uploadBackup() {
69
  }));
70
  }
71
 
72
- async function remoteFileExists() {
73
- const output = await rclone(["lsf", remoteFolder, "--files-only", "--include", backupFile], { allowFailure: true });
74
- return output.split(/\r?\n/).some((line) => line.trim() === backupFile);
75
- }
76
-
77
  function createSnapshot(snapshotPath) {
78
  const db = new DatabaseSync(dbPath);
79
  try {
@@ -108,7 +104,7 @@ function rclone(args, { allowFailure = false } = {}) {
108
  child.on("error", reject);
109
  child.on("close", (code) => {
110
  if (code === 0 || allowFailure) {
111
- resolve(stdout);
112
  return;
113
  }
114
  reject(new Error(`rclone_failed_${code}`));
 
30
  async function restoreBackup() {
31
  mkdirSync(dataDir, { recursive: true });
32
  const remote = remotePath(backupFile);
33
+ const workDir = mkdtempSync(join(tmpdir(), "notes-restore-"));
34
+ const restorePath = join(workDir, "notes.db");
35
+
36
+ const copied = await rclone(["copyto", remote, restorePath], { allowFailure: true });
37
+ if (!copied.ok) {
38
+ rmSync(workDir, { recursive: true, force: true });
39
+ console.log(JSON.stringify({ ok: true, mode: "restore", restored: false, reason: "remote_backup_unavailable" }));
40
  return;
41
  }
42
 
 
 
 
43
  assertUsableSqlite(restorePath);
44
  renameSync(restorePath, dbPath);
45
  rmSync(workDir, { recursive: true, force: true });
 
70
  }));
71
  }
72
 
 
 
 
 
 
73
  function createSnapshot(snapshotPath) {
74
  const db = new DatabaseSync(dbPath);
75
  try {
 
104
  child.on("error", reject);
105
  child.on("close", (code) => {
106
  if (code === 0 || allowFailure) {
107
+ resolve({ ok: code === 0, stdout });
108
  return;
109
  }
110
  reject(new Error(`rclone_failed_${code}`));