generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Conditonally create a scheduled Fargate task to backup RDS databases to S3 * Fargate was chosen, so that it doesn't rely on the ECS infrastructure to run - meaning we can have RDS backups running even if an ECS Infrastructure hasn't been launched * A CodeBuild project is created, which clones, builds and pushes the dalmatian-sql-backup image to ECR, ready to be used for the backup scheduled task. The Dockerfile for this is stored at https://github.com/dxw/dalmatian-sql-backup. This CodeBuild project is triggered every 24 hours to keep up with any updates. * The backup task will backup all databases (except system databases), and store the SQL dumps into an S3 bucket, which can be configured with the retention time in days.
- Loading branch information
Showing
22 changed files
with
881 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
version: 0.2 | ||
|
||
phases: | ||
pre_build: | ||
commands: | ||
- echo "Build started on $(date)" | ||
- echo "Logging in to Amazon ECR..." | ||
- aws ecr get-login-password --region "$AWS_DEFAULT_REGION" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com" | ||
- | | ||
if [ -n "$DOCKERHUB_USERNAME" ] && [ -n "DOCKERHUB_TOKEN" ]; | ||
then | ||
echo "Logging into Dockerhub ..."; | ||
docker login --username "$DOCKERHUB_USERNAME" --password "$DOCKERHUB_PASSWORD"; | ||
fi; | ||
- echo Building dalmatian-sql-backup docker image ... | ||
- docker build -t dalmatian-sql-backup:latest . | ||
build: | ||
commands: | ||
- echo Adding ECR repo tag... | ||
- docker tag dalmatian-sql-backup:latest "$REPOSITORY_URI:latest" | ||
post_build: | ||
commands: | ||
- echo Pushing the Docker image... | ||
- docker push "$REPOSITORY_URI:latest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/usr/local/bin/ump-to-s3-mysql.sh -b ${s3_bucket_name} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/usr/local/bin/dump-to-s3-postgres.sh -b ${s3_bucket_name} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/bin/bash | ||
|
||
# exit on failures | ||
set -e | ||
set -o pipefail | ||
|
||
usage() { | ||
echo "Usage: $(basename "$0") [OPTIONS]" 1>&2 | ||
echo " -h - help" | ||
echo " -n - CodeBuild project name" | ||
exit 1 | ||
} | ||
|
||
while getopts "n:h" opt; do | ||
case $opt in | ||
n) | ||
PROJECT_NAME=$OPTARG | ||
;; | ||
h) | ||
usage | ||
;; | ||
*) | ||
usage | ||
;; | ||
esac | ||
done | ||
|
||
if [ -z "$PROJECT_NAME" ] | ||
then | ||
usage | ||
fi | ||
|
||
BUILD_ID="$( | ||
aws codebuild start-build \ | ||
--project-name "$PROJECT_NAME" \ | ||
| jq -r '.build.id' | ||
)" | ||
|
||
echo "Triggered $PROJECT_NAME CodeBuild project ($BUILD_ID) ..." | ||
|
||
COMPLETED="" | ||
while [ -z "$COMPLETED" ] | ||
do | ||
sleep 10 | ||
echo "Checking progress of CodeBuild $BUILD_ID ..." | ||
BUILD_PROGRESS="$( | ||
aws codebuild batch-get-builds \ | ||
--ids "$BUILD_ID" \ | ||
)" | ||
COMPLETED="$( | ||
echo "$BUILD_PROGRESS" \ | ||
| jq -r \ | ||
'.builds[0].phases[] | select(.phaseType == "COMPLETED")' | ||
)" | ||
done | ||
echo "CodeBuild $BUILD_ID Completed, checking for failures ..." | ||
|
||
FAILURES="$( | ||
echo "$BUILD_PROGRESS" \ | ||
| jq -r \ | ||
'.builds[0].phases[] | select(.phaseStatus == "FAILED")' | ||
)" | ||
|
||
if [ -n "$FAILURES" ] | ||
then | ||
echo "$FAILURES" | ||
exit 1 | ||
fi | ||
|
||
echo "CodeBuild $BUILD_ID completed without failures" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"codebuild:StartBuild", | ||
"codebuild:StopBuild", | ||
"codebuild:BatchGet*", | ||
"codebuild:Get*", | ||
"codebuild:List*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"secretsmanager:GetSecretValue" | ||
], | ||
"Resource": ${secret_name_arns} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
resource "aws_ecr_repository" "infrastructure_rds_s3_backups" { | ||
count = local.enable_infrastructure_rds_backup_to_s3 ? 1 : 0 | ||
|
||
name = "${local.resource_prefix}-rds-s3-backups" | ||
|
||
#tfsec:ignore:aws-ecr-enforce-immutable-repository | ||
image_tag_mutability = "MUTABLE" | ||
|
||
encryption_configuration { | ||
encryption_type = local.infrastructure_kms_encryption ? "KMS" : "AES256" | ||
kms_key = local.infrastructure_kms_encryption ? aws_kms_key.infrastructure[0].arn : null | ||
} | ||
|
||
image_scanning_configuration { | ||
scan_on_push = true | ||
} | ||
} |
Oops, something went wrong.