github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/cliccl/load.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Licensed as a CockroachDB Enterprise file under the Cockroach Community 4 // License (the "License"); you may not use this file except in compliance with 5 // the License. You may obtain a copy of the License at 6 // 7 // https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt 8 9 package cliccl 10 11 import ( 12 "context" 13 "fmt" 14 "strings" 15 "time" 16 17 "github.com/cockroachdb/cockroach/pkg/base" 18 "github.com/cockroachdb/cockroach/pkg/blobs" 19 "github.com/cockroachdb/cockroach/pkg/ccl/backupccl" 20 "github.com/cockroachdb/cockroach/pkg/cli" 21 "github.com/cockroachdb/cockroach/pkg/settings/cluster" 22 "github.com/cockroachdb/cockroach/pkg/storage/cloud" 23 "github.com/cockroachdb/cockroach/pkg/util/hlc" 24 "github.com/cockroachdb/cockroach/pkg/util/humanizeutil" 25 "github.com/cockroachdb/cockroach/pkg/util/timeutil" 26 "github.com/cockroachdb/errors" 27 "github.com/spf13/cobra" 28 ) 29 30 func init() { 31 loadShowCmd := &cobra.Command{ 32 Use: "show <basepath>", 33 Short: "show backups", 34 Long: "Shows information about a SQL backup.", 35 RunE: cli.MaybeDecorateGRPCError(runLoadShow), 36 } 37 38 loadCmds := &cobra.Command{ 39 Use: "load [command]", 40 Short: "loading commands", 41 Long: `Commands for bulk loading external files.`, 42 RunE: func(cmd *cobra.Command, args []string) error { 43 return cmd.Usage() 44 }, 45 } 46 cli.AddCmd(loadCmds) 47 loadCmds.AddCommand(loadShowCmd) 48 } 49 50 func runLoadShow(cmd *cobra.Command, args []string) error { 51 if len(args) != 1 { 52 return errors.New("basepath argument is required") 53 } 54 55 ctx := context.Background() 56 basepath := args[0] 57 if !strings.Contains(basepath, "://") { 58 basepath = cloud.MakeLocalStorageURI(basepath) 59 } 60 61 externalStorageFromURI := func(ctx context.Context, uri string) (cloud.ExternalStorage, error) { 62 return cloud.ExternalStorageFromURI(ctx, uri, base.ExternalIODirConfig{}, 63 cluster.NoSettings, blobs.TestEmptyBlobClientFactory) 64 } 65 // This reads the raw backup descriptor (with table descriptors possibly not 66 // upgraded from the old FK representation, or even older formats). If more 67 // fields are added to the output, the table descriptors may need to be 68 // upgraded. 69 desc, err := backupccl.ReadBackupManifestFromURI(ctx, basepath, externalStorageFromURI, nil) 70 if err != nil { 71 return err 72 } 73 start := timeutil.Unix(0, desc.StartTime.WallTime).Format(time.RFC3339Nano) 74 end := timeutil.Unix(0, desc.EndTime.WallTime).Format(time.RFC3339Nano) 75 fmt.Printf("StartTime: %s (%s)\n", start, desc.StartTime) 76 fmt.Printf("EndTime: %s (%s)\n", end, desc.EndTime) 77 fmt.Printf("DataSize: %d (%s)\n", desc.EntryCounts.DataSize, humanizeutil.IBytes(desc.EntryCounts.DataSize)) 78 fmt.Printf("Rows: %d\n", desc.EntryCounts.Rows) 79 fmt.Printf("IndexEntries: %d\n", desc.EntryCounts.IndexEntries) 80 fmt.Printf("FormatVersion: %d\n", desc.FormatVersion) 81 fmt.Printf("ClusterID: %s\n", desc.ClusterID) 82 fmt.Printf("NodeID: %s\n", desc.NodeID) 83 fmt.Printf("BuildInfo: %s\n", desc.BuildInfo.Short()) 84 fmt.Printf("Spans:\n") 85 for _, s := range desc.Spans { 86 fmt.Printf(" %s\n", s) 87 } 88 fmt.Printf("Files:\n") 89 for _, f := range desc.Files { 90 fmt.Printf(" %s:\n", f.Path) 91 fmt.Printf(" Span: %s\n", f.Span) 92 fmt.Printf(" Sha512: %0128x\n", f.Sha512) 93 fmt.Printf(" DataSize: %d (%s)\n", f.EntryCounts.DataSize, humanizeutil.IBytes(f.EntryCounts.DataSize)) 94 fmt.Printf(" Rows: %d\n", f.EntryCounts.Rows) 95 fmt.Printf(" IndexEntries: %d\n", f.EntryCounts.IndexEntries) 96 } 97 // Note that these descriptors could be from any past version of the cluster, 98 // in case more fields need to be added to the output. 99 fmt.Printf("Descriptors:\n") 100 for _, d := range desc.Descriptors { 101 if desc := d.Table(hlc.Timestamp{}); desc != nil { 102 fmt.Printf(" %d: %s (table)\n", d.GetID(), d.GetName()) 103 } 104 if desc := d.GetDatabase(); desc != nil { 105 fmt.Printf(" %d: %s (database)\n", d.GetID(), d.GetName()) 106 } 107 } 108 return nil 109 }