From d2f031ece106ed2dc37283b194edfa94718a6306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Wed, 27 Jun 2018 21:52:02 +0200 Subject: Refactor backup postgresql --- .../profile/manifests/postgresql/backup_dump.pp | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 modules/profile/manifests/postgresql/backup_dump.pp (limited to 'modules/profile/manifests/postgresql/backup_dump.pp') diff --git a/modules/profile/manifests/postgresql/backup_dump.pp b/modules/profile/manifests/postgresql/backup_dump.pp new file mode 100644 index 0000000..10e349a --- /dev/null +++ b/modules/profile/manifests/postgresql/backup_dump.pp @@ -0,0 +1,53 @@ +define profile::postgresql::backup_dump ( + String $pg_user = "postgres", + String $pg_group = "postgres", +) { + $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], + } + + 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 | 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", + }, + ] + } +} -- cgit v1.2.3