Skip to content

Commit 99133a1

Browse files
committed
implement automatic compacting
1 parent 01d8e4f commit 99133a1

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

backup.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env -S deno run --ext=ts --allow-run --allow-read --allow-sys
22

3-
import * as hex from "jsr:@std/encoding@1.0.5/hex";
4-
import * as fs from "jsr:@std/fs@1.0.4";
5-
import * as path from "jsr:@std/path@1.0.6";
63
import {
74
ArgumentValue,
85
Command,
96
Type,
107
ValidationError,
118
} from "jsr:@cliffy/command@1.0.0-rc.7";
9+
import * as hex from "jsr:@std/encoding@1.0.5/hex";
10+
import * as fs from "jsr:@std/fs@1.0.4";
11+
import * as path from "jsr:@std/path@1.0.6";
12+
import * as semver from "jsr:@std/semver@1.0.3";
1213

1314
function command(name: string, ...args: string[]): Promise<Deno.CommandOutput> {
1415
return new Deno.Command(name, { args }).output();
@@ -55,6 +56,7 @@ const cli = new Command()
5556
required: true,
5657
})
5758
.option("-R --repo <path:file>", "Path to borg repo", { required: true })
59+
.option("--no-compact", "Disable automatic archive compacting")
5860
.option("--no-prune", "Disable automatic archive pruning")
5961
.option("-K --keep <value:keep>", "Timeframe to keep backups for (eg: 7d)", {
6062
required: true,
@@ -71,8 +73,19 @@ const cli = new Command()
7173
const borg = (...args: string[]) => command("borg", ...args);
7274
const rclone = (...args: string[]) => command("rclone", ...args);
7375

76+
let canCompact = false;
7477
try {
75-
await borg("-V");
78+
const resp = await borg("-V");
79+
if (!resp.success) throw new Error();
80+
81+
const version = new TextDecoder().decode(resp.stdout).replace(
82+
"borg ",
83+
"",
84+
);
85+
86+
const parsed = semver.parse(version);
87+
const isOneDotFour = semver.greaterOrEqual(parsed, semver.parse("1.4.0"));
88+
if (isOneDotFour) canCompact = true;
7689
} catch {
7790
console.log("missing `borg` binary");
7891
console.log(
@@ -88,6 +101,7 @@ const cli = new Command()
88101
init,
89102
name,
90103
repo,
104+
compact,
91105
prune,
92106
keep,
93107
sync,
@@ -169,7 +183,18 @@ const cli = new Command()
169183
print(resp.stderr);
170184
}
171185

172-
// TODO: Detect borg 1.2.x and compact
186+
if (compact && canCompact) {
187+
const resp = await borg(
188+
"compact",
189+
"--cleanup-commits",
190+
repo,
191+
);
192+
193+
if (!resp.success) {
194+
console.log("failed to compact backups");
195+
if (verbose) print(resp.stderr);
196+
}
197+
}
173198
}
174199

175200
if (sync) {

0 commit comments

Comments
 (0)