Skip to content

Commit

Permalink
add describe-backup --with-collections to show collections in backup
Browse files Browse the repository at this point in the history
  • Loading branch information
defbin committed Sep 14, 2023
1 parent f4ebcb3 commit 2be59c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cli/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"sort"
"strings"
"time"

Expand All @@ -14,7 +15,9 @@ import (
"gopkg.in/yaml.v2"

"github.com/percona/percona-backup-mongodb/pbm"
"github.com/percona/percona-backup-mongodb/pbm/archive"
"github.com/percona/percona-backup-mongodb/pbm/compress"
"github.com/percona/percona-backup-mongodb/pbm/storage"
"github.com/percona/percona-backup-mongodb/version"
)

Expand Down Expand Up @@ -66,6 +69,7 @@ func (b externBcpOut) String() string {

type descBcp struct {
name string
coll bool
}

func runBackup(cn *pbm.PBM, b *backupOpts, outf outFormat) (fmt.Stringer, error) {
Expand Down Expand Up @@ -307,6 +311,7 @@ type bcpReplDesc struct {
IsConfigSvr *bool `json:"configsvr,omitempty" yaml:"configsvr,omitempty"`
SecurityOpts *pbm.MongodOptsSec `json:"security,omitempty" yaml:"security,omitempty"`
Error *string `json:"error,omitempty" yaml:"error,omitempty"`
Collections []string `json:"collections,omitempty" yaml:"collections,omitempty"`
}

func (b *bcpDesc) String() string {
Expand Down Expand Up @@ -340,6 +345,19 @@ func describeBackup(cn *pbm.PBM, b *descBcp) (fmt.Stringer, error) {
return nil, err
}

var stg storage.Storage
if b.coll {
stg, err = cn.GetStorage(nil)
if err != nil {
return nil, errors.WithMessage(err, "get storage")
}

_, err := stg.FileStat(pbm.StorInitFile)
if err != nil {
return nil, errors.WithMessage(err, "check storage access")
}
}

rv := &bcpDesc{
Name: bcp.Name,
OPID: bcp.OPID,
Expand Down Expand Up @@ -398,6 +416,22 @@ func describeBackup(cn *pbm.PBM, b *descBcp) (fmt.Stringer, error) {
if bcp.Type == pbm.ExternalBackup {
rv.Replsets[i].Files = r.Files
}

if !b.coll || bcp.Type != pbm.LogicalBackup {
continue
}

nss, err := pbm.ReadArchiveNamespaces(stg, r.DumpName)
if err != nil {
return nil, errors.WithMessage(err, "read archive metadata")
}

rv.Replsets[i].Collections = make([]string, len(nss))
for j, ns := range nss {
rv.Replsets[i].Collections[j] = archive.NSify(ns.Database, ns.Collection)
}

sort.Strings(rv.Replsets[i].Collections)
}

return rv, err
Expand Down
2 changes: 2 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func Main() {

descBcpCmd := pbmCmd.Command("describe-backup", "Describe backup")
descBcp := descBcp{}
descBcpCmd.Flag("with-collections", "Show collections in backup").
BoolVar(&descBcp.coll)
descBcpCmd.Arg("backup_name", "Backup name").
StringVar(&descBcp.name)

Expand Down

0 comments on commit 2be59c6

Please sign in to comment.