Skip to content

Commit 050e495

Browse files
authored
Merge pull request #184 from YAPP-Github/fix/backup
[Fix] dev 환경 db 백업 문제 수정
2 parents 903ceb6 + 34ee883 commit 050e495

File tree

7 files changed

+58
-7
lines changed

7 files changed

+58
-7
lines changed

terraform/common/locals.tf

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@ locals {
3232
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
3333
]
3434
custom_inline_policies = {
35-
ssm_mysql_url_access = {
36-
name = "ssm-mysql-url-access"
37-
description = "Allow reading MySQL URL parameter from SSM"
35+
ssm_mysql_params_access = {
36+
name = "ssm-mysql-params-access"
37+
description = "Allow reading MySQL params from SSM"
3838
policy_document = {
3939
Version = "2012-10-17"
4040
Statement = [
4141
{
42-
Effect = "Allow"
43-
Action = ["ssm:GetParameter"]
44-
Resource = "arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/dev/MYSQL_URL"
42+
Effect = "Allow"
43+
Action = ["ssm:GetParameter", "ssm:GetParametersByPath"]
44+
Resource = [
45+
"arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/dev/MYSQL_URL",
46+
"arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/dev/MYSQL_USER_NAME",
47+
"arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/dev/MYSQL_PASSWORD"
48+
]
4549
}
4650
]
4751
}

terraform/dev/outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ output "cloudfront_domain_name" {
2828
value = module.s3.cloudfront_domain_name
2929
sensitive = true
3030
}
31+
32+
output "s3_bucket_id" {
33+
value = module.s3.s3_bucket_id
34+
}
35+
36+
output "s3_bucket_arn" {
37+
value = module.s3.s3_bucket_arn
38+
}

terraform/dev/s3/outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@ output "cloudfront_domain_name" {
22
value = aws_cloudfront_distribution.dev_cdn.domain_name
33
sensitive = true
44
}
5+
6+
output "s3_bucket_id" {
7+
value = aws_s3_bucket.dev.id
8+
}
9+
10+
output "s3_bucket_arn" {
11+
value = aws_s3_bucket.dev.arn
12+
}

terraform/dev/s3/scripts/mysql-backup.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ HOST=$(echo "$MYSQL_URL" | sed -E 's|jdbc:mysql://([^:/]+):([0-9]+)/([^?]+).*|\1
1414
PORT=$(echo "$MYSQL_URL" | sed -E 's|jdbc:mysql://([^:/]+):([0-9]+)/([^?]+).*|\2|')
1515
DB_NAME=$(echo "$MYSQL_URL" | sed -E 's|jdbc:mysql://([^:/]+):([0-9]+)/([^?]+).*|\3|')
1616

17-
if mysqldump -h "$HOST" -P "$PORT" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$DB_NAME" > "$ARCHIVE_PATH"; then
17+
retries=10
18+
count=0
19+
until mysqladmin ping -h "$HOST" -P "$PORT" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" --silent || [ $count -eq $retries ]; do
20+
echo "[INFO] Waiting for MySQL to be ready... ($count/$retries)"
21+
sleep 2
22+
count=$((count+1))
23+
done
24+
25+
if mysqldump --no-tablespaces -h "$HOST" -P "$PORT" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$DB_NAME" > "$ARCHIVE_PATH"; then
1826
echo "[INFO] MySQL backup successful: $ARCHIVE_PATH"
1927

2028
if aws s3 cp "$ARCHIVE_PATH" "$S3_BUCKET"; then

terraform/dev/scripts/user-data.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ until systemctl is-active --quiet crond; do
2121
sleep 1
2222
done
2323

24+
sudo chown -R ec2-user:ec2-user /home/ec2-user/mysql
25+
26+
sudo dnf install -y mariadb105
27+
2428
(
2529
sudo crontab -u ec2-user -l 2>/dev/null || true
2630
echo "0 0 * * 0 /home/ec2-user/scripts/app-backup-dev-logs.sh >> /home/ec2-user/logs/backup/app-backup.log 2>&1"

terraform/prod/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ output "cloudfront_domain_name" {
3030
value = module.s3.cloudfront_domain_name
3131
sensitive = true
3232
}
33+
34+
output "rds_instance_identifier" {
35+
description = "The identifier of the production RDS instance."
36+
value = module.rds.rds_instance_identifier
37+
}
38+
39+
output "rds_instance_id" {
40+
value = module.rds.rds_instance_id
41+
sensitive = true
42+
}

terraform/prod/rds/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@ output "arn" {
77
description = "RDS instance ARN"
88
value = aws_db_instance.prod.arn
99
}
10+
11+
output "rds_instance_identifier" {
12+
description = "The identifier of the production RDS instance."
13+
value = aws_db_instance.prod.identifier
14+
}
15+
16+
output "rds_instance_id" {
17+
value = aws_db_instance.prod.id
18+
}

0 commit comments

Comments
 (0)