From c17059cbd2df33d71091ee3fde44e36f32492ee4 Mon Sep 17 00:00:00 2001
From: Clark Boylan <clark.boylan@gmail.com>
Date: Tue, 13 Aug 2013 14:05:46 -0700
Subject: [PATCH] Add new mysql_backup module.

The puppetlabs mysql modules has a backup class which seems to be too
smart for its own good. Create a simple module with a backup define that
allows us to backup specific databases independently.

Change-Id: If8a68b254ab1df61a1c9f99f8484a6ebd2e66944
---
 modules/mysql_backup/manifests/backup.pp | 48 ++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 modules/mysql_backup/manifests/backup.pp

diff --git a/modules/mysql_backup/manifests/backup.pp b/modules/mysql_backup/manifests/backup.pp
new file mode 100644
index 0000000000..3babce4615
--- /dev/null
+++ b/modules/mysql_backup/manifests/backup.pp
@@ -0,0 +1,48 @@
+# == Define: mysql_backup::backup
+#
+# Arguments determine when backups should be taken, where they should
+# be located, and how often they shouled be rotated. The namevar
+# of the define must be the name of the database to backup.
+# This define assumes that the mysqldump command is installed under
+# /usr/bin.
+#
+define mysql_backup::backup (
+  $minute = '0',
+  $hour = '0',
+  $day = '*',
+  $dest_dir = '/var/backups/mysql_backups',
+  $rotation = 'daily',
+  $num_backups = '30',
+  $defaults_file = '/etc/mysql/debian.cnf'
+) {
+  # Wrap in check as there may be mutliple backup defines backing
+  # up to the same dir.
+  if ! defined(File[$dest_dir]) {
+    file { $dest_dir:
+      ensure => directory,
+      mode   => '0755',
+      owner  => 'root',
+      group  => 'root',
+    }
+  }
+
+  cron { "${name}-backup":
+    ensure  => present,
+    command => "/usr/bin/mysqldump --defaults-file=${defaults_file} --opt --all-databases | gzip -9 > ${dest_dir}/${name}.sql.gz",
+    minute  => $minute,
+    hour    => $hour,
+    weekday => $day,
+    require => File[$dest_dir],
+  }
+
+  include logrotate
+  logrotate::file { "${name}-rotate":
+    log     => "${dest_dir}/${name}.sql.gz",
+    options => [
+      'nocompress',
+      "rotate ${num_backups}",
+      $rotation,
+    ],
+    require => Cron["${name}-backup"],
+  }
+}