2016-08-04 17:42:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"restic"
|
|
|
|
"restic/debug"
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2016-08-15 18:13:56 +00:00
|
|
|
"restic/index"
|
2016-08-04 17:42:40 +00:00
|
|
|
"restic/repository"
|
|
|
|
"time"
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2016-08-04 17:42:40 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdPrune = &cobra.Command{
|
|
|
|
Use: "prune [flags]",
|
|
|
|
Short: "remove unneeded data from the repository",
|
|
|
|
Long: `
|
|
|
|
The "prune" command checks the repository and removes data that is not
|
|
|
|
referenced and therefore not needed any more.
|
|
|
|
`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runPrune(globalOptions)
|
|
|
|
},
|
2016-08-04 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-09-17 10:36:05 +00:00
|
|
|
cmdRoot.AddCommand(cmdPrune)
|
2016-08-04 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newProgressMax returns a progress that counts blobs.
|
|
|
|
func newProgressMax(show bool, max uint64, description string) *restic.Progress {
|
|
|
|
if !show {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:13:47 +00:00
|
|
|
p := restic.NewProgress()
|
2016-08-04 17:42:40 +00:00
|
|
|
|
|
|
|
p.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
|
|
|
|
status := fmt.Sprintf("[%s] %s %d / %d %s",
|
|
|
|
formatDuration(d),
|
|
|
|
formatPercent(s.Blobs, max),
|
|
|
|
s.Blobs, max, description)
|
|
|
|
|
|
|
|
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
|
|
|
|
if err == nil {
|
|
|
|
if len(status) > w {
|
|
|
|
max := w - len(status) - 4
|
|
|
|
status = status[:max] + "... "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:13:47 +00:00
|
|
|
PrintProgress("%s", status)
|
2016-08-04 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func runPrune(gopts GlobalOptions) error {
|
|
|
|
repo, err := OpenRepository(gopts)
|
2016-08-04 17:42:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := lockRepoExclusive(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-21 09:58:30 +00:00
|
|
|
return pruneRepository(gopts, repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
|
|
|
|
err := repo.LoadIndex()
|
2016-08-04 17:42:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
var stats struct {
|
|
|
|
blobs int
|
|
|
|
packs int
|
|
|
|
snapshots int
|
2016-08-15 18:13:56 +00:00
|
|
|
bytes int64
|
2016-08-04 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("counting files in repo\n")
|
2016-09-01 14:04:29 +00:00
|
|
|
for _ = range repo.List(restic.DataFile, done) {
|
2016-08-15 19:10:20 +00:00
|
|
|
stats.packs++
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("building new index for repo\n")
|
2016-08-15 19:10:20 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
bar := newProgressMax(!gopts.Quiet, uint64(stats.packs), "packs")
|
2016-08-15 19:10:20 +00:00
|
|
|
idx, err := index.New(repo, bar)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-15 14:45:52 +00:00
|
|
|
blobs := 0
|
2016-08-15 18:13:56 +00:00
|
|
|
for _, pack := range idx.Packs {
|
|
|
|
stats.bytes += pack.Size
|
2017-01-15 14:45:52 +00:00
|
|
|
blobs += len(pack.Entries)
|
2016-08-04 17:42:40 +00:00
|
|
|
}
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("repository contains %v packs (%v blobs) with %v bytes\n",
|
2017-01-15 14:45:52 +00:00
|
|
|
len(idx.Packs), blobs, formatBytes(uint64(stats.bytes)))
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
blobCount := make(map[restic.BlobHandle]int)
|
2016-08-04 17:42:40 +00:00
|
|
|
duplicateBlobs := 0
|
|
|
|
duplicateBytes := 0
|
|
|
|
|
2016-08-15 18:13:56 +00:00
|
|
|
// find duplicate blobs
|
|
|
|
for _, p := range idx.Packs {
|
|
|
|
for _, entry := range p.Entries {
|
2016-08-04 17:42:40 +00:00
|
|
|
stats.blobs++
|
2016-09-01 14:04:29 +00:00
|
|
|
h := restic.BlobHandle{ID: entry.ID, Type: entry.Type}
|
2016-08-15 18:13:56 +00:00
|
|
|
blobCount[h]++
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2016-08-15 18:13:56 +00:00
|
|
|
if blobCount[h] > 1 {
|
2016-08-04 17:42:40 +00:00
|
|
|
duplicateBlobs++
|
2016-08-15 18:13:56 +00:00
|
|
|
duplicateBytes += int(entry.Length)
|
2016-08-04 17:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("processed %d blobs: %d duplicate blobs, %v duplicate\n",
|
2016-08-15 19:13:38 +00:00
|
|
|
stats.blobs, duplicateBlobs, formatBytes(uint64(duplicateBytes)))
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("load all snapshots\n")
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2016-08-15 18:13:56 +00:00
|
|
|
// find referenced blobs
|
2016-08-04 17:42:40 +00:00
|
|
|
snapshots, err := restic.LoadAllSnapshots(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.snapshots = len(snapshots)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("find data that is still in use for %d snapshots\n", stats.snapshots)
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
usedBlobs := restic.NewBlobSet()
|
|
|
|
seenBlobs := restic.NewBlobSet()
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
bar = newProgressMax(!gopts.Quiet, uint64(len(snapshots)), "snapshots")
|
2016-08-04 17:42:40 +00:00
|
|
|
bar.Start()
|
|
|
|
for _, sn := range snapshots {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("process snapshot %v", sn.ID().Str())
|
2016-08-04 17:42:40 +00:00
|
|
|
|
|
|
|
err = restic.FindUsedBlobs(repo, *sn.Tree, usedBlobs, seenBlobs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("found %v blobs for snapshot %v", sn.ID().Str())
|
2016-08-04 17:42:40 +00:00
|
|
|
bar.Report(restic.Stat{Blobs: 1})
|
|
|
|
}
|
|
|
|
bar.Done()
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("found %d of %d data blobs still in use, removing %d blobs\n",
|
2016-09-12 12:26:47 +00:00
|
|
|
len(usedBlobs), stats.blobs, stats.blobs-len(usedBlobs))
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2016-08-15 18:13:56 +00:00
|
|
|
// find packs that need a rewrite
|
2016-09-01 14:04:29 +00:00
|
|
|
rewritePacks := restic.NewIDSet()
|
2017-01-15 14:45:52 +00:00
|
|
|
for _, pack := range idx.Packs {
|
|
|
|
for _, blob := range pack.Entries {
|
|
|
|
h := restic.BlobHandle{ID: blob.ID, Type: blob.Type}
|
|
|
|
if !usedBlobs.Has(h) {
|
|
|
|
rewritePacks.Insert(pack.ID)
|
|
|
|
continue
|
|
|
|
}
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2017-01-15 14:45:52 +00:00
|
|
|
if blobCount[h] > 1 {
|
|
|
|
rewritePacks.Insert(pack.ID)
|
|
|
|
}
|
2016-08-04 17:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 14:17:44 +00:00
|
|
|
removeBytes := duplicateBytes
|
2016-09-12 12:26:47 +00:00
|
|
|
|
2016-08-25 20:35:22 +00:00
|
|
|
// find packs that are unneeded
|
2016-09-01 14:04:29 +00:00
|
|
|
removePacks := restic.NewIDSet()
|
2016-08-25 20:35:22 +00:00
|
|
|
for packID, p := range idx.Packs {
|
2016-09-12 12:26:47 +00:00
|
|
|
|
|
|
|
hasActiveBlob := false
|
2016-08-25 20:35:22 +00:00
|
|
|
for _, blob := range p.Entries {
|
2016-09-01 14:04:29 +00:00
|
|
|
h := restic.BlobHandle{ID: blob.ID, Type: blob.Type}
|
2016-08-25 20:35:22 +00:00
|
|
|
if usedBlobs.Has(h) {
|
2016-09-12 12:26:47 +00:00
|
|
|
hasActiveBlob = true
|
|
|
|
continue
|
2016-08-25 20:35:22 +00:00
|
|
|
}
|
2016-09-12 12:26:47 +00:00
|
|
|
|
|
|
|
removeBytes += int(blob.Length)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasActiveBlob {
|
|
|
|
continue
|
2016-08-25 20:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removePacks.Insert(packID)
|
|
|
|
|
|
|
|
if !rewritePacks.Has(packID) {
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatalf("pack %v is unneeded, but not contained in rewritePacks", packID.Str())
|
2016-08-25 20:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rewritePacks.Delete(packID)
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("will delete %d packs and rewrite %d packs, this frees %s\n",
|
2016-09-12 12:26:47 +00:00
|
|
|
len(removePacks), len(rewritePacks), formatBytes(uint64(removeBytes)))
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2017-03-04 16:38:34 +00:00
|
|
|
if len(rewritePacks) != 0 {
|
2017-03-04 16:43:58 +00:00
|
|
|
bar = newProgressMax(!gopts.Quiet, uint64(len(rewritePacks)), "packs rewritten")
|
2017-03-04 16:38:34 +00:00
|
|
|
bar.Start()
|
|
|
|
err = repository.Repack(repo, rewritePacks, usedBlobs, bar)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bar.Done()
|
2016-08-04 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 16:38:34 +00:00
|
|
|
if len(removePacks) != 0 {
|
|
|
|
bar = newProgressMax(!gopts.Quiet, uint64(len(removePacks)), "packs deleted")
|
|
|
|
bar.Start()
|
|
|
|
for packID := range removePacks {
|
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: packID.String()}
|
|
|
|
err = repo.Backend().Remove(h)
|
|
|
|
if err != nil {
|
|
|
|
Warnf("unable to remove file %v from the repository\n", packID.Str())
|
|
|
|
}
|
|
|
|
bar.Report(restic.Stat{Blobs: 1})
|
2016-08-25 20:35:22 +00:00
|
|
|
}
|
2017-03-04 16:38:34 +00:00
|
|
|
bar.Done()
|
2016-08-25 20:35:22 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("creating new index\n")
|
2016-08-04 17:42:40 +00:00
|
|
|
|
2016-08-27 18:04:35 +00:00
|
|
|
stats.packs = 0
|
2016-09-01 14:04:29 +00:00
|
|
|
for _ = range repo.List(restic.DataFile, done) {
|
2016-08-20 18:44:57 +00:00
|
|
|
stats.packs++
|
|
|
|
}
|
2016-09-17 10:36:05 +00:00
|
|
|
bar = newProgressMax(!gopts.Quiet, uint64(stats.packs), "packs")
|
2016-08-15 19:10:20 +00:00
|
|
|
idx, err = index.New(repo, bar)
|
2016-08-04 17:42:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
var supersedes restic.IDs
|
|
|
|
for idxID := range repo.List(restic.IndexFile, done) {
|
2017-01-25 16:48:35 +00:00
|
|
|
h := restic.Handle{Type: restic.IndexFile, Name: idxID.String()}
|
|
|
|
err := repo.Backend().Remove(h)
|
2016-08-15 18:49:01 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "unable to remove index %v: %v\n", idxID.Str(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
supersedes = append(supersedes, idxID)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := idx.Save(repo, supersedes)
|
2016-08-15 18:46:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("saved new index as %v\n", id.Str())
|
2016-08-15 18:46:24 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("done\n")
|
2016-08-04 17:42:40 +00:00
|
|
|
return nil
|
|
|
|
}
|