diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index a96ef387..9930ec2f 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -291,12 +291,13 @@ var completeCmds = map[string]complete.Predictor{ "/share/list": nil, "/share/upload": s3Completer, - "/ilm/ls": s3Complete{deepLevel: 2}, - "/ilm/add": s3Complete{deepLevel: 2}, - "/ilm/edit": s3Complete{deepLevel: 2}, - "/ilm/rm": s3Complete{deepLevel: 2}, - "/ilm/export": s3Complete{deepLevel: 2}, - "/ilm/import": s3Complete{deepLevel: 2}, + "/ilm/ls": s3Complete{deepLevel: 2}, + "/ilm/add": s3Complete{deepLevel: 2}, + "/ilm/edit": s3Complete{deepLevel: 2}, + "/ilm/rm": s3Complete{deepLevel: 2}, + "/ilm/export": s3Complete{deepLevel: 2}, + "/ilm/import": s3Complete{deepLevel: 2}, + "/ilm/restore": s3Completer, "/undo": s3Completer, diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 3bbb95b0..ea3c30d8 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -1229,3 +1229,11 @@ func (f *fsClient) GetBucketInfo(ctx context.Context) (BucketInfo, *probe.Error) APIType: "filesystem", }) } + +// Restore object - not implemented +func (f *fsClient) Restore(_ context.Context, _ string, _ int) *probe.Error { + return probe.NewError(APINotImplemented{ + API: "Restore", + APIType: "filesystem", + }) +} diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 4b118680..245be20d 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -1960,6 +1960,7 @@ func (c *S3Client) objectInfo2ClientContent(bucket string, entry minio.ObjectInf content.StorageClass = entry.StorageClass content.IsDeleteMarker = entry.IsDeleteMarker content.IsLatest = entry.IsLatest + content.Restore = entry.Restore content.Metadata = map[string]string{} content.UserMetadata = map[string]string{} content.ReplicationStatus = entry.ReplicationStatus @@ -2625,3 +2626,22 @@ func (c *S3Client) GetBucketInfo(ctx context.Context) (BucketInfo, *probe.Error) } return b, nil } + +// Restore gets a copy of an archived object +func (c *S3Client) Restore(ctx context.Context, versionID string, days int) *probe.Error { + bucket, object := c.url2BucketAndObject() + if bucket == "" { + return probe.NewError(BucketNameEmpty{}) + } + if object == "" { + return probe.NewError(ObjectNameEmpty{}) + } + + req := minio.RestoreRequest{} + req.SetDays(days) + req.SetGlacierJobParameters(minio.GlacierJobParameters{Tier: minio.TierExpedited}) + if err := c.api.RestoreObject(ctx, bucket, object, versionID, req); err != nil { + return probe.NewError(err) + } + return nil +} diff --git a/cmd/client.go b/cmd/client.go index 57fa6198..0b63cbf3 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -167,6 +167,9 @@ type Client interface { DeleteEncryption(ctx context.Context) *probe.Error // Bucket info operation GetBucketInfo(ctx context.Context) (BucketInfo, *probe.Error) + + // Restore an object + Restore(ctx context.Context, versionID string, days int) *probe.Error } // ClientContent - Content container for content metadata @@ -194,7 +197,10 @@ type ClientContent struct { IsDeleteMarker bool IsLatest bool ReplicationStatus string - Err *probe.Error + + Restore *minio.RestoreInfo + + Err *probe.Error } // Config - see http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html diff --git a/cmd/ilm-main.go b/cmd/ilm-main.go index eff1b7e1..38fb2d83 100644 --- a/cmd/ilm-main.go +++ b/cmd/ilm-main.go @@ -30,6 +30,7 @@ var ilmSubcommands = []cli.Command{ ilmRmCmd, ilmExportCmd, ilmImportCmd, + ilmRestoreCmd, } var ilmCmd = cli.Command{ diff --git a/cmd/ilm-restore.go b/cmd/ilm-restore.go new file mode 100644 index 00000000..0fcb6bae --- /dev/null +++ b/cmd/ilm-restore.go @@ -0,0 +1,325 @@ +// Copyright (c) 2015-2021 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 . + +package cmd + +import ( + "bytes" + "context" + "fmt" + "time" + + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/mc/pkg/probe" +) + +// ilm restore specific flags. +var ( + ilmRestoreFlags = []cli.Flag{ + cli.IntFlag{ + Name: "days", + Value: 1, + Usage: "keep the restored copy for N days", + }, + cli.BoolFlag{ + Name: "recursive, r", + Usage: "apply recursively", + }, + cli.BoolFlag{ + Name: "versions", + Usage: "apply on versions", + }, + cli.StringFlag{ + Name: "version-id, vid", + Usage: "select a specific version id", + }, + } +) + +var ilmRestoreCmd = cli.Command{ + Name: "restore", + Usage: "restore archived objects", + Action: mainILMRestore, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(ilmRestoreFlags, globalFlags...), + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +DESCRIPTION: + Create a restored copy of one or more archived objects. + +EXAMPLES: + 1. Restore one specific object + {{.Prompt}} {{.HelpName}} myminio/mybucket/path/to/object + + 2. Restore a specific object version + {{.Prompt}} {{.HelpName}} --vid "CL3sWgdSN2pNntSf6UnZAuh2kcu8E8si" myminio/mybucket/path/to/object + + 3. Restore all objects under a specific prefix + {{.Prompt}} {{.HelpName}} --recursive myminio/mybucket/dir/ + + 4. Restore all objects with all versions under a specific prefix + {{.Prompt}} {{.HelpName}} --recursive --versions myminio/mybucket/dir/ + +`, +} + +// checkILMRestoreSyntax - validate arguments passed by user +func checkILMRestoreSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "restore", globalErrorExitStatus) + } + + if ctx.Int("days") <= 0 { + fatalIf(errDummy().Trace(), "--days should be equal or greater than 1") + } + + if ctx.Bool("version-id") && (ctx.Bool("recursive") || ctx.Bool("versions")) { + fatalIf(errDummy().Trace(), "You cannot combine --version-id with --recursive or --versions flags.") + } +} + +// Send Restore S3 API +func restoreObject(ctx context.Context, targetAlias, targetURL, versionID string, days int) *probe.Error { + clnt, err := newClientFromAlias(targetAlias, targetURL) + if err != nil { + return err + } + + return clnt.Restore(ctx, versionID, days) +} + +// Send restore S3 API request to one or more objects depending on the arguments +func sendRestoreRequests(ctx context.Context, targetAlias, targetURL, targetVersionID string, recursive, applyOnVersions bool, days int, restoreSentReq chan *probe.Error) { + defer close(restoreSentReq) + + client, err := newClientFromAlias(targetAlias, targetURL) + if err != nil { + restoreSentReq <- err + return + } + + if !recursive { + err := restoreObject(ctx, targetAlias, targetURL, targetVersionID, days) + restoreSentReq <- err + return + } + + prev := "" + for content := range client.List(ctx, ListOptions{ + Recursive: true, + WithOlderVersions: applyOnVersions, + ShowDir: DirNone, + }) { + if content.Err != nil { + errorIf(content.Err.Trace(client.GetURL().String()), "Unable to list folder.") + continue + } + err := restoreObject(ctx, targetAlias, content.URL.String(), content.VersionID, days) + if err != nil { + restoreSentReq <- err + continue + } + // Avoid sending the status of each separate version + // of the same object name. + if prev != content.URL.String() { + prev = content.URL.String() + restoreSentReq <- nil + } + } +} + +// Wait until an object which receives restore request is completely restored in the fast tier +func waitRestoreObject(ctx context.Context, targetAlias, targetURL, versionID string) *probe.Error { + clnt, err := newClientFromAlias(targetAlias, targetURL) + if err != nil { + return err + } + + for { + opts := StatOptions{versionID: versionID} + st, err := clnt.Stat(ctx, opts) + if err != nil { + return err + } + if st.Restore == nil { + return probe.NewError(fmt.Errorf("`%s` did not receive restore request", targetURL)) + } + if st.Restore != nil && !st.Restore.OngoingRestore { + return nil + } + // Restore still going on, wait for 5 seconds before checking again + time.Sleep(5 * time.Second) + } +} + +// Check and wait the restore status of one or more objects one by one. +func checkRestoreStatus(ctx context.Context, targetAlias, targetURL, targetVersionID string, recursive, applyOnVersions bool, restoreStatus chan *probe.Error) { + defer close(restoreStatus) + + client, err := newClientFromAlias(targetAlias, targetURL) + if err != nil { + restoreStatus <- err + return + } + + if !recursive { + restoreStatus <- waitRestoreObject(ctx, targetAlias, targetURL, targetVersionID) + return + } + + prev := "" + for content := range client.List(ctx, ListOptions{ + Recursive: true, + WithOlderVersions: applyOnVersions, + ShowDir: DirNone, + }) { + if content.Err != nil { + restoreStatus <- content.Err + continue + } + + err := waitRestoreObject(ctx, targetAlias, content.URL.String(), content.VersionID) + if err != nil { + restoreStatus <- err + continue + } + + if prev != content.URL.String() { + prev = content.URL.String() + restoreStatus <- nil + } + } +} + +var dotCycle = 0 + +// Clear and print text in the same line +func printStatus(msg string, args ...interface{}) { + if globalJSON { + return + } + + dotCycle++ + dots := bytes.Repeat([]byte{'.'}, dotCycle%3+1) + fmt.Print("\n\033[1A\033[K") + fmt.Printf(msg+string(dots), args...) +} + +// Receive restore request & restore finished status and print in the console +func showRestoreStatus(restoreReqStatus, restoreFinishedStatus chan *probe.Error, doneCh chan struct{}) { + var sent, finished int + var done bool + + ticker := time.NewTicker(1 * time.Second) + defer ticker.Stop() + + for !done { + select { + case err, ok := <-restoreReqStatus: + if !ok { + done = true + break + } + errorIf(err.Trace(), "Unable to send restore request.") + if err == nil { + sent++ + } + case <-ticker.C: + } + + printStatus("Sent restore requests to %d object(s)", sent) + } + + if !globalJSON { + fmt.Println("") + } + + done = false + + for !done { + select { + case err, ok := <-restoreFinishedStatus: + if !ok { + done = true + break + } + errorIf(err.Trace(), "Unable to check for restore status") + if err == nil { + finished++ + } + case <-ticker.C: + } + printStatus("%d/%d object(s) successfully restored", finished, sent) + } + + if !globalJSON { + fmt.Println("") + } else { + type ilmRestore struct { + Status string `json:"status"` + Restored int `json:"restored"` + } + + msgBytes, _ := json.Marshal(ilmRestore{Status: "success", Restored: sent}) + fmt.Println(string(msgBytes)) + } + + close(doneCh) +} + +func mainILMRestore(cliCtx *cli.Context) (cErr error) { + ctx, cancelILMRestore := context.WithCancel(globalContext) + defer cancelILMRestore() + + checkILMRestoreSyntax(cliCtx) + + args := cliCtx.Args() + aliasedURL := args.Get(0) + + versionID := cliCtx.String("version-id") + recursive := cliCtx.Bool("recursive") + includeVersions := cliCtx.Bool("versions") + days := cliCtx.Int("days") + + targetAlias, targetURL, _ := mustExpandAlias(aliasedURL) + if targetAlias == "" { + fatalIf(errDummy().Trace(), "Unable to restore the given URL") + } + + var restoreReqStatus = make(chan *probe.Error) + var restoreStatus = make(chan *probe.Error) + + var done = make(chan struct{}) + + go func() { + showRestoreStatus(restoreReqStatus, restoreStatus, done) + }() + + sendRestoreRequests(ctx, targetAlias, targetURL, versionID, recursive, includeVersions, days, restoreReqStatus) + checkRestoreStatus(ctx, targetAlias, targetURL, versionID, recursive, includeVersions, restoreStatus) + + // Wait until the UI printed all the status + <-done + + return nil +}