mirror of
https://github.com/kopia/kopia.git
synced 2026-01-28 00:08:04 -05:00
* cli: ensure advanced commands are not accidentally used This prints an error when a dangerous command is used without first setting KOPIA_ADVANCED_COMMANDS=enabled environment variable. Co-authored-by: Julio López <julio+gh@kasten.io>
44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/blob"
|
|
"github.com/kopia/kopia/repo/maintenance"
|
|
)
|
|
|
|
var (
|
|
blobGarbageCollectCommand = blobCommands.Command("gc", "Garbage-collect unused blobs")
|
|
blobGarbageCollectCommandDelete = blobGarbageCollectCommand.Flag("delete", "Whether to delete unused blobs").String()
|
|
blobGarbageCollectParallel = blobGarbageCollectCommand.Flag("parallel", "Number of parallel blob scans").Default("16").Int()
|
|
blobGarbageCollectMinAge = blobGarbageCollectCommand.Flag("min-age", "Garbage-collect blobs with minimum age").Default("24h").Duration()
|
|
blobGarbageCollectPrefix = blobGarbageCollectCommand.Flag("prefix", "Only GC blobs with given prefix").String()
|
|
)
|
|
|
|
func runBlobGarbageCollectCommand(ctx context.Context, rep *repo.DirectRepository) error {
|
|
advancedCommand()
|
|
|
|
opts := maintenance.DeleteUnreferencedBlobsOptions{
|
|
DryRun: *blobGarbageCollectCommandDelete != "yes",
|
|
MinAge: *blobGarbageCollectMinAge,
|
|
Parallel: *blobGarbageCollectParallel,
|
|
Prefix: blob.ID(*blobGarbageCollectPrefix),
|
|
}
|
|
|
|
n, err := maintenance.DeleteUnreferencedBlobs(ctx, rep, opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if opts.DryRun && n > 0 {
|
|
printStderr("Pass --delete=yes to delete.\n")
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func init() {
|
|
blobGarbageCollectCommand.Action(directRepositoryAction(runBlobGarbageCollectCommand))
|
|
}
|