mirror of
https://github.com/restic/restic.git
synced 2024-12-24 16:54:00 +00:00
Delete files in parallel in forget
This commit is contained in:
parent
2ee654763b
commit
1c0b61204b
1 changed files with 26 additions and 40 deletions
|
@ -94,34 +94,22 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
removeSnapshots := 0
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(gopts.ctx)
|
ctx, cancel := context.WithCancel(gopts.ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var snapshots restic.Snapshots
|
var snapshots restic.Snapshots
|
||||||
|
removeSnIDs := restic.NewIDSet()
|
||||||
|
|
||||||
for sn := range FindFilteredSnapshots(ctx, repo, opts.Hosts, opts.Tags, opts.Paths, args) {
|
for sn := range FindFilteredSnapshots(ctx, repo, opts.Hosts, opts.Tags, opts.Paths, args) {
|
||||||
snapshots = append(snapshots, sn)
|
snapshots = append(snapshots, sn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var jsonGroups []*ForgetGroup
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
// When explicit snapshots args are given, remove them immediately.
|
// When explicit snapshots args are given, remove them immediately.
|
||||||
for _, sn := range snapshots {
|
for _, sn := range snapshots {
|
||||||
if !opts.DryRun {
|
removeSnIDs.Insert(*sn.ID())
|
||||||
h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
|
|
||||||
if err = repo.Backend().Remove(gopts.ctx, h); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !gopts.JSON {
|
|
||||||
Verbosef("removed snapshot %v\n", sn.ID().Str())
|
|
||||||
}
|
|
||||||
removeSnapshots++
|
|
||||||
} else {
|
|
||||||
if !gopts.JSON {
|
|
||||||
Verbosef("would have removed snapshot %v\n", sn.ID().Str())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
snapshotGroups, _, err := restic.GroupSnapshots(snapshots, opts.GroupBy)
|
snapshotGroups, _, err := restic.GroupSnapshots(snapshots, opts.GroupBy)
|
||||||
|
@ -151,8 +139,6 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
|
||||||
Verbosef("Applying Policy: %v\n", policy)
|
Verbosef("Applying Policy: %v\n", policy)
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonGroups []*ForgetGroup
|
|
||||||
|
|
||||||
for k, snapshotGroup := range snapshotGroups {
|
for k, snapshotGroup := range snapshotGroups {
|
||||||
if gopts.Verbose >= 1 && !gopts.JSON {
|
if gopts.Verbose >= 1 && !gopts.JSON {
|
||||||
err = PrintSnapshotGroupHeader(gopts.stdout, k)
|
err = PrintSnapshotGroupHeader(gopts.stdout, k)
|
||||||
|
@ -191,37 +177,37 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
|
||||||
|
|
||||||
jsonGroups = append(jsonGroups, &fg)
|
jsonGroups = append(jsonGroups, &fg)
|
||||||
|
|
||||||
removeSnapshots += len(remove)
|
for _, sn := range remove {
|
||||||
|
removeSnIDs.Insert(*sn.ID())
|
||||||
if !opts.DryRun {
|
|
||||||
for _, sn := range remove {
|
|
||||||
h := restic.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
|
|
||||||
err = repo.Backend().Remove(gopts.ctx, h)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if gopts.JSON {
|
|
||||||
err = printJSONForget(gopts.stdout, jsonGroups)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if removeSnapshots > 0 && opts.Prune {
|
if len(removeSnIDs) > 0 {
|
||||||
if !gopts.JSON {
|
|
||||||
Verbosef("%d snapshots have been removed, running prune\n", removeSnapshots)
|
|
||||||
}
|
|
||||||
if !opts.DryRun {
|
if !opts.DryRun {
|
||||||
return pruneRepository(gopts, repo)
|
err := DeleteFilesChecked(gopts, repo, removeSnIDs, restic.SnapshotFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !gopts.JSON {
|
||||||
|
Printf("Would have removed the following snapshots:\n%v\n\n", removeSnIDs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if gopts.JSON && len(jsonGroups) > 0 {
|
||||||
|
err = printJSONForget(gopts.stdout, jsonGroups)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(removeSnIDs) > 0 && opts.Prune && !opts.DryRun {
|
||||||
|
return pruneRepository(gopts, repo)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue