Skip to content

Commit

Permalink
cross-regions flag implemented & multi-profile support removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Santonastaso committed Nov 13, 2019
1 parent 47b0e18 commit d387859
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
19 changes: 13 additions & 6 deletions actions/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@ package actions

import (
"log"
"strings"
"os"
"time"

"github.com/AltoStack/dynamodump/core"
)

// Table manages the consumer from a given DynamoDB table and a producer
// to a given s3 bucket
func TableBackup(tableName string, batchSize int64, waitPeriod time.Duration, bucket, prefix string, addDate bool, origin string, destination string) {
func TableBackup(tableName string, batchSize int64, waitPeriod time.Duration, bucket, prefix string, addDate, crossRegions bool, dynamoRegion string, s3Region string) {
if addDate {
t := time.Now().UTC()
prefix += "/" + t.Format("2006-01-02-15-04-05")
}

originSplit := strings.Split(origin, "@")
destinationSplit := strings.Split(destination, "@")
if crossRegions {
if dynamoRegion == "" || s3Region == "" {
log.Fatal("Error. Missing fields dynamoRegion or s3Region with cross regions flag enabled")
os.Exit(-1)
}
} else if dynamoRegion != "" || s3Region != "" {
log.Fatal("Error. Cross regions disabled, please either enable it or remove s3-bucket-region and dynamo-table-region flags")
os.Exit(-1)
}

proc := core.NewAwsHelper(originSplit[0], originSplit[1])
dest := core.NewAwsHelper(destinationSplit[0], destinationSplit[1])
proc := core.NewAwsHelper(dynamoRegion)
dest := core.NewAwsHelper(s3Region)

go proc.ChannelToS3(bucket, prefix, 10*1024*1024, dest)

Expand Down
2 changes: 1 addition & 1 deletion actions/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TableRestore(tableName string, batchSize int64, waitPeriod time.Duration, bucket, prefix string, appendToTable bool) {
proc := core.NewAwsHelper("", "")
proc := core.NewAwsHelper("")

// Check if the table exists and has data in it. If so, abort
itemsCount, err := proc.CheckTableEmpty(tableName)
Expand Down
7 changes: 4 additions & 3 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ func init() {
backupCmd.Flags().StringVarP(&s3BucketName, "s3-bucket-name", "b", "", "Name of the S3 bucket where to put the actions. Environment variable: DYN_S3_BUCKET_NAME")
backupCmd.Flags().StringVarP(&s3BucketFolderName, "s3-bucket-folder-name", "f", "", "Path inside the S3 bucket where to put actions. Environment variable: DYN_S3_BUCKET_FOLDER_NAME")
backupCmd.Flags().BoolVarP(&s3DateSuffix, "s3-bucket-folder-name-suffix", "p", false, "Adds an autogenerated suffix folder named using the UTC date in the format YYYY-mm-dd-HH24-MI-SS to the provided S3 folder. Environment variable: DYN_S3_BUCKET_NAME_SUFFIX")
backupCmd.Flags().StringVarP(&origin, "origin", "o", "", "The AWS region and profile of the origin target, in the form of profile@region")
backupCmd.Flags().StringVarP(&destination, "destination", "d", "", "The AWS region and profile of the origin destination target in the form of profile@region")
backupCmd.Flags().BoolVarP(&crossRegions, "cross-regions", "c", false, "Set flag to true to enable cross regions, this will require s3-bucket-region and dynamo-table-region")
backupCmd.Flags().StringVarP(&s3BucketRegion, "s3-bucket-region", "o", "", "AWS region of the s3 Bucket")
backupCmd.Flags().StringVarP(&dynamoTableRegion, "dynamo-table-region", "d", "", "AWS region of the dynamoDB table")
}

var backupCmd = &cobra.Command{
Use: "backup",
Short: "Backup a DynamoDB Table to S3",
Long: `WIP`,
Run: func(cmd *cobra.Command, args []string) {
actions.TableBackup(dynamoTableName, dynamoBatchSize, time.Duration(waitTime)*time.Millisecond, s3BucketName, s3BucketFolderName, s3DateSuffix, origin, destination)
actions.TableBackup(dynamoTableName, dynamoBatchSize, time.Duration(waitTime)*time.Millisecond, s3BucketName, s3BucketFolderName, s3DateSuffix, crossRegions, s3BucketRegion, dynamoTableRegion)
},
}
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ import (
)

var (
crossRegions bool
dynamoTableName string
dynamoBatchSize int64
dynamoAppendRestore bool
dynamoTableRegion string
s3BucketName string
s3BucketFolderName string
s3BucketRegion string
s3DateSuffix bool
waitTime int64
origin string
destination string
)

var rootCmd = &cobra.Command{
Expand Down
6 changes: 3 additions & 3 deletions core/dynamo.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ type AwsHelper struct {

// NewAwsHelper creates a new AwsHelper, initializing an AWS session and a few
// objects like a channel or a DynamoDB client
func NewAwsHelper(profile string, region string) *AwsHelper {
func NewAwsHelper(region string) *AwsHelper {
awsSess, err := session.NewSessionWithOptions(session.Options{
// Specify profile to load for the session's config
Profile: profile,
// Specify profile to load for the session's config - temporarily commented out
//Profile: profile,

// Provide SDK Config options, such as Region.
Config: aws.Config{
Expand Down

0 comments on commit d387859

Please sign in to comment.