]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - modules/profile/manifests/postgresql/backup_dump.pp
Pause replication before the postgresql backup
[perso/Immae/Projets/Puppet.git] / modules / profile / manifests / postgresql / backup_dump.pp
1 define profile::postgresql::backup_dump (
2 String $pg_user = "postgres",
3 String $pg_group = "postgres",
4 Optional[Variant[String, Integer]] $pg_port = undef,
5 ) {
6 $base_path = $title
7 $pg_path = "$base_path/postgresql"
8 $pg_backup_path = "$base_path/postgresql_backup"
9 $pg_host = split($base_path, "/")[-1]
10
11 ensure_packages(["python", "python-pip"])
12 ensure_resource("package", "pylog2rotate", {
13 source => "git+https://github.com/avian2/pylog2rotate",
14 ensure => present,
15 provider => "pip3",
16 require => Package["python-pip"],
17 })
18
19 file { $pg_backup_path:
20 ensure => directory,
21 owner => $pg_user,
22 group => $pg_group,
23 mode => "0700",
24 require => File[$base_path],
25 }
26
27 if $pg_port and !empty($pg_port) {
28 $pg_port_arg = " -p $pg_port"
29 } else {
30 $pg_port_arg = ""
31 }
32
33 file { "/usr/local/sbin/backup_psql_$pg_host.sh":
34 mode => "0755",
35 content => template("profile/postgresql/backup_psql.sh.erb"),
36 }
37
38 if ($pg_host == "eldiron.immae.eu") {
39 cron::job::multiple { "backup_psql_$pg_host":
40 ensure => "present",
41 require => [File[$pg_backup_path], File[$pg_path]],
42 jobs => [
43 {
44 command => "/usr/local/sbin/backup_psql_$pg_host.sh",
45 user => $pg_user,
46 hour => "22,4,10,16",
47 minute => 0,
48 description => "Backup the database",
49 },
50 {
51 command => "/usr/bin/rm -f $(ls -1 $pg_backup_path/*.sql | sort -r | sed -e '1,12d')",
52 user => $pg_user,
53 hour => 3,
54 minute => 0,
55 description => "Cleanup the database backups",
56 },
57 ]
58 }
59 } else {
60 cron::job::multiple { "backup_psql_$pg_host":
61 ensure => "present",
62 require => [File[$pg_backup_path], File[$pg_path]],
63 jobs => [
64 {
65 command => "/usr/local/sbin/backup_psql_$pg_host.sh",
66 user => $pg_user,
67 hour => "22,4,10,16",
68 minute => 0,
69 description => "Backup the database",
70 },
71 {
72 command => "/usr/bin/rm -f $(ls -1 $pg_backup_path/*.sql | grep -v 'T22:' | sort -r | sed -e '1,12d')",
73 user => $pg_user,
74 hour => 3,
75 minute => 0,
76 description => "Cleanup the database backups",
77 },
78 {
79 command => "cd $pg_backup_path ; /usr/bin/rm -f $(ls -1 *T22*.sql | log2rotate --skip 7 --fuzz 7 --delete --format='\%Y-\%m-\%dT\%H:\%M:\%S+02:00.sql')",
80 user => $pg_user,
81 hour => 3,
82 minute => 1,
83 description => "Cleanup the database backups exponentially",
84 },
85 ]
86 }
87 }
88
89 @profile::monitoring::local_service { "Last postgresql dump in $pg_backup_path is not too old":
90 sudos => {
91 "naemon-postgresql-dumps-$pg_host" => "naemon ALL=($pg_user) NOPASSWD: /usr/bin/find $pg_backup_path -mindepth 1 -maxdepth 1 -printf %T@?n",
92 },
93 local => {
94 check_command => "check_last_file_date!$pg_backup_path!7!$pg_user",
95 }
96 }
97 }