2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-02 11:46:36 +00:00

prune: Only remove data after index has been uploaded

Closes #1032
This commit is contained in:
Alexander Neumann 2017-06-15 13:12:46 +02:00
parent bd7d5a429f
commit 6f24d038f8
4 changed files with 15 additions and 11 deletions

View File

@ -97,7 +97,7 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
Verbosef("building new index for repo\n")
bar := newProgressMax(!gopts.Quiet, uint64(stats.packs), "packs")
idx, err := index.New(ctx, repo, bar)
idx, err := index.New(ctx, repo, restic.NewIDSet(), bar)
if err != nil {
return err
}
@ -222,6 +222,10 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
bar.Done()
}
if err = rebuildIndex(ctx, repo, removePacks); err != nil {
return err
}
if len(removePacks) != 0 {
bar = newProgressMax(!gopts.Quiet, uint64(len(removePacks)), "packs deleted")
bar.Start()
@ -236,10 +240,6 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
bar.Done()
}
if err = rebuildIndex(ctx, repo); err != nil {
return err
}
Verbosef("done\n")
return nil
}

View File

@ -38,10 +38,10 @@ func runRebuildIndex(gopts GlobalOptions) error {
ctx, cancel := context.WithCancel(gopts.ctx)
defer cancel()
return rebuildIndex(ctx, repo)
return rebuildIndex(ctx, repo, restic.NewIDSet())
}
func rebuildIndex(ctx context.Context, repo restic.Repository) error {
func rebuildIndex(ctx context.Context, repo restic.Repository, ignorePacks restic.IDSet) error {
Verbosef("counting files in repo\n")
var packs uint64
@ -50,7 +50,7 @@ func rebuildIndex(ctx context.Context, repo restic.Repository) error {
}
bar := newProgressMax(!globalOptions.Quiet, packs, "packs")
idx, err := index.New(ctx, repo, bar)
idx, err := index.New(ctx, repo, ignorePacks, bar)
if err != nil {
return err
}

View File

@ -34,12 +34,12 @@ func newIndex() *Index {
}
// New creates a new index for repo from scratch.
func New(ctx context.Context, repo restic.Repository, p *restic.Progress) (*Index, error) {
func New(ctx context.Context, repo restic.Repository, ignorePacks restic.IDSet, p *restic.Progress) (*Index, error) {
p.Start()
defer p.Done()
ch := make(chan worker.Job)
go list.AllPacks(ctx, repo, ch)
go list.AllPacks(ctx, repo, ignorePacks, ch)
idx := newIndex()

View File

@ -37,7 +37,7 @@ func (l Result) Entries() []restic.Blob {
}
// AllPacks sends the contents of all packs to ch.
func AllPacks(ctx context.Context, repo Lister, ch chan<- worker.Job) {
func AllPacks(ctx context.Context, repo Lister, ignorePacks restic.IDSet, ch chan<- worker.Job) {
f := func(ctx context.Context, job worker.Job) (interface{}, error) {
packID := job.Data.(restic.ID)
entries, size, err := repo.ListPack(ctx, packID)
@ -55,6 +55,10 @@ func AllPacks(ctx context.Context, repo Lister, ch chan<- worker.Job) {
go func() {
defer close(jobCh)
for id := range repo.List(ctx, restic.DataFile) {
if ignorePacks.Has(id) {
continue
}
select {
case jobCh <- worker.Job{Data: id}:
case <-ctx.Done():