-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update RDS postgres restore command #1118
Changes from 2 commits
050a57e
bfb3827
527dcbc
1a3b0c4
ad0e88a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
|
||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
rdserr "github.com/aws/aws-sdk-go/service/rds" | ||
"github.com/hashicorp/go-version" | ||
"github.com/pkg/errors" | ||
|
||
kanister "github.com/kanisterio/kanister/pkg" | ||
|
@@ -167,7 +168,14 @@ func restoreRDSSnapshot(ctx context.Context, namespace, instanceID, snapshotID, | |
} | ||
|
||
dbEndpoint := *descOp.DBInstances[0].Endpoint.Address | ||
if _, err = execDumpCommand(ctx, dbEngine, RestoreAction, namespace, dbEndpoint, username, password, nil, backupArtifactPrefix, backupID, profile); err != nil { | ||
|
||
// get the engine version | ||
dbEngineVersion, err := findRDSDBEngineVersion(ctx, rdsCli, instanceID) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Couldn't find DBInstance Version") | ||
} | ||
|
||
if _, err = execDumpCommand(ctx, dbEngine, RestoreAction, namespace, dbEndpoint, username, password, nil, backupArtifactPrefix, backupID, profile, dbEngineVersion); err != nil { | ||
return nil, errors.Wrapf(err, "Failed to restore RDS from dump. InstanceID=%s", instanceID) | ||
} | ||
|
||
|
@@ -177,11 +185,27 @@ func restoreRDSSnapshot(ctx context.Context, namespace, instanceID, snapshotID, | |
} | ||
|
||
// nolint:unparam | ||
func postgresRestoreCommand(pgHost, username, password string, dbList []string, backupArtifactPrefix, backupID string, profile []byte) ([]string, error) { | ||
func postgresRestoreCommand(pgHost, username, password string, dbList []string, backupArtifactPrefix, backupID string, profile []byte, dbEngineVersion string) ([]string, error) { | ||
replaceCommand := "" | ||
if len(dbList) == 0 { | ||
return nil, errors.New("No database found. Atleast one db needed to connect") | ||
} | ||
|
||
// check if PostgresDB version > 13 | ||
v1, err := version.NewVersion(dbEngineVersion) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Couldn't find DBInstance Version") | ||
} | ||
// Add Constraints | ||
constraints, err := version.NewConstraint(">= 13.0") | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Couldn't add constraint to DBInstance Version") | ||
} | ||
// Verify Constraints | ||
if !constraints.Check(v1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it same as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PrasadG193 Since we have used |
||
replaceCommand = " sed 's/LOCALE/LC_COLLATE/' |" | ||
} | ||
|
||
return []string{ | ||
"bash", | ||
"-o", | ||
|
@@ -191,8 +215,8 @@ func postgresRestoreCommand(pgHost, username, password string, dbList []string, | |
"-c", | ||
fmt.Sprintf(` | ||
export PGHOST=%s | ||
kando location pull --profile '%s' --path "%s" - | gunzip -c -f | psql -q -U "${PGUSER}" %s | ||
`, pgHost, profile, fmt.Sprintf("%s/%s", backupArtifactPrefix, backupID), dbList[0]), | ||
kando location pull --profile '%s' --path "%s" - | gunzip -c -f |%s psql -q -U "${PGUSER}" %s | ||
`, pgHost, profile, fmt.Sprintf("%s/%s", backupArtifactPrefix, backupID), replaceCommand, dbList[0]), | ||
}, nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -208,6 +208,20 @@ func findRDSEndpoint(ctx context.Context, rdsCli *rds.RDS, instanceID string) (s | |||||
return *dbInstance.DBInstances[0].Endpoint.Address, nil | ||||||
} | ||||||
|
||||||
// findRDSEndpoint returns the database engine version | ||||||
akankshakumari393 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
func findRDSDBEngineVersion(ctx context.Context, rdsCli *rds.RDS, instanceID string) (string, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
dbInstance, err := rdsCli.DescribeDBInstances(ctx, instanceID) | ||||||
if err != nil { | ||||||
return "", err | ||||||
} | ||||||
|
||||||
if (len(dbInstance.DBInstances) == 0) || (dbInstance.DBInstances[0].EngineVersion == nil) { | ||||||
return "", errors.Errorf("Received nil engine version") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we change this to |
||||||
} | ||||||
|
||||||
return *dbInstance.DBInstances[0].EngineVersion, nil | ||||||
} | ||||||
|
||||||
func createPostgresSecret(cli kubernetes.Interface, name, namespace, username, password string) error { | ||||||
secret := &v1.Secret{ | ||||||
ObjectMeta: metav1.ObjectMeta{ | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets declare version as const