From c2bf38f647f0f52c362099d97a837d5b76ff933f Mon Sep 17 00:00:00 2001 From: dorman <37854724+dormanze@users.noreply.github.com> Date: Sun, 3 Mar 2024 03:54:51 +0800 Subject: [PATCH] mirror: add bucket exclusions (#4861) --- cmd/mirror-main.go | 32 +++++++++++++++++++++------- cmd/mirror-url.go | 52 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 3b6da8da..ece6429b 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -101,6 +101,10 @@ var ( Name: "exclude", Usage: "exclude object(s) that match specified object name pattern", }, + cli.StringSliceFlag{ + Name: "exclude-bucket", + Usage: "exclude bucket(s) that match specified bucket name pattern", + }, cli.StringSliceFlag{ Name: "exclude-storageclass", Usage: "exclude object(s) that match the specified storage class", @@ -192,26 +196,30 @@ EXAMPLES: Exclude all .* files and *.temp files when mirroring. {{.Prompt}} {{.HelpName}} --exclude ".*" --exclude "*.temp" s3/test ~/test - 10. Mirror objects newer than 10 days from bucket test to a local folder. + 10. Mirror all buckets from aliased Amazon S3 cloud storage to a local folder. + Exclude test* buckets and backup* buckets when mirroring. + {{.Prompt}} {{.HelpName}} --exclude-bucket 'test*' --exclude 'backup*' s3 ~/test + + 11. Mirror objects newer than 10 days from bucket test to a local folder. {{.Prompt}} {{.HelpName}} --newer-than 10d s3/test ~/localfolder - 11. Mirror objects older than 30 days from Amazon S3 bucket test to a local folder. + 12. Mirror objects older than 30 days from Amazon S3 bucket test to a local folder. {{.Prompt}} {{.HelpName}} --older-than 30d s3/test ~/test - 12. Mirror server encrypted objects from MinIO cloud storage to a bucket on Amazon S3 cloud storage + 13. Mirror server encrypted objects from MinIO cloud storage to a bucket on Amazon S3 cloud storage {{.Prompt}} {{.HelpName}} --encrypt-key "minio/photos=32byteslongsecretkeymustbegiven1,s3/archive=32byteslongsecretkeymustbegiven2" minio/photos/ s3/archive/ - 13. Mirror server encrypted objects from MinIO cloud storage to a bucket on Amazon S3 cloud storage. In case the encryption key contains + 14. Mirror server encrypted objects from MinIO cloud storage to a bucket on Amazon S3 cloud storage. In case the encryption key contains non-printable character like tab, pass the base64 encoded string as key. {{.Prompt}} {{.HelpName}} --encrypt-key "s3/photos/=32byteslongsecretkeymustbegiven1,play/archive/=MzJieXRlc2xvbmdzZWNyZXRrZQltdXN0YmVnaXZlbjE=" s3/photos/ play/archive/ - 14. Update 'Cache-Control' header on all existing objects recursively. + 15. Update 'Cache-Control' header on all existing objects recursively. {{.Prompt}} {{.HelpName}} --attr "Cache-Control=max-age=90000,min-fresh=9000" myminio/video-files myminio/video-files - 15. Mirror a local folder recursively to Amazon S3 cloud storage and preserve all local file attributes. + 16. Mirror a local folder recursively to Amazon S3 cloud storage and preserve all local file attributes. {{.Prompt}} {{.HelpName}} -a backup/ s3/archive - 16. Cross mirror between sites in a active-active deployment. + 17. Cross mirror between sites in a active-active deployment. Site-A: {{.Prompt}} {{.HelpName}} --active-active siteA siteB Site-B: {{.Prompt}} {{.HelpName}} --active-active siteB siteA `, @@ -631,6 +639,10 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo) if matchExcludeOptions(mj.opts.excludeOptions, sourceSuffix, sourceURL.Type) { continue } + // Skip the bucket, if it matches the Exclude options provided + if matchExcludeBucketOptions(mj.opts.excludeBuckets, sourceSuffix) { + continue + } sc, ok := event.UserMetadata["x-amz-storage-class"] if ok { @@ -967,6 +979,7 @@ func runMirror(ctx context.Context, srcURL, dstURL string, cli *cli.Context, enc disableMultipart: cli.Bool("disable-multipart"), skipErrors: cli.Bool("skip-errors"), excludeOptions: cli.StringSlice("exclude"), + excludeBuckets: cli.StringSlice("exclude-bucket"), excludeStorageClasses: cli.StringSlice("exclude-storageclass"), olderThan: cli.String("older-than"), newerThan: cli.String("newer-than"), @@ -1038,6 +1051,11 @@ func runMirror(ctx context.Context, srcURL, dstURL string, cli *cli.Context, enc continue } + // Skip create bucket, if it matches the Exclude options provided + if matchExcludeBucketOptions(mopts.excludeBuckets, sourceSuffix) { + continue + } + // Bucket only exists in the source, create the same bucket in the destination if err := newDstClt.MakeBucket(ctx, cli.String("region"), false, withLock); err != nil { errorIf(err, "Unable to create bucket at `"+newTgtURL+"`.") diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index 4e43bb41..14194367 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -106,6 +106,26 @@ func matchExcludeOptions(excludeOptions []string, srcSuffix string, typ ClientUR return false } +func matchExcludeBucketOptions(excludeBuckets []string, srcSuffix string) bool { + if strings.HasPrefix(srcSuffix, "/") { + srcSuffix = srcSuffix[1:] + } else if runtime.GOOS == "windows" && strings.HasPrefix(srcSuffix, `\`) { + srcSuffix = srcSuffix[1:] + } + var bucketName string + if runtime.GOOS == "windows" { + bucketName = strings.Split(srcSuffix, `\`)[0] + } else { + bucketName = strings.Split(srcSuffix, "/")[0] + } + for _, pattern := range excludeBuckets { + if wildcard.Match(pattern, bucketName) { + return true + } + } + return false +} + func deltaSourceTarget(ctx context.Context, sourceURL, targetURL string, opts mirrorOptions, URLsCh chan<- URLs) { // source and targets are always directories sourceSeparator := string(newClientURL(sourceURL).Separator) @@ -158,12 +178,22 @@ func deltaSourceTarget(ctx context.Context, sourceURL, targetURL string, opts mi continue } + // Skip the source bucket if it matches the Exclude options provided + if matchExcludeBucketOptions(opts.excludeBuckets, srcSuffix) { + continue + } + tgtSuffix := strings.TrimPrefix(diffMsg.SecondURL, targetURL) // Skip the target object if it matches the Exclude options provided if matchExcludeOptions(opts.excludeOptions, tgtSuffix, newClientURL(targetURL).Type) { continue } + // Skip the target bucket if it matches the Exclude options provided + if matchExcludeBucketOptions(opts.excludeBuckets, tgtSuffix) { + continue + } + if diffMsg.firstContent != nil { var found bool for _, esc := range opts.excludeStorageClasses { @@ -233,17 +263,17 @@ func deltaSourceTarget(ctx context.Context, sourceURL, targetURL string, opts mi } type mirrorOptions struct { - isFake, isOverwrite, activeActive bool - isWatch, isRemove, isMetadata bool - isRetriable bool - isSummary bool - skipErrors bool - excludeOptions, excludeStorageClasses []string - encKeyDB map[string][]prefixSSEPair - md5, disableMultipart bool - olderThan, newerThan string - storageClass string - userMetadata map[string]string + isFake, isOverwrite, activeActive bool + isWatch, isRemove, isMetadata bool + isRetriable bool + isSummary bool + skipErrors bool + excludeOptions, excludeStorageClasses, excludeBuckets []string + encKeyDB map[string][]prefixSSEPair + md5, disableMultipart bool + olderThan, newerThan string + storageClass string + userMetadata map[string]string } // Prepares urls that need to be copied or removed based on requested options.