-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollback.js
62 lines (54 loc) · 1.75 KB
/
rollback.js
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
const fs = require("fs-extra");
const cp = require("node:child_process");
const { list } = require("./ls");
exports.rollback = async (num) => {
await new Promise((resolve, reject) => {
const proc = cp.spawn("snapper-rollback", [num]);
proc.stdin.setDefaultEncoding("utf8");
process.stdin.on("data", (data) => {
console.log("stdin: ", String(data));
proc.stdin.write(String(data) + "\n");
proc.stdin.end();
process.stdin.pause();
});
proc.stdout.on("data", (out) => {
process.stdout.write(out);
});
proc.stderr.on("data", (err) => {
console.log(String(err));
});
proc.on("close", (code) => {
if (code > 0) {
console.log("Process exited with code: " + code);
reject(code);
} else {
resolve(code);
}
});
});
const snaps = await list();
const snap = snaps.find((snap) => parseInt(snap.num) === parseInt(num));
if (!snap) {
console.warn("Unable to find snapshot: ", num);
return;
}
const description = snap.description;
// If description is "Linux backup for 6.4.11-arch2-1 => ./bootbackup/6.4.11-arch2-1_20230806023652",
// copy the contents of the directory to /boot
const regex = /Linux backup for (.*) => (.*)/;
const match = description.match(regex);
if (match) {
const backupDir = match[2];
const exists = await fs.exists(backupDir);
if (exists) {
console.log("Removing contents of /boot...");
await fs.copy(backupDir, "/boot");
console.log("Copied contents of " + backupDir + " to /boot.");
console.log("\nReboot to finish.")
} else {
console.warn("Unable to find backup directory: ", backupDir);
}
} else {
console.warn("Unable to parse description: ", description);
}
};