Merge pull request #64 from runningman/backup

Added mysql::backup class.
This commit is contained in:
Dan Bode 2012-05-10 22:44:52 -07:00
commit 360f8d968a
5 changed files with 142 additions and 0 deletions

View file

@ -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
@ -80,6 +81,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:

68
manifests/backup.pp Normal file
View file

@ -0,0 +1,68 @@
# 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 (
$backupuser,
$backuppassword,
$backupdir,
$ensure = 'present'
) {
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 file

@ -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 file

@ -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

8
tests/backup.pp Normal file
View file

@ -0,0 +1,8 @@
class { 'mysql::server':
config_hash => {'root_password' => 'password'}
}
class { 'mysql::backup':
backupuser => 'myuser',
backuppassword => 'mypassword',
backupdir => '/tmp/backups',
}