]>
Commit | Line | Data |
---|---|---|
d2f031ec | 1 | define profile::postgresql::backup_dump ( |
bd9c5fa8 IB |
2 | String $pg_user = "postgres", |
3 | String $pg_group = "postgres", | |
4 | Optional[Variant[String, Integer]] $pg_port = undef, | |
d2f031ec IB |
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 | ||
bd9c5fa8 IB |
27 | if $pg_port and !empty($pg_port) { |
28 | $pg_port_arg = " -p $pg_port" | |
29 | } else { | |
30 | $pg_port_arg = "" | |
31 | } | |
32 | ||
c6a29ebe IB |
33 | if ($pg_host == "eldiron.immae.eu") { |
34 | cron::job::multiple { "backup_psql_$pg_host": | |
35 | ensure => "present", | |
36 | require => [File[$pg_backup_path], File[$pg_path]], | |
37 | jobs => [ | |
38 | { | |
39 | command => "/usr/bin/pg_dumpall -h $pg_path$pg_port_arg -f $pg_backup_path/\$(date -Iseconds).sql", | |
40 | user => $pg_user, | |
41 | hour => "22,4,10,16", | |
42 | minute => 0, | |
43 | description => "Backup the database", | |
44 | }, | |
45 | { | |
46 | command => "/usr/bin/rm -f $(ls -1 $pg_backup_path/*.sql | sort -r | sed -e '1,12d')", | |
47 | user => $pg_user, | |
48 | hour => 3, | |
49 | minute => 0, | |
50 | description => "Cleanup the database backups", | |
51 | }, | |
52 | ] | |
53 | } | |
54 | } else { | |
55 | cron::job::multiple { "backup_psql_$pg_host": | |
56 | ensure => "present", | |
57 | require => [File[$pg_backup_path], File[$pg_path]], | |
58 | jobs => [ | |
59 | { | |
60 | command => "/usr/bin/pg_dumpall -h $pg_path$pg_port_arg -f $pg_backup_path/\$(date -Iseconds).sql", | |
61 | user => $pg_user, | |
62 | hour => "22,4,10,16", | |
63 | minute => 0, | |
64 | description => "Backup the database", | |
65 | }, | |
66 | { | |
67 | command => "/usr/bin/rm -f $(ls -1 $pg_backup_path/*.sql | grep -v 'T22:' | sort -r | sed -e '1,12d')", | |
68 | user => $pg_user, | |
69 | hour => 3, | |
70 | minute => 0, | |
71 | description => "Cleanup the database backups", | |
72 | }, | |
73 | { | |
74 | 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')", | |
75 | user => $pg_user, | |
76 | hour => 3, | |
77 | minute => 1, | |
78 | description => "Cleanup the database backups exponentially", | |
79 | }, | |
80 | ] | |
81 | } | |
d2f031ec | 82 | } |
b5305b5c IB |
83 | |
84 | @profile::monitoring::local_service { "Last postgresql dump in $pg_backup_path is not too old": | |
85 | sudos => { | |
bff7d1b7 IB |
86 | "naemon-postgresql-dumps-$pg_host" => "naemon ALL=($pg_user) NOPASSWD: /usr/bin/find $pg_backup_path -mindepth 1 -maxdepth 1 -printf %T@?n", |
87 | }, | |
b5305b5c | 88 | local => { |
d8bc7696 | 89 | check_command => "check_last_file_date!$pg_backup_path!7!$pg_user", |
b5305b5c IB |
90 | } |
91 | } | |
d2f031ec | 92 | } |