github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-cluster-bucket-export.go (about) 1 // Copyright (c) 2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "context" 22 "fmt" 23 "io" 24 "os" 25 "path/filepath" 26 "strings" 27 "time" 28 29 "github.com/fatih/color" 30 "github.com/minio/cli" 31 json "github.com/minio/colorjson" 32 "github.com/minio/mc/pkg/probe" 33 "github.com/minio/pkg/v2/console" 34 ) 35 36 var adminClusterBucketExportCmd = cli.Command{ 37 Name: "export", 38 Usage: "backup bucket metadata to a zip file", 39 Action: mainClusterBucketExport, 40 OnUsageError: onUsageError, 41 Before: setGlobalsFromContext, 42 Flags: globalFlags, 43 HideHelpCommand: true, 44 CustomHelpTemplate: `NAME: 45 {{.HelpName}} - {{.Usage}} 46 47 USAGE: 48 {{.HelpName}} [FLAGS] TARGET/[BUCKET] 49 50 FLAGS: 51 {{range .VisibleFlags}}{{.}} 52 {{end}} 53 EXAMPLES: 54 1. Save metadata of all buckets to a zip file. 55 {{.Prompt}} {{.HelpName}} myminio 56 `, 57 } 58 59 func checkBucketExportSyntax(ctx *cli.Context) { 60 if len(ctx.Args()) != 1 { 61 showCommandHelpAndExit(ctx, 1) // last argument is exit code 62 } 63 } 64 65 // mainClusterBucketExport - metadata export command 66 func mainClusterBucketExport(ctx *cli.Context) error { 67 // Check for command syntax 68 checkBucketExportSyntax(ctx) 69 70 // Get the alias parameter from cli 71 args := ctx.Args() 72 aliasedURL := args.Get(0) 73 console.SetColor("File", color.New(color.FgWhite, color.Bold)) 74 75 // Create a new MinIO Admin Client 76 client, err := newAdminClient(aliasedURL) 77 if err != nil { 78 fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.") 79 return nil 80 } 81 82 // Compute bucket and object from the aliased URL 83 aliasedURL = filepath.ToSlash(aliasedURL) 84 aliasedURL = filepath.Clean(aliasedURL) 85 _, bucket := url2Alias(aliasedURL) 86 r, e := client.ExportBucketMetadata(context.Background(), bucket) 87 fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to export bucket metadata.") 88 89 if bucket == "" { 90 bucket = "bucket" 91 } 92 // Create bucket metadata zip file 93 tmpFile, e := os.CreateTemp("", fmt.Sprintf("%s-%s-metadata-", strings.ReplaceAll(aliasedURL, "/", "-"), bucket)) 94 fatalIf(probe.NewError(e), "Unable to download file data.") 95 96 ext := "zip" 97 // Copy zip content to target download file 98 _, e = io.Copy(tmpFile, r) 99 fatalIf(probe.NewError(e), "Unable to download bucket metadata.") 100 // Close everything 101 r.Close() 102 tmpFile.Close() 103 // We use 4 bytes of the 32 bytes to identify the file. 104 downloadPath := fmt.Sprintf("%s-%s-metadata.%s", aliasedURL, bucket, ext) 105 // Create necessary directories. 106 dir := filepath.Dir(downloadPath) 107 if e := os.MkdirAll(dir, 0o755); e != nil { 108 fatalIf(probe.NewError(e).Trace(dir), "Unable to create download directory") 109 } 110 111 fi, e := os.Stat(downloadPath) 112 if e == nil && !fi.IsDir() { 113 e = moveFile(downloadPath, downloadPath+"."+time.Now().Format(dateTimeFormatFilename)) 114 fatalIf(probe.NewError(e), "Unable to create a backup of "+downloadPath) 115 } else { 116 if !os.IsNotExist(e) { 117 fatal(probe.NewError(e), "Unable to download file data") 118 } 119 } 120 fatalIf(probe.NewError(moveFile(tmpFile.Name(), downloadPath)), "Unable to rename downloaded data, file exists at %s", tmpFile.Name()) 121 122 if !globalJSON { 123 console.Infof("Bucket metadata successfully downloaded as %s\n", downloadPath) 124 return nil 125 } 126 v := struct { 127 File string `json:"file"` 128 Key string `json:"key,omitempty"` 129 }{ 130 File: downloadPath, 131 } 132 b, e := json.Marshal(v) 133 fatalIf(probe.NewError(e), "Unable to serialize data") 134 console.Println(string(b)) 135 return nil 136 }