Skip to content

Backup restore functionality added for different versions #14

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

Open
wants to merge 59 commits into
base: release-1.1.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
f34b6e3
upgrade backup-restore functionality
divyanshujainSquareops Jun 20, 2024
41f3a28
update backup-restore feature
divyanshujainSquareops Jun 24, 2024
fc2d643
update backup-restore feature
divyanshujainSquareops Jun 24, 2024
bbda84b
update backup-restore feature
divyanshujainSquareops Jun 24, 2024
6b8d721
update backup-restore feature
divyanshujainSquareops Jun 24, 2024
a36032e
Delete modules/db-backup-restore/backup/.helmignore
divyanshujainSquareops Jun 24, 2024
3376919
Delete modules/db-backup-restore/restore/.helmignore
divyanshujainSquareops Jun 24, 2024
8d81967
update backup-restore feature
divyanshujainSquareops Jun 24, 2024
4e1028f
update backup-restore feature
divyanshujainSquareops Jun 24, 2024
004d9ae
update backup-restore feature
divyanshujainSquareops Jun 24, 2024
2cf312e
update backup-restore feature
divyanshujainSquareops Jun 26, 2024
9e7dfb4
Update main.tf
divyanshujainSquareops Jun 26, 2024
1b0d849
Updated terraform aws privider version (#13)
ankush-sqops Aug 30, 2024
43af34b
added backup-restore functionality
Nov 14, 2024
df03298
Update main.tf
amanravi-squareops Nov 14, 2024
fc0a2d1
Merge branch 'squareops:main' into main
divyanshujainSquareops Nov 15, 2024
2625bc8
Merge pull request #1 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Nov 15, 2024
9ab3d73
update module location
divyanshujainSquareops Nov 15, 2024
ad5cba4
updated readme file
Nov 15, 2024
3ccfa4e
Merge branch 'divyanshujainSquareops:main' into release-1.1.3
amanravi-squareops Nov 15, 2024
4b1d355
Update README.md
amanravi-squareops Nov 15, 2024
6a98434
Merge pull request #2 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Nov 18, 2024
8722dbe
updated the repo
Nov 18, 2024
14829f0
Merge branch 'divyanshujainSquareops:main' into release-1.1.3
amanravi-squareops Nov 18, 2024
e429c8c
updated the repo
Nov 18, 2024
22dc814
Merge branch 'release-1.1.3' of https://github.com/amanravi-squareops…
Nov 18, 2024
e9f34cd
updated readme file
Nov 18, 2024
b563e9e
updated readme file
Nov 18, 2024
1ff5b73
updated readme file
Nov 18, 2024
db5ee98
Merge pull request #3 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Nov 18, 2024
70b0ff2
Update main.tf
divyanshujainSquareops Nov 18, 2024
79a953b
Update main.tf
divyanshujainSquareops Nov 18, 2024
9487cdd
added backup restore functionality and fixed security group
Nov 27, 2024
ecc6036
Merge pull request #4 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Nov 27, 2024
3956204
Update README.md
divyanshujainSquareops Nov 27, 2024
609ddc8
updated readme file
divyanshujainSquareops Nov 27, 2024
cce7d35
Update main.tf
divyanshujainSquareops Nov 27, 2024
51ed115
Merge pull request #15 from divyanshujainSquareops/main
ankush-sqops Nov 27, 2024
5a614ec
added path module in the backup main.tf
amanravi-squareops Jan 7, 2025
914e03b
Update outputs.tf
amanravi-squareops Jan 7, 2025
3cb293c
Update output.tf
amanravi-squareops Jan 7, 2025
3ef1675
Update main.tf
amanravi-squareops Jan 7, 2025
dc6c6a7
Merge pull request #5 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Jan 7, 2025
69e373f
Merge pull request #17 from divyanshujainSquareops/main
ankush-sqops Jan 7, 2025
aa63d18
fixed restore database
Feb 7, 2025
a8b75b3
run lint command
Feb 7, 2025
f671e2e
Merge pull request #6 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Feb 7, 2025
7588fb3
Merge pull request #18 from divyanshujainSquareops/main
ns-squareops Mar 4, 2025
a728d7f
Merge pull request #19 from squareops/release-2.0.0
ns-squareops Mar 4, 2025
ad853a5
added path module in the backup & restore module
amanravi-squareops Mar 12, 2025
ec5990f
Merge pull request #7 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Mar 13, 2025
e6777a1
changed the release version
amanravi-squareops Mar 17, 2025
2d7bc72
Merge pull request #8 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Mar 17, 2025
c12e664
Update variables.tf
amanravi-squareops Mar 19, 2025
61d27f6
Update variables.tf
amanravi-squareops Mar 19, 2025
70ef411
Update variables.tf
amanravi-squareops Mar 19, 2025
f0e949a
Update variables.tf
amanravi-squareops Mar 19, 2025
4add7f1
Update variables.tf
amanravi-squareops Mar 19, 2025
6e3f0a0
Merge pull request #9 from amanravi-squareops/release-1.1.3
divyanshujainSquareops Mar 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*.tfvars
*.pem
vars
.helmignore
23 changes: 22 additions & 1 deletion examples/complete-psql-replica/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ locals {
family = "postgres15"
vpc_cidr = "10.20.0.0/16"
environment = "prod"
create_namespace = true
namespace = "postgres"
storage_type = "gp3"
engine_version = "15.2"
instance_class = "db.m5d.large"
Expand Down Expand Up @@ -101,7 +103,7 @@ module "rds-pg" {
publicly_accessible = false
skip_final_snapshot = true
backup_window = "03:00-06:00"
maintenance_window = "Mon:00:00-Mon:03:00"
maintenance_window = "Mon:00:00-Mon:03:00
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add double quotes at the end

final_snapshot_identifier_prefix = "final"
major_engine_version = local.engine_version
deletion_protection = true
Expand All @@ -113,4 +115,23 @@ module "rds-pg" {
slack_channel = "postgresql-notification"
slack_webhook_url = "https://hooks/xxxxxxxx"
custom_user_password = local.custom_user_password
#if you want backup and restore then you have to create your cluster with rds vpc , subnet, key_arn.
#And allow cluster security group in rds security group
# cluster_name = "cluster-name"
# namespace = local.namespace
# create_namespace = local.create_namespace
# postgresdb_backup_enabled = false
# postgresdb_backup_config = {
# postgres_database_name = "" # which database backup you want
# s3_bucket_region = "" #s3 bucket region
# cron_for_full_backup = "*/3 * * * *"
# bucket_uri = "s3://xyz" #s3 bucket uri
# }
# postgresdb_restore_enabled = false
# postgresdb_restore_config = {
# bucket_uri = "s3://xyz" #s3 bucket uri which have dackup dump file
# backup_file_name = "abc.dump" #Give only .sql or .zip file for restore
# s3_bucket_region = "" # bucket region
# DB_NAME = "" # which db to restore backup file
# }
}
41 changes: 31 additions & 10 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
locals {
region = "us-east-2"
region = "us-east-1"
name = "postgresql"
family = "postgres15"
vpc_cidr = "10.20.0.0/16"
environment = "prod"
engine_version = "15.2"
instance_class = "db.m5d.large"
create_namespace = true
namespace = "pg"
engine_version = "15.4"
instance_class = "db.t4g.micro"
storage_type = "gp3"
current_identity = data.aws_caller_identity.current.arn
allowed_security_groups = ["sg-0a680afd35"]
allowed_security_groups = ["sg-xxxxxxxxxxxxxxxx"]
custom_user_password = ""
additional_tags = {
Owner = "Organization_Name"
Expand All @@ -22,7 +24,7 @@ data "aws_region" "current" {}

module "kms" {
source = "terraform-aws-modules/kms/aws"

version = "~> 1.0"
deletion_window_in_days = 7
description = "Complete key example showing various configurations available"
enable_key_rotation = true
Expand Down Expand Up @@ -99,32 +101,51 @@ module "vpc" {
module "rds-pg" {
source = "squareops/rds-postgresql/aws"
name = local.name
db_name = "postgres"
db_name = "test"
multi_az = "true"
family = local.family
vpc_id = module.vpc.vpc_id
vpc_id = module.vpc.vpc_id
allowed_security_groups = local.allowed_security_groups
subnet_ids = module.vpc.database_subnets ## db subnets
environment = local.environment
kms_key_arn = module.kms.key_arn
storage_type = local.storage_type
engine_version = local.engine_version
instance_class = local.instance_class
master_username = "pguser"
master_username = "admin"
allocated_storage = "20"
max_allocated_storage = 120
publicly_accessible = false
skip_final_snapshot = true
backup_window = "03:00-06:00"
maintenance_window = "Mon:00:00-Mon:03:00"
final_snapshot_identifier_prefix = "final"
final_snapshot_identifier_prefix = "final"
major_engine_version = local.engine_version
deletion_protection = false
cloudwatch_metric_alarms_enabled = true
cloudwatch_metric_alarms_enabled = false
alarm_cpu_threshold_percent = 70
disk_free_storage_space = "10000000" # in bytes
slack_notification_enabled = false
slack_username = "Admin"
slack_channel = "postgresql-notification"
slack_webhook_url = "https://hooks/xxxxxxxx"
custom_user_password = local.custom_user_password
#if you want backup and restore then you have to create your cluster with rds vpc id , private subnets, kms key.
#And allow cluster security group in rds security group
cluster_name = ""
namespace = local.namespace
create_namespace = local.create_namespace
postgresdb_backup_enabled = false
postgresdb_backup_config = {
postgres_database_name = "" # Specify the database name or Leave empty if you wish to backup all databases
cron_for_full_backup = "*/2 * * * *" # set cronjob for backup
bucket_uri = "s3://mongodb-backups-atmosly" # s3 bucket uri
}
postgresdb_restore_enabled = false
postgresdb_restore_config = {
bucket_uri = "s3://mongodb-backups-atmosly" #S3 bucket URI (without a trailing slash /) containing the backup dump file.
backup_file_name = "db5_20241114111607.sql" #Give .sql or .zip file for restore
}
}


21 changes: 21 additions & 0 deletions examples/complete/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@ provider "aws" {
tags = local.additional_tags
}
}
data "aws_eks_cluster" "cluster" {
name = ""

}
data "aws_eks_cluster_auth" "cluster" {
name = ""
}

provider "kubernetes" {
host = data.aws_eks_cluster.cluster.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
token = data.aws_eks_cluster_auth.cluster.token
}

provider "helm" {
kubernetes {
host = data.aws_eks_cluster.cluster.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
token = data.aws_eks_cluster_auth.cluster.token
}
}
2 changes: 1 addition & 1 deletion examples/complete/version.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.43.0"
version = ">= 5.0.0"
}
}
}
35 changes: 35 additions & 0 deletions helm/values/backup/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Enable Full backup
backup:
bucket_uri: ${bucket_uri}
aws_default_region: ${s3_bucket_region}
cron_for_full_backup: "${cron_for_full_backup}"
postgres_database_name: "${postgres_database_name}"
database_endpoint: "${db_endpoint}"
database_password: "${db_password}"
database_user: "${db_username}"


annotations:
${annotations}

auth:
username: "${db_username}"

affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "Addons-Services"
operator: In
values:
- "true"

backupjob:
resources:
requests:
memory: 100Mi
cpu: 50m
limits:
memory: 200Mi
cpu: 100m
33 changes: 33 additions & 0 deletions helm/values/restore/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
restore:
bucket_uri: ${bucket_uri}
aws_default_region: ${s3_bucket_region}
db_endpoint: "${db_endpoint}"
db_password: "${db_password}"
db_username: "${db_username}"
DB_NAME: "${DB_NAME}"
backup_file_name: "${backup_file_name}"

auth:
username: "${db_username}"

annotations:
${annotations}

affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "Addons-Services"
operator: In
values:
- "true"

restorejob:
resources:
requests:
memory: 100Mi
cpu: 50m
limits:
memory: 200Mi
cpu: 100m
33 changes: 31 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ resource "aws_security_group_rule" "cidr_ingress" {

module "security_group_rds" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4"
version = "~> 5.0"
name = format("%s-%s-%s", var.environment, var.name, "rds-sg")
create = var.create_security_group
vpc_id = var.vpc_id
Expand All @@ -155,7 +155,7 @@ module "security_group_rds" {
resource "aws_secretsmanager_secret" "secret_master_db" {
name = format("%s/%s/%s", var.environment, var.name, "rds-postgresql-pass")
tags = merge(
{ "Name" = format("%s/%s/%s", var.environment, var.name, "rds-mysql-pass") },
{ "Name" = format("%s/%s/%s", var.environment, var.name, "rds-postgres-pass") },
local.tags,
)
}
Expand Down Expand Up @@ -317,3 +317,32 @@ resource "aws_lambda_permission" "sns_lambda_slack_invoke" {
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.slack_topic[0].arn
}

module "backup_restore" {
depends_on = [module.db]
source = "./modules/db-backup-restore"
cluster_name = var.cluster_name
namespace = var.namespace
create_namespace = var.create_namespace
postgresdb_backup_enabled = var.postgresdb_backup_enabled
postgresdb_backup_config = {
db_username = var.master_username
db_password = var.custom_user_password != "" ? var.custom_user_password : nonsensitive(random_password.master[0].result)
postgres_database_name = var.postgresdb_backup_config.postgres_database_name
s3_bucket_region = var.postgresdb_backup_config.s3_bucket_region
cron_for_full_backup = var.postgresdb_backup_config.cron_for_full_backup
bucket_uri = var.postgresdb_backup_config.bucket_uri
db_endpoint = replace(var.replica_enable ? module.db_replica[0].db_instance_endpoint : module.db.db_instance_endpoint, ":5432", "")
}

postgresdb_restore_enabled = var.postgresdb_restore_enabled
postgresdb_restore_config = {
db_endpoint = replace(var.replica_enable ? module.db_replica[0].db_instance_endpoint : module.db.db_instance_endpoint, ":5432", "")
db_username = var.master_username
db_password = var.custom_user_password != "" ? var.custom_user_password : nonsensitive(random_password.master[0].result)
bucket_uri = var.postgresdb_restore_config.bucket_uri
s3_bucket_region = var.postgresdb_restore_config.s3_bucket_region
DB_NAME = var.postgresdb_restore_config.DB_NAME,
backup_file_name = var.postgresdb_restore_config.backup_file_name,
}
}
4 changes: 4 additions & 0 deletions modules/db-backup-restore/backup/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
description: A helm chart for Backup of postgres and stored in S3
name: postgres-backup
version: 1.0.0
8 changes: 8 additions & 0 deletions modules/db-backup-restore/backup/templates/backup-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: postgres-bucket-uri
namespace: {{ .Release.Namespace }}
labels:
data:
MYSQL_BUCKET_URI: {{ .Values.backup.bucket_uri | b64enc | quote }}
37 changes: 37 additions & 0 deletions modules/db-backup-restore/backup/templates/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: backup-postgresdb
spec:
schedule: {{ .Values.backup.cron_for_full_backup | quote }}
concurrencyPolicy: Forbid
suspend: false
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1

jobTemplate:
spec:
template:
spec:
affinity: {{ .Values.affinity | toYaml | nindent 10 }}
restartPolicy: OnFailure
serviceAccountName: sa-postgres-backup
containers:
- name: backup-postgresdb
image: divyanshujain11/rds:postgres-backup-restore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

imagePullPolicy: Always
command: ["./backup.sh"]
env:
- name: db_endpoint
value: {{ .Values.backup.database_endpoint }}
- name: db_username
value: {{ .Values.backup.database_user }}
- name: db_password
value: {{ .Values.backup.database_password }}
- name: bucket_uri
value: {{ .Values.backup.bucket_uri }}
- name: AWS_DEFAULT_REGION
value: {{ .Values.backup.aws_default_region}}
- name: postgres_database_name
value: {{ .Values.backup.postgres_database_name }}
resources: {{ .Values.backupjob.resources | toYaml | nindent 12 }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: sa-postgres-backup
namespace: {{ .Release.Namespace }}
annotations:
{{ toYaml .Values.annotations | indent 4 }}
Loading