parrot-blackbox 2.1.1 → 2.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "parrot-blackbox",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "Crash-proof, multi-cloud backup & recovery automation for Parrot OS. Daily/weekly off-disk backups with automatic catch-up, smart storage across many MEGA + Google Drive accounts, Timeshift snapshot backups and one-command restore.",
5
5
  "type": "module",
6
6
  "main": "src/cli.js",
@@ -103,8 +103,25 @@ export function buildUrgentBundle({ home = process.env.HOME } = {}) {
103
103
  */
104
104
  export async function runUrgentBackup(cfg = loadConfig(), state = loadState(), { onProgress } = {}) {
105
105
  const now = clock();
106
- const id = iso(now);
107
- journal('urgent', `start id=${id}`);
106
+
107
+ // ── Resume an interrupted upload ─────────────────────────────────────────
108
+ // If a previous urgent upload died mid-transfer (power cut / crash / Ctrl+C),
109
+ // a pending marker was persisted in state BEFORE any long work began. On the
110
+ // next run we REUSE that id so `planAndPlace` writes back into the SAME cloud
111
+ // directory — rclone copy is per-file idempotent, so already-uploaded files
112
+ // are skipped and only the remainder is transferred, then the manifest is
113
+ // rewritten once the last file lands. The bundle itself is regenerated (the
114
+ // staging dir is transient), which is fine because the sources are fixed.
115
+ const resumed = Boolean(state.urgentPending?.id);
116
+ const id = resumed ? state.urgentPending.id : iso(now);
117
+ if (!resumed) {
118
+ state.urgentPending = { id, since: iso(now) };
119
+ saveState(state); // persist the marker before any long work → crash-safe
120
+ journal('urgent', `start id=${id}`);
121
+ } else {
122
+ journal('urgent', `resume id=${id} (previous upload was interrupted)`);
123
+ }
124
+
108
125
  const bundle = buildUrgentBundle();
109
126
 
110
127
  const accounts = await refreshAccounts(cfg);
@@ -134,11 +151,16 @@ export async function runUrgentBackup(cfg = loadConfig(), state = loadState(), {
134
151
  totalSize: manifest.totalSize,
135
152
  entryCount: manifest.entries?.length || 0,
136
153
  };
154
+ // Only clear the pending marker once the upload FULLY landed (manifest is on
155
+ // the cloud + mirrored locally). If we crash before this line, the next run
156
+ // resumes the same id — re-copying is a no-op for finished files.
157
+ delete state.urgentPending;
137
158
  saveState(state);
138
159
  journal('urgent', `done id=${id} bytes=${manifest.totalSize}`);
139
160
 
140
161
  return {
141
162
  id,
163
+ resumed,
142
164
  manifest,
143
165
  sizeBytes: manifest.totalSize,
144
166
  skippedRepos: bundle.skippedRepos,
package/src/cli.js CHANGED
@@ -423,7 +423,7 @@ const main = defineCommand({
423
423
  try {
424
424
  const r = await runUrgentBackup(undefined, undefined, { onProgress: progress });
425
425
  progress.stop();
426
- console.log(`${pc.green('✔')} Urgent backup stored (${bytesHuman(r.sizeBytes)}). Restore with: \`parrot-blackbox restore urgent\`.`);
426
+ console.log(`${pc.green('✔')} Urgent backup ${r.resumed ? 'resumed & ' : ''}stored (${bytesHuman(r.sizeBytes)}). Restore with: \`parrot-blackbox restore urgent\`.`);
427
427
  if (r.skippedRepos?.length) console.log(pc.dim(`Skipped ${r.skippedRepos.length} git-tracked folder(s).`));
428
428
  if (r.missing?.length) console.log(pc.dim(`Source(s) not present, skipped: ${r.missing.join(', ')}.`));
429
429
  } catch (e) {
@@ -65,7 +65,11 @@ export function walkFiles(localDir) {
65
65
  * will Google Drive accounts be used.
66
66
  * 2. Within each provider tier the classic water-filling strategy applies:
67
67
  * pick the account that results in the lowest used-percentage after
68
- * placing the file (most headroom wins on ties).
68
+ * placing the file. Accounts that are within ~0.5% of quota from each
69
+ * other are treated as equally full, and the account EARLIEST in the
70
+ * configured pool order wins the tie — so an all-empty pool fills in
71
+ * account order (mega-1, mega-2, …) instead of chasing `rclone about`
72
+ * usage noise.
69
73
  */
70
74
  export function chooseAccount(needed, accounts) {
71
75
  // Tier 0 = mega, Tier 1 = gdrive / everything else.
@@ -79,14 +83,26 @@ export function chooseAccount(needed, accounts) {
79
83
  const candidates = eligible.filter((a) => providerTier(a) === bestTier);
80
84
 
81
85
  // Water-fill within the chosen tier: minimise resulting used-percentage.
86
+ // SCORE_EPS absorbs `rclone about` usage noise — MEGA inconsistently reports
87
+ // a few bytes to tens-of-MiB of phantom usage on otherwise-empty accounts.
88
+ // As long as accounts are effectively equally full, files go onto the account
89
+ // EARLIEST in the configured pool order.
90
+ const SCORE_EPS = 0.005; // 0.5 percentage points of quota
82
91
  let best = null;
83
92
  let bestScore = Infinity;
84
- for (const acc of candidates) {
93
+ let bestIdx = Infinity;
94
+ for (let i = 0; i < candidates.length; i++) {
95
+ const acc = candidates[i];
85
96
  const quota = acc.total || 0;
86
97
  const score = quota ? (acc.used + needed) / quota : needed;
87
- if (score < bestScore || (score === bestScore && acc.free > (best?.free ?? -1))) {
98
+ if (score < bestScore - SCORE_EPS) {
88
99
  bestScore = score;
89
100
  best = acc;
101
+ bestIdx = i;
102
+ } else if (Math.abs(score - bestScore) <= SCORE_EPS && i < bestIdx) {
103
+ // Effectively a tie → the earlier account in the pool wins.
104
+ best = acc;
105
+ bestIdx = i;
90
106
  }
91
107
  }
92
108
  return best;
@@ -150,7 +150,7 @@ export async function restoreArtifact(manifest, destDir, { onProgress } = {}) {
150
150
  for (const entry of batch.entries) {
151
151
  const target = path.join(destDir, ...entry.rel.split('/'));
152
152
  const loc = entry.loc[0];
153
- const r = await copyToFile(`${loc.remote}:${loc.path}`, target);
153
+ const r = await copyToFile(`${loc.remote}:${loc.path}`, target, { force: true });
154
154
  if (!r.ok) throw new Error(`download failed for ${entry.rel}: ${r.error}`);
155
155
  files += 1;
156
156
  bytes += entry.size;
@@ -64,9 +64,10 @@ export async function copyDir(localDir, remotePath) {
64
64
  }
65
65
 
66
66
  /** Copy a single local file to an exact remote path. */
67
- export async function copyToFile(localFile, remotePath, { ignoreExisting = false } = {}) {
67
+ export async function copyToFile(localFile, remotePath, { ignoreExisting = false, force = false } = {}) {
68
68
  const args = ['copyto', localFile, remotePath];
69
69
  if (ignoreExisting) args.push('--ignore-existing');
70
+ if (force) args.push('--ignore-times'); // overwrite even a "newer" destination
70
71
  const res = await rejectFalse(args);
71
72
  return { ok: res.exitCode === 0, exitCode: res.exitCode, error: res.stderr?.trim() };
72
73
  }
@@ -131,7 +132,12 @@ export async function downloadBatch(remotePath, localDir, filesFromPath) {
131
132
  '--files-from', filesFromPath,
132
133
  '--transfers=16',
133
134
  '--checkers=16',
134
- '--fast-list'
135
+ '--fast-list',
136
+ // Restore MUST reproduce the artifact exactly. rclone's default skips a
137
+ // destination file whose mtime looks newer (exactly what fresh-install
138
+ // default files are), which silently leaves "conflicts" — your backed-up
139
+ // version never comes back. --ignore-times forces the overwrite.
140
+ '--ignore-times',
135
141
  ];
136
142
  const res = await rejectFalse(args);
137
143
  return { ok: res.exitCode === 0, exitCode: res.exitCode, error: res.stderr?.trim() };