-
Notifications
You must be signed in to change notification settings - Fork 794
Added mysql::backup class. #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class { 'mysql::server': | ||
config_hash => {'root_password' => 'password'} | ||
} | ||
class { 'mysql::backup': | ||
backupuser => 'myuser', | ||
backuppassword => 'mypassword', | ||
backupdir => '/tmp/backups', | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to make the frequency, time, and retention of backups more configurable? Perhaps make the cron time settings configurable and incorporate logrotate to make retention configurable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cboylan It is possible, but I was hoping to get this base module merged and then everyone can submit pull requests against it to make it even better.