]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/commitdiff
Add cron job to backup postgresql
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Wed, 16 May 2018 10:21:11 +0000 (12:21 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Wed, 16 May 2018 10:30:32 +0000 (12:30 +0200)
modules/role/manifests/backup/postgresql.pp

index 51ce37efbd14b394f7692fa47cc9edad38eb0c54..e63561149e40de398a33818eafeeb2147c78cef6 100644 (file)
@@ -96,6 +96,7 @@ class role::backup::postgresql inherits role::backup {
 
   $pg_backup_hosts.each |$pg_backup_host, $pg_infos| {
     $pg_path = "$mountpoint/$pg_backup_host/postgresql"
+    $pg_backup_path = "$mountpoint/$pg_backup_host/postgresql_backup"
     $pg_host = "$pg_backup_host"
     $pg_port = $pg_infos["dbport"]
 
@@ -132,6 +133,35 @@ class role::backup::postgresql inherits role::backup {
       require => File["$mountpoint/$pg_backup_host"],
     }
 
+    file { $pg_backup_path:
+      ensure  => directory,
+      owner   => $pg_user,
+      group   => $pg_group,
+      mode    => "0700",
+      require => File["$mountpoint/$pg_backup_host"],
+    }
+
+    cron::job::multiple { "backup_psql_$pg_host":
+      ensure  => "present",
+      require => [File[$pg_backup_path], File[$pg_path]],
+      jobs    => [
+        {
+          command     => "/usr/bin/pg_dumpall -h $pg_path -f $pg_backup_path/\$(date -Iseconds).sql",
+          user        => $pg_user,
+          hour        => "22,4,10,16",
+          minute      => 0,
+          description => "Backup the database",
+        },
+        {
+          command     => "/usr/bin/rm -f $(ls -1 $pg_backup_path/*.sql | sort -r | sed -e '1,16d')",
+          user        => $pg_user,
+          hour        => 3,
+          minute      => 0,
+          description => "Cleanup the database backups",
+        },
+      ]
+    }
+
     exec { "pg_basebackup $pg_path":
       cwd         => $pg_path,
       user        => $pg_user,