Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added mysql::backup class.

Installs a mysql backup script, cronjob, and priviledged backup user. 
Includes rspec tests and updated documentation.
  • Loading branch information...
commit 4158137278387de262dd8c92360d8786892ff3a1 1 parent 12a4410
@razorsedge razorsedge authored
View
10 README.md
@@ -38,6 +38,7 @@ This module is based on work by David Schmitt. The following contributor have co
* Lowe Schmidt
* Matthias Pigulla
* William Van Hevelingen
+* Michael Arnold
## Usage
@@ -75,6 +76,15 @@ Creates a database with a user and assign some privileges.
grant => ['all'],
}
+### mysql::backup
+Installs a mysql backup script, cronjob, and priviledged backup user.
+
+ class { 'mysql::backup':
+ backupuser => 'myuser',
+ backuppassword => 'mypassword',
+ backupdir => '/tmp/backups',
+ }
+
### Providers for database types:
MySQL provider supports puppet resources command:
View
72 manifests/backup.pp
@@ -0,0 +1,72 @@
+# Class: mysql::backup
+#
+# This module handles ...
+#
+# Parameters:
+# [*backupuser*] - The name of the mysql backup user.
+# [*backuppassword*] - The password of the mysql backup user.
+# [*backupdir*] - The target directory of the mysqldump.
+#
+# Actions:
+# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
+# IDENTIFIED BY 'password';
+#
+# Requires:
+# Class['mysql::config']
+#
+# Sample Usage:
+# class { 'mysql::backup':
+# backupuser => 'myuser',
+# backuppassword => 'mypassword',
+# backupdir => '/tmp/backups',
+# }
+#
+class mysql::backup (
+ $ensure = 'present',
+ $backupuser = $mysql::params::backupuser,
+ $backuppassword = $mysql::params::backuppassword,
+ $backupdir = $mysql::params::backupdir
+) {
+
+ if $backupuser == 'UNSET' or $backupdir == 'UNSET' or $backuppassword == 'UNSET' {
+ fail('mysql::backup - You must specify a backup user, password, and target directory.')
+ }
+
+ database_user { "${backupuser}@localhost":
+ ensure => $ensure,
+ password_hash => mysql_password($backuppassword),
+ provider => 'mysql',
+ require => Class['mysql::config'],
+ }
+
+ database_grant { "${backupuser}@localhost":
+ privileges => [ 'select_priv', 'reload_priv', 'lock_tables_priv' ],
+ require => Database_user["${backupuser}@localhost"],
+ }
+
+ cron { 'mysql-backup':
+ ensure => $ensure,
+ command => '/usr/local/sbin/mysqlbackup.sh',
+ user => 'root',
+ hour => 23,
+ minute => 5,
+ require => File['mysqlbackup.sh'],
+ }
+
+ file { 'mysqlbackup.sh':
+ ensure => $ensure,
+ path => '/usr/local/sbin/mysqlbackup.sh',
+ mode => '0700',
+ owner => 'root',
+ group => 'root',
+ content => template('mysql/mysqlbackup.sh.erb'),
+ }
+
+ file { 'mysqlbackupdir':
+ ensure => 'directory',
+ path => $backupdir,
+ mode => '0700',
+ owner => 'root',
+ group => 'root',
+ }
+}
View
3  manifests/params.pp
@@ -21,6 +21,9 @@
$ssl_ca = '/etc/mysql/cacert.pem'
$ssl_cert = '/etc/mysql/server-cert.pem'
$ssl_key = '/etc/mysql/server-key.pem'
+ $backupuser = 'UNSET'
+ $backuppassword = 'UNSET'
+ $backupdir = 'UNSET'
case $::operatingsystem {
"Ubuntu": {
View
33 spec/classes/mysql_backup_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe 'mysql::backup' do
+
+ let(:params) {
+ { 'backupuser' => 'testuser',
+ 'backuppassword' => 'testpass',
+ 'backupdir' => '/tmp',
+ }
+ }
+
+ it { should contain_database_user('testuser@localhost')}
+
+ it { should contain_database_grant('testuser@localhost').with(
+ :privileges => [ 'select_priv', 'reload_priv', 'lock_tables_priv' ]
+ )}
+
+ it { should contain_cron('mysql-backup').with(
+ :command => '/usr/local/sbin/mysqlbackup.sh',
+ :ensure => 'present'
+ )}
+
+ it { should contain_file('mysqlbackup.sh').with(
+ :path => '/usr/local/sbin/mysqlbackup.sh',
+ :ensure => 'present'
+ )}
+
+ it { should contain_file('mysqlbackupdir').with(
+ :path => '/tmp',
+ :ensure => 'directory'
+ )}
+
+end
View
23 templates/mysqlbackup.sh.erb
@@ -0,0 +1,23 @@
+#!/bin/sh
+#
+# MySQL Backup Script
+# Dumps mysql databases to a file for another backup tool to pick up.
+#
+# MySQL code:
+# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
+# IDENTIFIED BY 'password';
+# FLUSH PRIVILEGES;
+#
+##### START CONFIG ###################################################
+
+USER=<%= backupuser %>
+PASS=<%= backuppassword %>
+DIR=<%= backupdir %>
+
+##### STOP CONFIG ####################################################
+PATH=/usr/bin:/usr/sbin:/bin:/sbin
+
+find $DIR -mtime +30 -exec rm -f {} \;
+mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
+ --all-databases | bzcat -zc > ${DIR}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql.bz2
+
View
8 tests/backup.pp
@@ -0,0 +1,8 @@
+class { 'mysql::server':
+ config_hash => {'root_password' => 'password'}
+}
+class { 'mysql::backup':
+ backupuser => 'myuser',
+ backuppassword => 'mypassword',
+ backupdir => '/tmp/backups',
+}
Please sign in to comment.
Something went wrong with that request. Please try again.