diff --git a/cmd/admin-cluster-bucket-export.go b/cmd/admin-cluster-bucket-export.go index e48e3643..d493e2f4 100644 --- a/cmd/admin-cluster-bucket-export.go +++ b/cmd/admin-cluster-bucket-export.go @@ -51,10 +51,7 @@ FLAGS: {{range .VisibleFlags}}{{.}} {{end}} EXAMPLES: - 1. Download all bucket metadata for a bucket into zip file. - {{.Prompt}} {{.HelpName}} myminio/bucket - - 2. Download metadata of all buckets in cluster into zip file. + 1. Save metadata of all buckets to a zip file. {{.Prompt}} {{.HelpName}} myminio `, } @@ -91,8 +88,9 @@ func mainClusterBucketExport(ctx *cli.Context) error { fatalIf(probe.NewError(ierr).Trace(aliasedURL), "Unable to export bucket metadata.") if bucket == "" { - bucket = "allbuckets" + bucket = "cluster" } + // Create bucket metadata zip file tmpFile, e := ioutil.TempFile("", fmt.Sprintf("%s-metadata-", bucket)) fatalIf(probe.NewError(e), "Unable to download file data.") diff --git a/cmd/admin-cluster-bucket-import.go b/cmd/admin-cluster-bucket-import.go index e6d90d65..a1f884b4 100644 --- a/cmd/admin-cluster-bucket-import.go +++ b/cmd/admin-cluster-bucket-import.go @@ -18,14 +18,19 @@ package cmd import ( + "bytes" "context" "fmt" "io" "os" "path/filepath" + "strings" + "github.com/fatih/color" "github.com/klauspost/compress/zip" "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/madmin-go" "github.com/minio/mc/pkg/probe" "github.com/minio/pkg/console" ) @@ -48,11 +53,8 @@ FLAGS: {{range .VisibleFlags}}{{.}} {{end}} EXAMPLES: - 1. Set bucket metadata for 'mybucket' from previously exported metadata zip file. - {{.Prompt}} {{.HelpName}} myminio/mybucket /backups/mybucket-metadata.zip - - 2. Set bucket metadata for all buckets from previously exported metadata zip file. - {{.Prompt}} {{.HelpName}} myminio /backups/cluster-bucket-metadata.zip + 1. Recover bucket metadata for all buckets from previously saved bucket metadata backup. + {{.Prompt}} {{.HelpName}} myminio /backups/cluster-metadata.zip `, } @@ -66,6 +68,13 @@ func checkBucketImportSyntax(ctx *cli.Context) { func mainClusterBucketImport(ctx *cli.Context) error { // Check for command syntax checkBucketImportSyntax(ctx) + console.SetColor("Name", color.New(color.Bold, color.FgCyan)) + console.SetColor("success", color.New(color.Bold, color.FgGreen)) + console.SetColor("warning", color.New(color.Bold, color.FgYellow)) + console.SetColor("errors", color.New(color.Bold, color.FgRed)) + console.SetColor("statusMsg", color.New(color.Bold, color.FgHiWhite)) + console.SetColor("failCell", color.New(color.FgRed)) + console.SetColor("passCell", color.New(color.FgGreen)) // Get the alias parameter from cli args := ctx.Args() @@ -100,10 +109,123 @@ func mainClusterBucketImport(ctx *cli.Context) error { aliasedURL = filepath.Clean(aliasedURL) _, bucket := url2Alias(aliasedURL) - ierr := client.ImportBucketMetadata(context.Background(), bucket, f) + rpt, ierr := client.ImportBucketMetadata(context.Background(), bucket, f) fatalIf(probe.NewError(ierr).Trace(aliasedURL), "Unable to import bucket metadata.") - if !globalJSON { - console.Infof("Bucket metadata imported to %s from %s\n", bucket, args.Get(1)) - } + printMsg(importMetaMsg{ + BucketMetaImportErrs: rpt, + Status: "success", + URL: aliasedURL, + Op: "import", + }) + return nil } + +type importMetaMsg struct { + madmin.BucketMetaImportErrs + Op string + URL string `json:"url"` + Status string `json:"status"` +} + +func statusTick(s madmin.MetaStatus) string { + switch { + case s.Err != "": + return console.Colorize("failCell", crossTickCell) + case !s.IsSet: + return blankCell + default: + return console.Colorize("passCell", tickCell) + } +} + +func (i importMetaMsg) String() string { + m := i.BucketMetaImportErrs.Buckets + totBuckets := len(m) + totErrs := 0 + for _, st := range m { + if st.ObjectLock.Err != "" || st.Versioning.Err != "" || + st.SSEConfig.Err != "" || st.Tagging.Err != "" || + st.Lifecycle.Err != "" || st.Quota.Err != "" || + st.Policy.Err != "" || st.Notification.Err != "" { + totErrs++ + } + } + var b strings.Builder + numSch := "success" + if totErrs > 0 { + numSch = "warning" + } + msg := "\n" + console.Colorize(numSch, totBuckets-totErrs) + + console.Colorize("statusMsg", "/") + + console.Colorize("success", totBuckets) + + console.Colorize("statusMsg", " buckets were imported successfully.") + fmt.Fprintln(&b, msg) + if totErrs > 0 { + fmt.Fprintln(&b, console.Colorize("errors", "Errors: \n")) + for bucket, st := range m { + if st.ObjectLock.Err != "" || st.Versioning.Err != "" || + st.SSEConfig.Err != "" || st.Tagging.Err != "" || + st.Lifecycle.Err != "" || st.Quota.Err != "" || + st.Policy.Err != "" || st.Notification.Err != "" { + fmt.Fprintln(&b, printImportErrs(bucket, st)) + } + } + } + return b.String() +} + +func (i importMetaMsg) JSON() string { + i.Status = "success" + buf := &bytes.Buffer{} + enc := json.NewEncoder(buf) + enc.SetIndent("", " ") + // Disable escaping special chars to display XML tags correctly + enc.SetEscapeHTML(false) + + fatalIf(probe.NewError(enc.Encode(i.BucketMetaImportErrs.Buckets)), "Unable to marshal into JSON.") + return buf.String() +} + +// pretty print import errors +func printImportErrs(bucket string, r madmin.BucketStatus) string { + var b strings.Builder + placeHolder := "" + key := fmt.Sprintf("%-10s: %s", "Name", bucket) + fmt.Fprintln(&b, console.Colorize("Name", key)) + + if r.ObjectLock.IsSet { + fmt.Fprintf(&b, "%2s%s %s", placeHolder, "Object lock: ", statusTick(r.ObjectLock)) + fmt.Fprintln(&b) + } + if r.Versioning.IsSet { + fmt.Fprintf(&b, "%2s%s %s", placeHolder, "Versioning: ", statusTick(r.Versioning)) + fmt.Fprintln(&b) + } + + if r.SSEConfig.IsSet { + fmt.Fprintf(&b, "%2s%s %s", placeHolder, "Encryption: ", statusTick(r.SSEConfig)) + fmt.Fprintln(&b) + } + if r.Lifecycle.IsSet { + fmt.Fprintf(&b, "%2s%s %s", placeHolder, "Lifecycle: ", statusTick(r.Lifecycle)) + fmt.Fprintln(&b) + } + if r.Notification.IsSet { + fmt.Fprintf(&b, "%2s%s %s", placeHolder, "Notification: ", statusTick(r.Notification)) + fmt.Fprintln(&b) + } + if r.Quota.IsSet { + fmt.Fprintf(&b, "%2s%s %s", placeHolder, "Quota: ", statusTick(r.Quota)) + fmt.Fprintln(&b) + } + if r.Policy.IsSet { + fmt.Fprintf(&b, "%2s%s %s", placeHolder, "Policy: ", statusTick(r.Policy)) + fmt.Fprintln(&b) + } + if r.Tagging.IsSet { + fmt.Fprintf(&b, "%2s%s %s", placeHolder, "Tagging: ", statusTick(r.Tagging)) + fmt.Fprintln(&b) + } + return b.String() +} diff --git a/go.mod b/go.mod index 745ea580..0bf75f9b 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/minio/cli v1.22.0 github.com/minio/colorjson v1.0.2 github.com/minio/filepath v1.0.0 - github.com/minio/madmin-go v1.4.9 + github.com/minio/madmin-go v1.4.12 github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/minio-go/v7 v7.0.31 github.com/minio/pkg v1.2.0 diff --git a/go.sum b/go.sum index 912cbc3d..a0a2365b 100644 --- a/go.sum +++ b/go.sum @@ -60,7 +60,6 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= @@ -272,7 +271,6 @@ github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= -github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s= github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4= github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= @@ -342,9 +340,8 @@ github.com/minio/colorjson v1.0.2/go.mod h1:JWxcL2n8T8JVf+NY6awl6kn5nK49aAzHOeQE github.com/minio/filepath v1.0.0 h1:fvkJu1+6X+ECRA6G3+JJETj4QeAYO9sV43I79H8ubDY= github.com/minio/filepath v1.0.0/go.mod h1:/nRZA2ldl5z6jT9/KQuvZcQlxZIMQoFFQPvEXx9T/Bw= github.com/minio/madmin-go v1.3.5/go.mod h1:vGKGboQgGIWx4DuDUaXixjlIEZOCIp6ivJkQoiVaACc= -github.com/minio/madmin-go v1.3.11/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98= -github.com/minio/madmin-go v1.4.9 h1:kFJhzWlDomaK1l2RT6yMRVDTYBUTcfn7sT5c8PCM/3Q= -github.com/minio/madmin-go v1.4.9/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98= +github.com/minio/madmin-go v1.4.12 h1:2jr4QLoJOXkwxpPPSbriopZJ3ExyO4TwF/j57pE6dVQ= +github.com/minio/madmin-go v1.4.12/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98= github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=