Files
kopia/cli/command_logs_cleanup.go
Jarek Kowalski d84c884321 Added content manager internal logging (#1116)
* logging: added logger wrappers for Broadcast and Prefix

* nit: moved max hash size to a named constant

* content: added internal logger

* content: replaced context-based logging with explicit Loggers

This will capture the logger.Logger associated with the context when
the repository is opened and will reuse it for all logs instead of
creating new logger for each log message.

The new logger will also write logs to the internal logger in addition
to writing to a log file/console.

* cli: allow decrypting all blobs whose names start with _

* maintenance: added logs cleanup

* cli: commands to view logs

* cli: log selected command on each write session
2021-06-05 08:48:43 -07:00

54 lines
1.4 KiB
Go

package cli
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/maintenance"
)
type commandLogsCleanup struct {
maxTotalSizeMB int64
maxCount int
maxAge time.Duration
dryRun bool
}
func (c *commandLogsCleanup) setup(svc appServices, parent commandParent) {
cmd := parent.Command("cleanup", "Clean up logs")
cmd.Flag("max-age", "Maximal age").Default("720h").DurationVar(&c.maxAge)
cmd.Flag("max-count", "Maximal number of files to keep").Default("10000").IntVar(&c.maxCount)
cmd.Flag("max-total-size-mb", "Maximal total size in MiB").Default("1024").Int64Var(&c.maxTotalSizeMB)
cmd.Flag("dry-run", "Do not delete").BoolVar(&c.dryRun)
cmd.Action(svc.directRepositoryWriteAction(c.run))
}
func (c *commandLogsCleanup) run(ctx context.Context, rep repo.DirectRepositoryWriter) error {
toDelete, err := maintenance.CleanupLogs(ctx, rep, maintenance.LogRetentionOptions{
MaxTotalSize: c.maxTotalSizeMB << 20, //nolint:gomnd
MaxCount: c.maxCount,
MaxAge: c.maxAge,
DryRun: c.dryRun,
})
if err != nil {
return errors.Wrap(err, "error expiring logs")
}
if len(toDelete) > 0 {
if c.dryRun {
log(ctx).Infof("Would delete %v logs.", len(toDelete))
} else {
log(ctx).Infof("Deleted %v logs.", len(toDelete))
}
} else {
log(ctx).Infof("No logs found to delete.")
}
return nil
}