1
1
#!/usr/bin/env -S deno run --ext=ts --allow-run --allow-read --allow-sys
2
2
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" ;
6
3
import {
7
4
ArgumentValue ,
8
5
Command ,
9
6
Type ,
10
7
ValidationError ,
11
8
} 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" ;
12
13
13
14
function command ( name : string , ...args : string [ ] ) : Promise < Deno . CommandOutput > {
14
15
return new Deno . Command ( name , { args } ) . output ( ) ;
@@ -55,6 +56,7 @@ const cli = new Command()
55
56
required : true ,
56
57
} )
57
58
. option ( "-R --repo <path:file>" , "Path to borg repo" , { required : true } )
59
+ . option ( "--no-compact" , "Disable automatic archive compacting" )
58
60
. option ( "--no-prune" , "Disable automatic archive pruning" )
59
61
. option ( "-K --keep <value:keep>" , "Timeframe to keep backups for (eg: 7d)" , {
60
62
required : true ,
@@ -71,8 +73,19 @@ const cli = new Command()
71
73
const borg = ( ...args : string [ ] ) => command ( "borg" , ...args ) ;
72
74
const rclone = ( ...args : string [ ] ) => command ( "rclone" , ...args ) ;
73
75
76
+ let canCompact = false ;
74
77
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 ;
76
89
} catch {
77
90
console . log ( "missing `borg` binary" ) ;
78
91
console . log (
@@ -88,6 +101,7 @@ const cli = new Command()
88
101
init,
89
102
name,
90
103
repo,
104
+ compact,
91
105
prune,
92
106
keep,
93
107
sync,
@@ -169,7 +183,18 @@ const cli = new Command()
169
183
print ( resp . stderr ) ;
170
184
}
171
185
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
+ }
173
198
}
174
199
175
200
if ( sync ) {
0 commit comments