Files
kopia/cli/command_index_optimize.go
Jarek Kowalski 6a14ac8a2a cli: ensure advanced commands are not accidentally used (#611)
* 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>
2020-09-12 20:31:25 -07:00

38 lines
1.3 KiB
Go

package cli
import (
"context"
"github.com/kopia/kopia/internal/clock"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
)
var (
optimizeCommand = indexCommands.Command("optimize", "Optimize indexes blobs.")
optimizeMaxSmallBlobs = optimizeCommand.Flag("max-small-blobs", "Maximum number of small index blobs that can be left after compaction.").Default("1").Int()
optimizeDropDeletedOlderThan = optimizeCommand.Flag("drop-deleted-older-than", "Drop deleted contents above given age").Duration()
optimizeDropContents = optimizeCommand.Flag("drop-contents", "Drop contents with given IDs").Strings()
optimizeAllIndexes = optimizeCommand.Flag("all", "Optimize all indexes, even those above maximum size.").Bool()
)
func runOptimizeCommand(ctx context.Context, rep *repo.DirectRepository) error {
advancedCommand()
opt := content.CompactOptions{
MaxSmallBlobs: *optimizeMaxSmallBlobs,
AllIndexes: *optimizeAllIndexes,
DropContents: toContentIDs(*optimizeDropContents),
}
if age := *optimizeDropDeletedOlderThan; age > 0 {
opt.DropDeletedBefore = clock.Now().Add(-age)
}
return rep.Content.CompactIndexes(ctx, opt)
}
func init() {
optimizeCommand.Action(directRepositoryAction(runOptimizeCommand))
}