add import/export bucket metadata command (#4087)
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
// Copyright (c) 2022 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
json "github.com/minio/colorjson"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
"github.com/minio/pkg/console"
|
||||
)
|
||||
|
||||
var adminClusterBucketExportCmd = cli.Command{
|
||||
Name: "export",
|
||||
Usage: "backup bucket metadata to a zip file",
|
||||
Action: mainClusterBucketExport,
|
||||
OnUsageError: onUsageError,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
HideHelpCommand: true,
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} [FLAGS] TARGET/[BUCKET]
|
||||
|
||||
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.
|
||||
{{.Prompt}} {{.HelpName}} myminio
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
func checkBucketExportSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) != 1 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "export", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
// mainClusterBucketExport - metadata export command
|
||||
func mainClusterBucketExport(ctx *cli.Context) error {
|
||||
// Check for command syntax
|
||||
checkBucketExportSyntax(ctx)
|
||||
|
||||
// Get the alias parameter from cli
|
||||
args := ctx.Args()
|
||||
aliasedURL := args.Get(0)
|
||||
console.SetColor("File", color.New(color.FgWhite, color.Bold))
|
||||
|
||||
// Create a new MinIO Admin Client
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
if err != nil {
|
||||
fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Compute bucket and object from the aliased URL
|
||||
aliasedURL = filepath.ToSlash(aliasedURL)
|
||||
aliasedURL = filepath.Clean(aliasedURL)
|
||||
_, bucket := url2Alias(aliasedURL)
|
||||
|
||||
r, ierr := client.ExportBucketMetadata(context.Background(), bucket)
|
||||
fatalIf(probe.NewError(ierr).Trace(aliasedURL), "Unable to export bucket metadata.")
|
||||
|
||||
if bucket == "" {
|
||||
bucket = "allbuckets"
|
||||
}
|
||||
// Create bucket metadata zip file
|
||||
tmpFile, e := ioutil.TempFile("", fmt.Sprintf("%s-metadata-", bucket))
|
||||
fatalIf(probe.NewError(e), "Unable to download file data.")
|
||||
|
||||
ext := "zip"
|
||||
// Copy zip content to target download file
|
||||
_, e = io.Copy(tmpFile, r)
|
||||
fatalIf(probe.NewError(e), "Unable to download bucket metadata.")
|
||||
|
||||
// Close everything
|
||||
r.Close()
|
||||
tmpFile.Close()
|
||||
|
||||
// We use 4 bytes of the 32 bytes to identify the file.
|
||||
downloadPath := fmt.Sprintf("%s-metadata.%s", bucket, ext)
|
||||
fi, e := os.Stat(downloadPath)
|
||||
if e == nil && !fi.IsDir() {
|
||||
e = moveFile(downloadPath, downloadPath+"."+time.Now().Format(dateTimeFormatFilename))
|
||||
fatalIf(probe.NewError(e), "Unable to create a backup of "+downloadPath)
|
||||
} else {
|
||||
if !os.IsNotExist(e) {
|
||||
fatal(probe.NewError(e), "Unable to download file data")
|
||||
}
|
||||
}
|
||||
|
||||
fatalIf(probe.NewError(moveFile(tmpFile.Name(), downloadPath)), "Unable to rename downloaded data, file exists at %s", tmpFile.Name())
|
||||
|
||||
if !globalJSON {
|
||||
console.Infof("Bucket metadata successfully downloaded as %s\n", downloadPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
v := struct {
|
||||
File string `json:"file"`
|
||||
Key string `json:"key,omitempty"`
|
||||
}{
|
||||
File: downloadPath,
|
||||
}
|
||||
b, e := json.Marshal(v)
|
||||
fatalIf(probe.NewError(e), "Unable to serialize data")
|
||||
console.Println(string(b))
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// Copyright (c) 2022 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/klauspost/compress/zip"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
"github.com/minio/pkg/console"
|
||||
)
|
||||
|
||||
var adminClusterBucketImportCmd = cli.Command{
|
||||
Name: "import",
|
||||
Usage: "restore bucket metadata from a zip file",
|
||||
Action: mainClusterBucketImport,
|
||||
OnUsageError: onUsageError,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
HideHelpCommand: true,
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} [FLAGS] TARGET/[BUCKET] /path/to/bucket-metadata.zip
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
EXAMPLES:
|
||||
1. Set bucket metadata for 'mybucket' from previously exported metadata zip file.
|
||||
{{.Prompt}} {{.HelpName}} myminio/mybucket /tmp/mybucket-metadata.zip
|
||||
|
||||
2. Set bucket metadata for all buckets from previously exported metadata zip file.
|
||||
{{.Prompt}} {{.HelpName}} myminio /tmp/cluster-bucket-metadata.zip
|
||||
`,
|
||||
}
|
||||
|
||||
func checkBucketImportSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) != 2 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "import", 1) // last argument is exit code
|
||||
}
|
||||
}
|
||||
|
||||
// mainClusterBucketImport - bucket metadata import command
|
||||
func mainClusterBucketImport(ctx *cli.Context) error {
|
||||
// Check for command syntax
|
||||
checkBucketImportSyntax(ctx)
|
||||
|
||||
// Get the alias parameter from cli
|
||||
args := ctx.Args()
|
||||
aliasedURL := args.Get(0)
|
||||
var r io.Reader
|
||||
var sz int64
|
||||
f, e := os.Open(args.Get(1))
|
||||
if e != nil {
|
||||
fatalIf(probe.NewError(e).Trace(args...), "Unable to get bucket metadata")
|
||||
}
|
||||
if st, e := f.Stat(); e == nil {
|
||||
sz = st.Size()
|
||||
}
|
||||
defer f.Close()
|
||||
r = f
|
||||
|
||||
_, e = zip.NewReader(r.(io.ReaderAt), sz)
|
||||
fatalIf(probe.NewError(e).Trace(args...), fmt.Sprintf("Unable to read zip file %s", args.Get(1)))
|
||||
|
||||
f, e = os.Open(args.Get(1))
|
||||
fatalIf(probe.NewError(e).Trace(args...), "Unable to get bucket metadata")
|
||||
|
||||
// Create a new MinIO Admin Client
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
if err != nil {
|
||||
fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Compute bucket and object from the aliased URL
|
||||
aliasedURL = filepath.ToSlash(aliasedURL)
|
||||
aliasedURL = filepath.Clean(aliasedURL)
|
||||
_, bucket := url2Alias(aliasedURL)
|
||||
|
||||
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))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2022 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package cmd
|
||||
|
||||
import "github.com/minio/cli"
|
||||
|
||||
var adminClusterBucketSubcommands = []cli.Command{
|
||||
adminClusterBucketImportCmd,
|
||||
adminClusterBucketExportCmd,
|
||||
}
|
||||
|
||||
var adminClusterBucketCmd = cli.Command{
|
||||
Name: "bucket",
|
||||
Usage: "manage bucket metadata on MinIO cluster",
|
||||
Action: mainAdminClusterBucket,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
Subcommands: adminClusterBucketSubcommands,
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
|
||||
// mainAdminClusterBucket is the handle for "mc admin cluster bucket" command.
|
||||
func mainAdminClusterBucket(ctx *cli.Context) error {
|
||||
commandNotFound(ctx, adminClusterBucketSubcommands)
|
||||
return nil
|
||||
// Sub-commands like "export", "import" have their own main.
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2022 MinIO, Inc.
|
||||
//
|
||||
// This file is part of MinIO Object Storage stack
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package cmd
|
||||
|
||||
import "github.com/minio/cli"
|
||||
|
||||
var adminClusterSubcommands = []cli.Command{
|
||||
adminClusterBucketCmd,
|
||||
}
|
||||
|
||||
var adminClusterCmd = cli.Command{
|
||||
Name: "cluster",
|
||||
Usage: "manage MinIO cluster metadata",
|
||||
Action: mainAdminCluster,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
Subcommands: adminClusterSubcommands,
|
||||
HideHelpCommand: true,
|
||||
}
|
||||
|
||||
// mainAdminCluster is the handle for "mc admin cluster" command.
|
||||
func mainAdminCluster(ctx *cli.Context) error {
|
||||
commandNotFound(ctx, adminClusterSubcommands)
|
||||
return nil
|
||||
// Sub-commands like "bucket", "iam" have their own main.
|
||||
}
|
||||
@@ -51,6 +51,7 @@ var adminCmdSubcommands = []cli.Command{
|
||||
adminTopCmd,
|
||||
adminTraceCmd,
|
||||
adminConsoleCmd,
|
||||
adminClusterCmd,
|
||||
}
|
||||
|
||||
var adminCmd = cli.Command{
|
||||
|
||||
@@ -399,6 +399,9 @@ var completeCmds = map[string]complete.Predictor{
|
||||
"/admin/replicate/status": aliasCompleter,
|
||||
"/admin/replicate/remove": aliasCompleter,
|
||||
|
||||
"/admin/cluster/bucket/export": aliasCompleter,
|
||||
"/admin/cluster/bucket/import": aliasCompleter,
|
||||
|
||||
"/alias/set": nil,
|
||||
"/alias/list": aliasCompleter,
|
||||
"/alias/remove": aliasCompleter,
|
||||
|
||||
@@ -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.3.13
|
||||
github.com/minio/madmin-go v1.3.19
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/minio/minio-go/v7 v7.0.27
|
||||
github.com/minio/pkg v1.1.22
|
||||
|
||||
@@ -62,6 +62,7 @@ github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqO
|
||||
github.com/briandowns/spinner v1.18.1 h1:yhQmQtM1zsqFsouh09Bk/jCjd50pC3EOGsh28gLVvwY=
|
||||
github.com/briandowns/spinner v1.18.1/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
|
||||
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=
|
||||
@@ -273,6 +274,7 @@ 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=
|
||||
@@ -343,8 +345,9 @@ 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.13 h1:157u3bFK9qh2EkkqjpJ/bwOw/5KonXUWqhKP3ZczAdY=
|
||||
github.com/minio/madmin-go v1.3.13/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98=
|
||||
github.com/minio/madmin-go v1.3.11/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98=
|
||||
github.com/minio/madmin-go v1.3.19 h1:X/L4MTnDoR1VG1wwkeaQOBIQQohMTp5k8mjKNBbxGkE=
|
||||
github.com/minio/madmin-go v1.3.19/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=
|
||||
|
||||
Reference in New Issue
Block a user