backupman 0.2.1 → 0.2.3
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/index.js +28 -28
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
2
|
const fs = require("fs");
|
|
5
3
|
const path = require("path");
|
|
6
4
|
const readline = require("readline");
|
|
@@ -22,11 +20,11 @@ class BackupMan {
|
|
|
22
20
|
try {
|
|
23
21
|
const data = await fs.promises.readFile(this.indexFile, "utf8");
|
|
24
22
|
const parsed = JSON.parse(data);
|
|
25
|
-
if (!
|
|
23
|
+
if (!parsed.snapshots || typeof parsed.lastId !== "number") {
|
|
26
24
|
throw new Error("Corrupted index");
|
|
27
25
|
}
|
|
28
26
|
return parsed;
|
|
29
|
-
} catch (
|
|
27
|
+
} catch (err) {
|
|
30
28
|
return { lastId: 0, snapshots: [] };
|
|
31
29
|
}
|
|
32
30
|
}
|
|
@@ -105,7 +103,9 @@ class BackupMan {
|
|
|
105
103
|
return `#${snapshot.id} [${ts}] ${msg}`;
|
|
106
104
|
}
|
|
107
105
|
|
|
108
|
-
async
|
|
106
|
+
async listSnapshots() {
|
|
107
|
+
await this.ensureDirs();
|
|
108
|
+
const index = await this.loadIndex();
|
|
109
109
|
if (!index.snapshots.length) {
|
|
110
110
|
console.log("No snapshots yet. Use `backupman save \"message\"` first.");
|
|
111
111
|
return null;
|
|
@@ -118,39 +118,40 @@ class BackupMan {
|
|
|
118
118
|
console.log(" " + this.formatSnapshot(s));
|
|
119
119
|
});
|
|
120
120
|
console.log("");
|
|
121
|
-
|
|
122
|
-
while (true) {
|
|
123
|
-
const answer = (await this.prompt("Type snapshot id to restore (empty = cancel): ")).trim();
|
|
124
|
-
if (!answer) return null;
|
|
125
|
-
const id = Number.parseInt(answer, 10);
|
|
126
|
-
if (!Number.isNaN(id)) {
|
|
127
|
-
const snap = list.find(s => s.id === id);
|
|
128
|
-
if (snap) return snap;
|
|
129
|
-
}
|
|
130
|
-
console.log("Invalid id. Please type an existing snapshot id or press Enter to cancel.");
|
|
131
|
-
}
|
|
121
|
+
return { index, list };
|
|
132
122
|
}
|
|
133
123
|
|
|
134
124
|
async restore() {
|
|
135
|
-
await this.
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
125
|
+
const loaded = await this.listSnapshots();
|
|
126
|
+
if (!loaded) return;
|
|
127
|
+
const { index } = loaded;
|
|
128
|
+
|
|
129
|
+
const answer = (await this.prompt("Type snapshot id to restore (empty = cancel): ")).trim();
|
|
130
|
+
if (!answer) {
|
|
139
131
|
console.log("Restore cancelled.");
|
|
140
132
|
return;
|
|
141
133
|
}
|
|
142
134
|
|
|
135
|
+
const id = Number.parseInt(answer, 10);
|
|
136
|
+
if (Number.isNaN(id)) {
|
|
137
|
+
console.log("Invalid id. Restore aborted.");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const snapshot = index.snapshots.find(s => s.id === id);
|
|
142
|
+
if (!snapshot) {
|
|
143
|
+
console.log("Snapshot not found. Restore aborted.");
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
143
147
|
console.log("");
|
|
144
148
|
console.log(`You chose snapshot: ${this.formatSnapshot(snapshot)}`);
|
|
145
|
-
const confirm = (await this.prompt("
|
|
149
|
+
const confirm = (await this.prompt("Restore this snapshot and overwrite current folder? (y/n): ")).trim().toLowerCase();
|
|
146
150
|
if (confirm !== "y" && confirm !== "yes") {
|
|
147
151
|
console.log("Restore aborted.");
|
|
148
152
|
return;
|
|
149
153
|
}
|
|
150
154
|
|
|
151
|
-
console.log("Backing up current state...");
|
|
152
|
-
await this.createSnapshot(`[auto] before restore from #${snapshot.id}`);
|
|
153
|
-
|
|
154
155
|
console.log("Clearing current directory (except .backupman)...");
|
|
155
156
|
await this.deleteEverythingExceptBackupDir();
|
|
156
157
|
|
|
@@ -165,10 +166,9 @@ class BackupMan {
|
|
|
165
166
|
function printUsage() {
|
|
166
167
|
console.log("backupman - ultra-simple local snapshot tool");
|
|
167
168
|
console.log("");
|
|
168
|
-
console.log("
|
|
169
|
-
console.log(
|
|
170
|
-
console.log(" backupman restore
|
|
171
|
-
console.log("");
|
|
169
|
+
console.log("Usage:");
|
|
170
|
+
console.log(' backupman save "message" Save current directory as a snapshot');
|
|
171
|
+
console.log(" backupman restore Restore from a snapshot");
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
async function main() {
|