aboutsummaryrefslogtreecommitdiff
path: root/modules/profile/manifests/postgresql/backup_dump.pp
blob: cae20f748180833d4fe4065ebcfe43472516a9a4 (plain) (blame)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
define profile::postgresql::backup_dump (
  String                             $pg_user  = "postgres",
  String                             $pg_group = "postgres",
  Optional[Variant[String, Integer]] $pg_port  = undef,
) {
  $base_path        = $title
  $pg_path          = "$base_path/postgresql"
  $pg_backup_path   = "$base_path/postgresql_backup"
  $pg_host          = split($base_path, "/")[-1]

  ensure_packages(["python", "python-pip"])
  ensure_resource("package", "pylog2rotate", {
    source   => "git+https://github.com/avian2/pylog2rotate",
    ensure   => present,
    provider => "pip3",
    require  => Package["python-pip"],
  })

  file { $pg_backup_path:
    ensure  => directory,
    owner   => $pg_user,
    group   => $pg_group,
    mode    => "0700",
    require => File[$base_path],
  }

  if $pg_port and !empty($pg_port) {
    $pg_port_arg = " -p $pg_port"
  } else {
    $pg_port_arg = ""
  }

  file { "/usr/local/sbin/backup_psql_$pg_host.sh":
    mode    => "0755",
    content => template("profile/postgresql/backup_psql.sh.erb"),
  }

  if ($pg_host == "eldiron.immae.eu") {
    cron::job::multiple { "backup_psql_$pg_host":
      ensure  => "present",
      require => [File[$pg_backup_path], File[$pg_path]],
      jobs    => [
        {
          command     => "/usr/local/sbin/backup_psql_$pg_host.sh",
          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,12d')",
          user        => $pg_user,
          hour        => 3,
          minute      => 0,
          description => "Cleanup the database backups",
        },
      ]
    }
  } else {
    cron::job::multiple { "backup_psql_$pg_host":
      ensure  => "present",
      require => [File[$pg_backup_path], File[$pg_path]],
      jobs    => [
        {
          command     => "/usr/local/sbin/backup_psql_$pg_host.sh",
          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 | grep -v 'T22:' | sort -r | sed -e '1,12d')",
          user        => $pg_user,
          hour        => 3,
          minute      => 0,
          description => "Cleanup the database backups",
        },
        {
          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')",
          user        => $pg_user,
          hour        => 3,
          minute      => 1,
          description => "Cleanup the database backups exponentially",
        },
      ]
    }
  }

  @profile::monitoring::local_service { "Last postgresql dump in $pg_backup_path is not too old":
    sudos => {
      "naemon-postgresql-dumps-$pg_host" => "naemon  ALL=($pg_user) NOPASSWD: /usr/bin/find $pg_backup_path -mindepth 1 -maxdepth 1 -printf %T@?n",
    },
    local => {
      check_command => "check_last_file_date!$pg_backup_path!7!$pg_user",
    }
  }
}