-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql-backup.hcl
54 lines (41 loc) · 1.21 KB
/
postgresql-backup.hcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
job "postgresql-backup" {
datacenters = ["dc1"]
type = "batch"
periodic {
crons = ["0 */3 * * * *"]
prohibit_overlap = true
}
group "db-backup" {
task "postgres-backup" {
driver = "raw_exec"
config {
command = "/bin/bash"
args = ["local/script.sh"]
}
template {
data = <<EOH
#!/bin/bash
file=/backups/postgresql/postgresql-$(date +%Y-%m-%d_%H-%M-%S).sql
alloc_id=$(curl -s --request GET http://nomad.service.consul:4646/v1/job/postgres/allocations | jq '.[0].ID' | tr -d '"')
nomad alloc exec $alloc_id pg_dumpall -U root > "${file}"
find /backups/postgresql/postgresql* -ctime +14 -exec rm {} \;
file_size=$(find $file -exec du -sh {} \; | cut -f1 | xargs | sed 's/$//')
if [ -s "$file" ]; then
exit 0
else
rm $file
curl -H "Content-Type: application/json" -d '{"content": "`PostgreSQL` backup has just **FAILED**\nFile name: `'"$file"'`\nDate: `'"$(TZ=Europe/Dublin date)"'`"}' {{ key "discord/log/webhook" }}
fi
EOH
destination = "local/script.sh"
}
template {
data = <<EOH
#hostname:port:database:username:password
*:*:*:root:rootpassword
EOH
destination = "local/.pgpass"
}
}
}
}