add cronjob to backup postgres dbs [ci skip]

This commit is contained in:
Viktor Barzin 2024-11-23 12:40:03 +00:00
parent 1c947538e6
commit da9c126382
No known key found for this signature in database
GPG key ID: 4056458DBDBF8863
2 changed files with 51 additions and 0 deletions

View file

@ -898,3 +898,54 @@ resource "kubernetes_ingress_v1" "pgadmin" {
}
}
}
resource "kubernetes_cron_job_v1" "postgresql-backup" {
metadata {
name = "postgresql-backup"
namespace = "dbaas"
}
spec {
concurrency_policy = "Replace"
failed_jobs_history_limit = 5
schedule = "0 */6 * * *"
# schedule = "* * * * *"
starting_deadline_seconds = 10
successful_jobs_history_limit = 10
job_template {
metadata {}
spec {
backoff_limit = 3
ttl_seconds_after_finished = 10
template {
metadata {}
spec {
container {
name = "postgresql-backup"
image = "postgres:16.4-bullseye"
command = ["/bin/sh", "-c", <<-EOT
export now=$(date +"%Y_%m_%d_%H_%M")
PGPASSWORD=${var.postgresql_root_password} pg_dumpall -h postgresql.dbaas -U root > /backup/dump_$now.sql
# Rotate - delete last log file
cd /backup
find . -name "dump_*.sql" -type f -mtime +14 -delete # 14 day retention of backups
EOT
]
volume_mount {
name = "postgresql-backup"
mount_path = "/backup"
}
}
volume {
name = "postgresql-backup"
nfs {
path = "/mnt/main/postgresql-backup"
server = "10.0.10.15"
}
}
}
}
}
}
}
}

Binary file not shown.