mirror of
https://github.com/restic/restic.git
synced 2025-10-27 09:48:56 +00:00
checker: add option to remove orphaned packs
This commit is contained in:
@@ -5,11 +5,13 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/restic/restic/backend"
|
||||
"github.com/restic/restic/checker"
|
||||
)
|
||||
|
||||
type CmdCheck struct {
|
||||
ReadData bool ` long:"read-data" description:"Read data blobs" default:"false"`
|
||||
ReadData bool `long:"read-data" description:"Read data blobs" default:"false"`
|
||||
RemoveOrphaned bool `long:"remove" description:"Remove data that isn't used" default:"false"`
|
||||
|
||||
global *GlobalOptions
|
||||
}
|
||||
@@ -45,10 +47,10 @@ func (cmd CmdCheck) Execute(args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
checker := checker.New(repo)
|
||||
chkr := checker.New(repo)
|
||||
|
||||
cmd.global.Verbosef("Load indexes\n")
|
||||
if err = checker.LoadIndex(); err != nil {
|
||||
if err = chkr.LoadIndex(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -59,20 +61,42 @@ func (cmd CmdCheck) Execute(args []string) error {
|
||||
errChan := make(chan error)
|
||||
|
||||
cmd.global.Verbosef("Check all packs\n")
|
||||
go checker.Packs(errChan, done)
|
||||
go chkr.Packs(errChan, done)
|
||||
|
||||
foundOrphanedPacks := false
|
||||
for err := range errChan {
|
||||
errorsFound = true
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
|
||||
if e, ok := err.(checker.PackError); ok && e.Orphaned {
|
||||
foundOrphanedPacks = true
|
||||
}
|
||||
}
|
||||
|
||||
cmd.global.Verbosef("Check snapshots, trees and blobs\n")
|
||||
errChan = make(chan error)
|
||||
go chkr.Structure(errChan, done)
|
||||
|
||||
for err := range errChan {
|
||||
errorsFound = true
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
}
|
||||
|
||||
cmd.global.Verbosef("Check snapshots, trees and blobs\n")
|
||||
errChan = make(chan error)
|
||||
go checker.Structure(errChan, done)
|
||||
for _, id := range chkr.UnusedBlobs() {
|
||||
cmd.global.Verbosef("unused blob %v\n", id.Str())
|
||||
}
|
||||
|
||||
for err := range errChan {
|
||||
errorsFound = true
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
if foundOrphanedPacks && cmd.RemoveOrphaned {
|
||||
IDs := chkr.OrphanedPacks()
|
||||
cmd.global.Verbosef("Remove %d orphaned packs... ", len(IDs))
|
||||
|
||||
for _, id := range IDs {
|
||||
if err := repo.Backend().Remove(backend.Data, id.String()); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
cmd.global.Verbosef("done\n")
|
||||
}
|
||||
|
||||
if errorsFound {
|
||||
|
||||
Reference in New Issue
Block a user