Skip to content

Commit b1d0302

Browse files
Merge pull request #7 from ShibraAmin18/main
added backup and restore
2 parents 4bd0d7b + 5bdc804 commit b1d0302

File tree

21 files changed

+398
-5
lines changed

21 files changed

+398
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ No modules.
100100
| Name | Type |
101101
|------|------|
102102
| [helm_release.postgres_exporter](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
103+
| [helm_release.postgresql_backup](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
103104
| [helm_release.postgresql_ha](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
105+
| [helm_release.postgresql_restore](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
104106
| [kubernetes_namespace.postgresql](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) | resource |
105107

106108
## Inputs
@@ -109,13 +111,20 @@ No modules.
109111
|------|-------------|------|---------|:--------:|
110112
| <a name="input_chart_version"></a> [chart\_version](#input\_chart\_version) | Version of the Postgresql helm chart that will be deployed. | `string` | `"11.7.9"` | no |
111113
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of eks cluster | `string` | `""` | no |
114+
| <a name="input_create_namespace"></a> [create\_namespace](#input\_create\_namespace) | Whether or not to deploy postgresql | `bool` | `true` | no |
112115
| <a name="input_custom_credentials_config"></a> [custom\_credentials\_config](#input\_custom\_credentials\_config) | Specify the configuration settings for Postgresql to pass custom credentials during creation. | `any` | <pre>{<br> "postgres_password": "",<br> "repmgr_password": ""<br>}</pre> | no |
113116
| <a name="input_custom_credentials_enabled"></a> [custom\_credentials\_enabled](#input\_custom\_credentials\_enabled) | Specifies whether to enable custom credentials for PostgreSQL database. | `bool` | `false` | no |
117+
| <a name="input_iam_role_arn_backup"></a> [iam\_role\_arn\_backup](#input\_iam\_role\_arn\_backup) | IAM role ARN for backup (AWS) | `string` | `""` | no |
118+
| <a name="input_iam_role_arn_restore"></a> [iam\_role\_arn\_restore](#input\_iam\_role\_arn\_restore) | IAM role ARN for restore (AWS) | `string` | `""` | no |
114119
| <a name="input_postgres_password"></a> [postgres\_password](#input\_postgres\_password) | PostgresQL password | `any` | `""` | no |
120+
| <a name="input_postgresql_backup_config"></a> [postgresql\_backup\_config](#input\_postgresql\_backup\_config) | configuration options for Pgsql database backups. It includes properties such as the S3 bucket Name, the S3 bucket region, and the cron expression for full backups. | `any` | <pre>{<br> "bucket_name": "",<br> "cron_for_full_backup": "",<br> "s3_bucket_region": ""<br>}</pre> | no |
121+
| <a name="input_postgresql_backup_enabled"></a> [postgresql\_backup\_enabled](#input\_postgresql\_backup\_enabled) | Specifies whether to enable backups for Pgsql database. | `bool` | `false` | no |
115122
| <a name="input_postgresql_config"></a> [postgresql\_config](#input\_postgresql\_config) | Configuration options for the postgresql such as number of replica,chart version, storage class and store password at secret manager. | `map(string)` | <pre>{<br> "environment": "",<br> "name": "",<br> "postgresql_values": "",<br> "replicaCount": 3,<br> "storage_class": "gp2",<br> "store_password_to_secret_manager": true<br>}</pre> | no |
116123
| <a name="input_postgresql_enabled"></a> [postgresql\_enabled](#input\_postgresql\_enabled) | Whether or not to deploy postgresql | `bool` | `true` | no |
117124
| <a name="input_postgresql_exporter_enabled"></a> [postgresql\_exporter\_enabled](#input\_postgresql\_exporter\_enabled) | Whether or not to deploy postgresql exporter | `bool` | `false` | no |
118125
| <a name="input_postgresql_namespace"></a> [postgresql\_namespace](#input\_postgresql\_namespace) | Name of the Kubernetes namespace where the postgresql will be deployed. | `string` | `"postgresql"` | no |
126+
| <a name="input_postgresql_restore_config"></a> [postgresql\_restore\_config](#input\_postgresql\_restore\_config) | Configuration options for restoring dump to the Postgresql database. | `any` | <pre>{<br> "bucket_uri": "",<br> "file_name": "",<br> "s3_bucket_region": ""<br>}</pre> | no |
127+
| <a name="input_postgresql_restore_enabled"></a> [postgresql\_restore\_enabled](#input\_postgresql\_restore\_enabled) | Specifies whether to enable restoring dump to the Postgresql database. | `bool` | `false` | no |
119128
| <a name="input_recovery_window_aws_secret"></a> [recovery\_window\_aws\_secret](#input\_recovery\_window\_aws\_secret) | Number of days that AWS Secrets Manager will wait before deleting a secret. This value can be set to 0 to force immediate deletion, or to a value between 7 and 30 days to allow for recovery. | `number` | `0` | no |
120129
| <a name="input_repmgr_password"></a> [repmgr\_password](#input\_repmgr\_password) | Replication manager password | `any` | `""` | no |
121130

examples/complete/aws/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ No requirements.
2121

2222
| Name | Source | Version |
2323
|------|--------|---------|
24-
| <a name="module_aws"></a> [aws](#module\_aws) | ../../../modules/resources/aws | n/a |
25-
| <a name="module_postgresql"></a> [postgresql](#module\_postgresql) | ../../../ | n/a |
24+
| <a name="module_aws"></a> [aws](#module\_aws) | [email protected]:sq-ia/terraform-kubernetes-postgresql.git//modules/resources/aws | n/a |
25+
| <a name="module_postgresql"></a> [postgresql](#module\_postgresql) | [email protected]:sq-ia/terraform-kubernetes-postgresql.git | n/a |
2626

2727
## Resources
2828

examples/complete/aws/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,18 @@ module "postgresql" {
4040
postgresql_values = file("./helm/postgresql.yaml")
4141
store_password_to_secret_manager = local.store_password_to_secret_manager
4242
}
43+
iam_role_arn_backup = module.aws.iam_role_arn_backup
44+
postgresql_backup_enabled = true
45+
postgresql_backup_config = {
46+
bucket_name = "backup-309017165673"
47+
s3_bucket_region = "us-east-2"
48+
cron_for_full_backup = "*/5 * * * *"
49+
}
50+
postgresql_restore_enabled = true
51+
iam_role_arn_restore = module.aws.iam_role_arn_restore
52+
postgresql_restore_config = {
53+
bucket_uri = "s3://backup-309017165673/pgdump__20231208095502.zip"
54+
file_name = "pgdump__20231208095502.zip"
55+
s3_bucket_region = "us-east-2"
56+
}
4357
}

helm/backup/values.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Enable Full backup
2+
backup:
3+
bucket_name: ${bucket_name}
4+
aws_default_region: ${s3_bucket_region}
5+
cron_for_full_backup: "${cron_for_full_backup}"
6+
7+
annotations:
8+
${annotations}
9+
10+
auth:
11+
username: ${custom_user_username}

helm/restore/values.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
restore:
2+
bucket_uri: ${bucket_uri}
3+
file_name: ${file_name}
4+
aws_default_region: ${s3_bucket_region}
5+
6+
auth:
7+
username: "${custom_user_username}"
8+
9+
annotations:
10+
${annotations}

main.tf

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
resource "kubernetes_namespace" "postgresql" {
2-
count = var.postgresql_enabled ? 1 : 0
2+
count = var.create_namespace ? 1 : 0
33
metadata {
44
name = var.postgresql_namespace
55
}
66
}
77

88
resource "helm_release" "postgresql_ha" {
9-
count = var.postgresql_enabled ? 1 : 0
109
depends_on = [kubernetes_namespace.postgresql]
1110
name = "postgresql-ha"
1211
chart = "postgresql-ha"
@@ -27,7 +26,7 @@ resource "helm_release" "postgresql_ha" {
2726
}
2827

2928
resource "helm_release" "postgres_exporter" {
30-
count = var.postgresql_enabled && var.postgresql_exporter_enabled ? 1 : 0
29+
count = var.postgresql_exporter_enabled ? 1 : 0
3130
depends_on = [helm_release.postgresql_ha]
3231
name = "postgres-exporter"
3332
chart = "prometheus-postgres-exporter"
@@ -42,3 +41,41 @@ resource "helm_release" "postgres_exporter" {
4241
})
4342
]
4443
}
44+
45+
46+
resource "helm_release" "postgresql_backup" {
47+
depends_on = [helm_release.postgresql_ha]
48+
count = var.postgresql_backup_enabled ? 1 : 0
49+
name = "postgresql-backup"
50+
chart = "${path.module}/modules/backup"
51+
timeout = 600
52+
namespace = var.postgresql_namespace
53+
values = [
54+
templatefile("${path.module}/helm/backup/values.yaml", {
55+
bucket_name = var.postgresql_backup_config.bucket_name,
56+
s3_bucket_region = var.postgresql_backup_config.s3_bucket_region,
57+
cron_for_full_backup = var.postgresql_backup_config.cron_for_full_backup,
58+
custom_user_username = "postgres",
59+
annotations = "eks.amazonaws.com/role-arn: ${var.iam_role_arn_backup}"
60+
})
61+
]
62+
}
63+
64+
## DB dump restore
65+
resource "helm_release" "postgresql_restore" {
66+
depends_on = [helm_release.postgresql_ha]
67+
count = var.postgresql_restore_enabled ? 1 : 0
68+
name = "postgresql-restore"
69+
chart = "${path.module}/modules/restore"
70+
timeout = 600
71+
namespace = var.postgresql_namespace
72+
values = [
73+
templatefile("${path.module}/helm/restore/values.yaml", {
74+
bucket_uri = var.postgresql_restore_config.bucket_uri,
75+
file_name = var.postgresql_restore_config.file_name,
76+
s3_bucket_region = var.postgresql_restore_config.s3_bucket_region,
77+
custom_user_username = "postgres",
78+
annotations = "eks.amazonaws.com/role-arn: ${var.iam_role_arn_restore}"
79+
})
80+
]
81+
}

modules/backup/.helmignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*~
18+
# Various IDEs
19+
.project
20+
.idea/
21+
*.tmproj

modules/backup/Chart.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
description: A helm chart for Backup of pgsql and stored in S3
3+
name: pgsql-backup
4+
version: 1.0.0
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: pgsql-bucket-name
5+
namespace: {{ .Release.Namespace }}
6+
labels:
7+
data:
8+
S3_BUCKET: {{ .Values.backup.bucket_name | b64enc | quote }}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apiVersion: batch/v1
2+
kind: CronJob
3+
metadata:
4+
name: backup-pgsqldb
5+
spec:
6+
schedule: {{ .Values.backup.cron_for_full_backup | quote }}
7+
concurrencyPolicy: Forbid
8+
suspend: false
9+
successfulJobsHistoryLimit: 3
10+
failedJobsHistoryLimit: 1
11+
12+
jobTemplate:
13+
spec:
14+
template:
15+
spec:
16+
restartPolicy: OnFailure
17+
imagePullSecrets:
18+
- name: regcred
19+
serviceAccountName: sa-pgsql-backup
20+
containers:
21+
- name: backup-pgsqldb
22+
image: squareops01/pgsqldb-backup:v4
23+
imagePullPolicy: Always
24+
env:
25+
- name: DB_HOST
26+
value: postgresql-ha-postgresql-headless.{{ .Release.Namespace }}.svc.cluster.local
27+
- name: DB_USER
28+
value: {{ .Values.auth.username }}
29+
- name: PGPASSWORD
30+
valueFrom:
31+
secretKeyRef:
32+
name: postgresql-ha-postgresql
33+
key: password
34+
- name: S3_BUCKET
35+
valueFrom:
36+
secretKeyRef:
37+
name: pgsql-bucket-name
38+
key: S3_BUCKET
39+
- name: AWS_DEFAULT_REGION
40+
value: {{ .Values.backup.aws_default_region }}

0 commit comments

Comments
 (0)