-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01d8e4f
commit 99133a1
Showing
1 changed file
with
30 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
#!/usr/bin/env -S deno run --ext=ts --allow-run --allow-read --allow-sys | ||
|
||
import * as hex from "jsr:@std/[email protected]/hex"; | ||
import * as fs from "jsr:@std/[email protected]"; | ||
import * as path from "jsr:@std/[email protected]"; | ||
import { | ||
ArgumentValue, | ||
Command, | ||
Type, | ||
ValidationError, | ||
} from "jsr:@cliffy/[email protected]"; | ||
import * as hex from "jsr:@std/[email protected]/hex"; | ||
import * as fs from "jsr:@std/[email protected]"; | ||
import * as path from "jsr:@std/[email protected]"; | ||
import * as semver from "jsr:@std/[email protected]"; | ||
|
||
function command(name: string, ...args: string[]): Promise<Deno.CommandOutput> { | ||
return new Deno.Command(name, { args }).output(); | ||
|
@@ -55,6 +56,7 @@ const cli = new Command() | |
required: true, | ||
}) | ||
.option("-R --repo <path:file>", "Path to borg repo", { required: true }) | ||
.option("--no-compact", "Disable automatic archive compacting") | ||
.option("--no-prune", "Disable automatic archive pruning") | ||
.option("-K --keep <value:keep>", "Timeframe to keep backups for (eg: 7d)", { | ||
required: true, | ||
|
@@ -71,8 +73,19 @@ const cli = new Command() | |
const borg = (...args: string[]) => command("borg", ...args); | ||
const rclone = (...args: string[]) => command("rclone", ...args); | ||
|
||
let canCompact = false; | ||
try { | ||
await borg("-V"); | ||
const resp = await borg("-V"); | ||
if (!resp.success) throw new Error(); | ||
|
||
const version = new TextDecoder().decode(resp.stdout).replace( | ||
"borg ", | ||
"", | ||
); | ||
|
||
const parsed = semver.parse(version); | ||
const isOneDotFour = semver.greaterOrEqual(parsed, semver.parse("1.4.0")); | ||
if (isOneDotFour) canCompact = true; | ||
} catch { | ||
console.log("missing `borg` binary"); | ||
console.log( | ||
|
@@ -88,6 +101,7 @@ const cli = new Command() | |
init, | ||
name, | ||
repo, | ||
compact, | ||
prune, | ||
keep, | ||
sync, | ||
|
@@ -169,7 +183,18 @@ const cli = new Command() | |
print(resp.stderr); | ||
} | ||
|
||
// TODO: Detect borg 1.2.x and compact | ||
if (compact && canCompact) { | ||
const resp = await borg( | ||
"compact", | ||
"--cleanup-commits", | ||
repo, | ||
); | ||
|
||
if (!resp.success) { | ||
console.log("failed to compact backups"); | ||
if (verbose) print(resp.stderr); | ||
} | ||
} | ||
} | ||
|
||
if (sync) { | ||
|