diff --git a/manifests/backup/mysqlbackup.pp b/manifests/backup/mysqlbackup.pp index 3cd4e15..ab0bd17 100644 --- a/manifests/backup/mysqlbackup.pp +++ b/manifests/backup/mysqlbackup.pp @@ -1,124 +1,125 @@ # @summary # Manage the mysqlbackup client. # # @api private # class mysql::backup::mysqlbackup ( - $backupuser = '', - $backuppassword = '', - $maxallowedpacket = '1M', - $backupdir = '', - $backupdirmode = '0700', - $backupdirowner = 'root', - $backupdirgroup = $mysql::params::root_group, - $backupcompress = true, - $backuprotate = 30, - $backupmethod = '', - $ignore_events = true, - $delete_before_dump = false, - $backupdatabases = [], - $file_per_database = false, - $include_triggers = true, - $include_routines = false, - $ensure = 'present', - $time = ['23', '5'], - $prescript = false, - $postscript = false, - $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', - $optional_args = [], + $backupuser = '', + $backuppassword = '', + $maxallowedpacket = '1M', + $backupdir = '', + $backupdirmode = '0700', + $backupdirowner = 'root', + $backupdirgroup = $mysql::params::root_group, + $backupcompress = true, + $backuprotate = 30, + $backupmethod = '', + $backup_success_file_path = undef, + $ignore_events = true, + $delete_before_dump = false, + $backupdatabases = [], + $file_per_database = false, + $include_triggers = true, + $include_routines = false, + $ensure = 'present', + $time = ['23', '5'], + $prescript = false, + $postscript = false, + $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', + $optional_args = [], ) inherits mysql::params { mysql_user { "${backupuser}@localhost": ensure => $ensure, password_hash => mysql::password($backuppassword), require => Class['mysql::server::root_password'], } package { 'meb': ensure => $ensure, } # http://dev.mysql.com/doc/mysql-enterprise-backup/3.11/en/mysqlbackup.privileges.html mysql_grant { "${backupuser}@localhost/*.*": ensure => $ensure, user => "${backupuser}@localhost", table => '*.*', privileges => [ 'RELOAD', 'SUPER', 'REPLICATION CLIENT' ], require => Mysql_user["${backupuser}@localhost"], } mysql_grant { "${backupuser}@localhost/mysql.backup_progress": ensure => $ensure, user => "${backupuser}@localhost", table => 'mysql.backup_progress', privileges => [ 'CREATE', 'INSERT', 'DROP', 'UPDATE' ], require => Mysql_user["${backupuser}@localhost"], } mysql_grant { "${backupuser}@localhost/mysql.backup_history": ensure => $ensure, user => "${backupuser}@localhost", table => 'mysql.backup_history', privileges => [ 'CREATE', 'INSERT', 'SELECT', 'DROP', 'UPDATE' ], require => Mysql_user["${backupuser}@localhost"], } if $::osfamily == 'RedHat' and $::operatingsystemmajrelease == '5' { package {'crontabs': ensure => present, } } elsif $::osfamily == 'RedHat' { package {'cronie': ensure => present, } } elsif $::osfamily != 'FreeBSD' { package {'cron': ensure => present, } } cron { 'mysqlbackup-weekly': ensure => $ensure, command => 'mysqlbackup backup', user => 'root', hour => $time[0], minute => $time[1], weekday => '0', require => Package['meb'], } cron { 'mysqlbackup-daily': ensure => $ensure, command => 'mysqlbackup --incremental backup', user => 'root', hour => $time[0], minute => $time[1], weekday => '1-6', require => Package['meb'], } $default_options = { 'mysqlbackup' => { 'backup-dir' => $backupdir, 'with-timestamp' => true, 'incremental_base' => 'history:last_backup', 'incremental_backup_dir' => $backupdir, 'user' => $backupuser, 'password' => $backuppassword, } } $options = mysql::normalise_and_deepmerge($default_options, $mysql::server::override_options) file { 'mysqlbackup-config-file': path => '/etc/mysql/conf.d/meb.cnf', content => template('mysql/meb.cnf.erb'), mode => '0600', } file { $backupdir: ensure => 'directory', mode => $backupdirmode, owner => $backupdirowner, group => $backupdirgroup, } } diff --git a/manifests/backup/mysqldump.pp b/manifests/backup/mysqldump.pp index d76545d..7c95f91 100644 --- a/manifests/backup/mysqldump.pp +++ b/manifests/backup/mysqldump.pp @@ -1,109 +1,110 @@ # @summary # "Provider" for mysqldump # @api private # class mysql::backup::mysqldump ( - $backupuser = '', - $backuppassword = '', - $backupdir = '', - $maxallowedpacket = '1M', - $backupdirmode = '0700', - $backupdirowner = 'root', - $backupdirgroup = $mysql::params::root_group, - $backupcompress = true, - $backuprotate = 30, - $backupmethod = 'mysqldump', - $ignore_events = true, - $delete_before_dump = false, - $backupdatabases = [], - $file_per_database = false, - $include_triggers = false, - $include_routines = false, - $ensure = 'present', - $time = ['23', '5'], - $prescript = false, - $postscript = false, - $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', - $optional_args = [], - $mysqlbackupdir_ensure = 'directory', - $mysqlbackupdir_target = undef, + $backupuser = '', + $backuppassword = '', + $backupdir = '', + $maxallowedpacket = '1M', + $backupdirmode = '0700', + $backupdirowner = 'root', + $backupdirgroup = $mysql::params::root_group, + $backupcompress = true, + $backuprotate = 30, + $backupmethod = 'mysqldump', + $backup_success_file_path = undef, + $ignore_events = true, + $delete_before_dump = false, + $backupdatabases = [], + $file_per_database = false, + $include_triggers = false, + $include_routines = false, + $ensure = 'present', + $time = ['23', '5'], + $prescript = false, + $postscript = false, + $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', + $optional_args = [], + $mysqlbackupdir_ensure = 'directory', + $mysqlbackupdir_target = undef, ) inherits mysql::params { if $backupcompress { ensure_packages(['bzip2']) Package['bzip2'] -> File['mysqlbackup.sh'] } mysql_user { "${backupuser}@localhost": ensure => $ensure, password_hash => mysql::password($backuppassword), require => Class['mysql::server::root_password'], } if $include_triggers { $privs = [ 'SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS', 'TRIGGER' ] } else { $privs = [ 'SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS' ] } mysql_grant { "${backupuser}@localhost/*.*": ensure => $ensure, user => "${backupuser}@localhost", table => '*.*', privileges => $privs, require => Mysql_user["${backupuser}@localhost"], } if $::osfamily == 'RedHat' and $::operatingsystemmajrelease == '5' { package {'crontabs': ensure => present, } } elsif $::osfamily == 'RedHat' { package {'cronie': ensure => present, } } elsif $::osfamily != 'FreeBSD' { package {'cron': ensure => present, } } cron { 'mysql-backup': ensure => $ensure, command => '/usr/local/sbin/mysqlbackup.sh', user => 'root', hour => $time[0], minute => $time[1], monthday => $time[2], month => $time[3], weekday => $time[4], require => File['mysqlbackup.sh'], } file { 'mysqlbackup.sh': ensure => $ensure, path => '/usr/local/sbin/mysqlbackup.sh', mode => '0700', owner => 'root', group => $mysql::params::root_group, content => template('mysql/mysqlbackup.sh.erb'), } if $mysqlbackupdir_target { file { $backupdir: ensure => $mysqlbackupdir_ensure, target => $mysqlbackupdir_target, mode => $backupdirmode, owner => $backupdirowner, group => $backupdirgroup, } } else { file { $backupdir: ensure => $mysqlbackupdir_ensure, mode => $backupdirmode, owner => $backupdirowner, group => $backupdirgroup, } } } diff --git a/manifests/server/backup.pp b/manifests/server/backup.pp index 7e6e325..0b462b9 100644 --- a/manifests/server/backup.pp +++ b/manifests/server/backup.pp @@ -1,123 +1,125 @@ # @summary # Create and manage a MySQL backup. # # @example Create a basic MySQL backup: # class { 'mysql::server': # root_password => 'password' # } # class { 'mysql::server::backup': # backupuser => 'myuser', # backuppassword => 'mypassword', # backupdir => '/tmp/backups', # } # class { 'mysql::server::backup': # backupmethod => 'mariabackup', # provider => 'xtrabackup', # backupdir => '/tmp/backups', # } # # @param backupuser # MySQL user with backup administrator privileges. # @param backuppassword # Password for `backupuser`. # @param backupdir # Directory to store backup. # @param backupdirmode # Permissions applied to the backup directory. This parameter is passed directly to the file resource. # @param backupdirowner # Owner for the backup directory. This parameter is passed directly to the file resource. # @param backupdirgroup # Group owner for the backup directory. This parameter is passed directly to the file resource. # @param backupcompress # Whether or not to compress the backup (when using the mysqldump provider) # @param backupmethod -# The execution binary for backing up. ex. mysqldump, xtrabackup, mariabackup +# The execution binary for backing up. ex. mysqldump, xtrabackup, mariabackup +# @param backup_success_file_path +# Specify a path where upon successfull backup a file should be created for checking purposes. # @param backuprotate # Backup rotation interval in 24 hour periods. # @param ignore_events # Ignore the mysql.event table. # @param delete_before_dump # Whether to delete old .sql files before backing up. Setting to true deletes old files before backing up, while setting to false deletes them after backup. # @param backupdatabases # Databases to backup (required if using xtrabackup provider). By default `[]` will back up all databases. # @param file_per_database # Use file per database mode creating one file per database backup. # @param include_routines # Dump stored routines (procedures and functions) from dumped databases when doing a `file_per_database` backup. # @param include_triggers # Dump triggers for each dumped table when doing a `file_per_database` backup. # @param ensure # @param time # An array of two elements to set the backup time. Allows ['23', '5'] (i.e., 23:05) or ['3', '45'] (i.e., 03:45) for HH:MM times. # @param prescript # A script that is executed before the backup begins. # @param postscript # A script that is executed when the backup is finished. This could be used to sync the backup to a central store. This script can be either a single line that is directly executed or a number of lines supplied as an array. It could also be one or more externally managed (executable) files. # @param execpath # Allows you to set a custom PATH should your MySQL installation be non-standard places. Defaults to `/usr/bin:/usr/sbin:/bin:/sbin`. # @param provider -# Sets the server backup implementation. Valid values are: +# Sets the server backup implementation. Valid values are: # @param maxallowedpacket # Defines the maximum SQL statement size for the backup dump script. The default value is 1MB, as this is the default MySQL Server value. # @param optional_args # Specifies an array of optional arguments which should be passed through to the backup tool. (Supported by the xtrabackup and mysqldump providers.) -# class mysql::server::backup ( - $backupuser = undef, - $backuppassword = undef, - $backupdir = undef, - $backupdirmode = '0700', - $backupdirowner = 'root', - $backupdirgroup = 'root', - $backupcompress = true, - $backuprotate = 30, - $backupmethod = undef, - $ignore_events = true, - $delete_before_dump = false, - $backupdatabases = [], - $file_per_database = false, - $include_routines = false, - $include_triggers = false, - $ensure = 'present', - $time = ['23', '5'], - $prescript = false, - $postscript = false, - $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', - $provider = 'mysqldump', - $maxallowedpacket = '1M', - $optional_args = [], + $backupuser = undef, + $backuppassword = undef, + $backupdir = undef, + $backupdirmode = '0700', + $backupdirowner = 'root', + $backupdirgroup = 'root', + $backupcompress = true, + $backuprotate = 30, + $backupmethod = undef, + $backup_success_file_path = '/tmp/mysqlbackup_success', + $ignore_events = true, + $delete_before_dump = false, + $backupdatabases = [], + $file_per_database = false, + $include_routines = false, + $include_triggers = false, + $ensure = 'present', + $time = ['23', '5'], + $prescript = false, + $postscript = false, + $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', + $provider = 'mysqldump', + $maxallowedpacket = '1M', + $optional_args = [], ) { if $prescript and $provider =~ /(mysqldump|mysqlbackup)/ { warning(translate("The 'prescript' option is not currently implemented for the %{provider} backup provider.", {'provider' => $provider})) } create_resources('class', { "mysql::backup::${provider}" => { - 'backupuser' => $backupuser, - 'backuppassword' => $backuppassword, - 'backupdir' => $backupdir, - 'backupdirmode' => $backupdirmode, - 'backupdirowner' => $backupdirowner, - 'backupdirgroup' => $backupdirgroup, - 'backupcompress' => $backupcompress, - 'backuprotate' => $backuprotate, - 'backupmethod' => $backupmethod, - 'ignore_events' => $ignore_events, - 'delete_before_dump' => $delete_before_dump, - 'backupdatabases' => $backupdatabases, - 'file_per_database' => $file_per_database, - 'include_routines' => $include_routines, - 'include_triggers' => $include_triggers, - 'ensure' => $ensure, - 'time' => $time, - 'prescript' => $prescript, - 'postscript' => $postscript, - 'execpath' => $execpath, - 'maxallowedpacket' => $maxallowedpacket, - 'optional_args' => $optional_args, + 'backupuser' => $backupuser, + 'backuppassword' => $backuppassword, + 'backupdir' => $backupdir, + 'backupdirmode' => $backupdirmode, + 'backupdirowner' => $backupdirowner, + 'backupdirgroup' => $backupdirgroup, + 'backupcompress' => $backupcompress, + 'backuprotate' => $backuprotate, + 'backupmethod' => $backupmethod, + 'backup_success_file_path' => $backup_success_file_path, + 'ignore_events' => $ignore_events, + 'delete_before_dump' => $delete_before_dump, + 'backupdatabases' => $backupdatabases, + 'file_per_database' => $file_per_database, + 'include_routines' => $include_routines, + 'include_triggers' => $include_triggers, + 'ensure' => $ensure, + 'time' => $time, + 'prescript' => $prescript, + 'postscript' => $postscript, + 'execpath' => $execpath, + 'maxallowedpacket' => $maxallowedpacket, + 'optional_args' => $optional_args, } }) - } diff --git a/spec/classes/mysql_server_backup_spec.rb b/spec/classes/mysql_server_backup_spec.rb index b7ff1d6..02c9fa9 100644 --- a/spec/classes/mysql_server_backup_spec.rb +++ b/spec/classes/mysql_server_backup_spec.rb @@ -1,361 +1,388 @@ require 'spec_helper' describe 'mysql::server::backup' do on_supported_os.each do |os, facts| context "on #{os}" do let(:pre_condition) do <<-EOF class { 'mysql::server': } EOF end let(:facts) do facts.merge(root_home: '/root') end let(:default_params) do { 'backupuser' => 'testuser', 'backuppassword' => 'testpass', 'backupdir' => '/tmp/mysql-backup', 'backuprotate' => '25', 'delete_before_dump' => true, 'execpath' => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin', 'maxallowedpacket' => '1M' } end context 'standard conditions' do let(:params) { default_params } # Cannot use that_requires here, doesn't work on classes. it { is_expected.to contain_mysql_user('testuser@localhost').with( require: 'Class[Mysql::Server::Root_password]', ) } it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with( privileges: ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS'], ).that_requires('Mysql_user[testuser@localhost]') } context 'with triggers included' do let(:params) do { include_triggers: true }.merge(default_params) end it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with( privileges: ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS', 'TRIGGER'], ).that_requires('Mysql_user[testuser@localhost]') } end it { is_expected.to contain_cron('mysql-backup').with( command: '/usr/local/sbin/mysqlbackup.sh', ensure: 'present', ) } it { is_expected.to contain_file('mysqlbackup.sh').with( path: '/usr/local/sbin/mysqlbackup.sh', ensure: 'present', ) } it { is_expected.to contain_file('/tmp/mysql-backup').with( ensure: 'directory', ) } it 'has compression by default' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{bzcat -zc}, ) end it 'skips backing up events table by default' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="--ignore-table=mysql.event"}, ) end it 'does not mention triggers by default because file_per_database is false' do is_expected.to contain_file('mysqlbackup.sh').without_content( %r{.*triggers.*}, ) end it 'does not mention routines by default because file_per_database is false' do is_expected.to contain_file('mysqlbackup.sh').without_content( %r{.*routines.*}, ) end it 'has 25 days of rotation' do # MySQL counts from 0 is_expected.to contain_file('mysqlbackup.sh').with_content(%r{.*ROTATE=24.*}) end it 'has a standard PATH' do is_expected.to contain_file('mysqlbackup.sh').with_content(%r{PATH=/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin}) end end + context 'with delete after dump' do + let(:custom_params) do + { + 'delete_before_dump' => false, + } + end + let(:params) do + default_params.merge!(custom_params) + end + + it { is_expected.to contain_file('mysqlbackup.sh').with_content(%r{touch /tmp/mysqlbackup_success}) } + end + + context 'with delete after dump and custom success file path' do + let(:custom_params) do + { + 'delete_before_dump' => false, + 'backup_success_file_path' => '/opt/mysqlbackup_success', + } + end + let(:params) do + default_params.merge!(custom_params) + end + + it { is_expected.to contain_file('mysqlbackup.sh').with_content(%r{touch /opt/mysqlbackup_success}) } + end + context 'custom ownership and mode for backupdir' do let(:params) do { backupdirmode: '0750', backupdirowner: 'testuser', backupdirgroup: 'testgrp' }.merge(default_params) end it { is_expected.to contain_file('/tmp/mysql-backup').with( ensure: 'directory', mode: '0750', owner: 'testuser', group: 'testgrp', ) } end context 'with compression disabled' do let(:params) do { backupcompress: false }.merge(default_params) end it { is_expected.to contain_file('mysqlbackup.sh').with( path: '/usr/local/sbin/mysqlbackup.sh', ensure: 'present', ) } it 'is able to disable compression' do is_expected.to contain_file('mysqlbackup.sh').without_content( %r{.*bzcat -zc.*}, ) end end context 'with mysql.events backedup' do let(:params) do { ignore_events: false }.merge(default_params) end it { is_expected.to contain_file('mysqlbackup.sh').with( path: '/usr/local/sbin/mysqlbackup.sh', ensure: 'present', ) } it 'is able to backup events table' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="--events"}, ) end end context 'with database list specified' do let(:params) do { backupdatabases: ['mysql'] }.merge(default_params) end it { is_expected.to contain_file('mysqlbackup.sh').with( path: '/usr/local/sbin/mysqlbackup.sh', ensure: 'present', ) } it 'has a backup file for each database' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date'}, ) end it 'skips backup triggers by default' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"}, ) end it 'skips backing up routines by default' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"}, ) end context 'with include_triggers set to true' do let(:params) do default_params.merge(backupdatabases: ['mysql'], include_triggers: true) end it 'backups triggers when asked' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"}, ) end end context 'with include_triggers set to false' do let(:params) do default_params.merge(backupdatabases: ['mysql'], include_triggers: false) end it 'skips backing up triggers when asked to skip' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"}, ) end end context 'with include_routines set to true' do let(:params) do default_params.merge(backupdatabases: ['mysql'], include_routines: true) end it 'backups routines when asked' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"}, ) end end context 'with include_routines set to false' do let(:params) do default_params.merge(backupdatabases: ['mysql'], include_triggers: true) end it 'skips backing up routines when asked to skip' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"}, ) end end end context 'with file per database' do let(:params) do default_params.merge(file_per_database: true) end it 'loops through backup all databases' do is_expected.to contain_file('mysqlbackup.sh').with_content(%r{.*SHOW DATABASES.*}) end context 'with compression disabled' do let(:params) do default_params.merge(file_per_database: true, backupcompress: false) end it 'loops through backup all databases without compression #show databases' do is_expected.to contain_file('mysqlbackup.sh').with_content(%r{.*SHOW DATABASES.*}) end it 'loops through backup all databases without compression #bzcat' do is_expected.to contain_file('mysqlbackup.sh').without_content(%r{.*bzcat -zc.*}) end end it 'skips backup triggers by default' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"}, ) end it 'skips backing up routines by default' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"}, ) end context 'with include_triggers set to true' do let(:params) do default_params.merge(file_per_database: true, include_triggers: true) end it 'backups triggers when asked' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"}, ) end end context 'with include_triggers set to false' do let(:params) do default_params.merge(file_per_database: true, include_triggers: false) end it 'skips backing up triggers when asked to skip' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"}, ) end end context 'with include_routines set to true' do let(:params) do default_params.merge(file_per_database: true, include_routines: true) end it 'backups routines when asked' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"}, ) end end context 'with include_routines set to false' do let(:params) do default_params.merge(file_per_database: true, include_triggers: true) end it 'skips backing up routines when asked to skip' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"}, ) end end end context 'with postscript' do let(:params) do default_params.merge(postscript: 'rsync -a /tmp backup01.local-lan:') end it 'is add postscript' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{rsync -a \/tmp backup01.local-lan:}, ) end end context 'with postscripts' do let(:params) do default_params.merge(postscript: [ 'rsync -a /tmp backup01.local-lan:', 'rsync -a /tmp backup02.local-lan:', ]) end it 'is add postscript' do is_expected.to contain_file('mysqlbackup.sh').with_content( %r{.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*}, ) end end end end # rubocop:enable RSpec/NestedGroups end diff --git a/templates/mysqlbackup.sh.erb b/templates/mysqlbackup.sh.erb index 77a437f..19706fb 100755 --- a/templates/mysqlbackup.sh.erb +++ b/templates/mysqlbackup.sh.erb @@ -1,125 +1,125 @@ <%- if @kernel == 'Linux' -%> #!/bin/bash <%- else -%> #!/bin/sh <%- end -%> # # 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 %>' MAX_ALLOWED_PACKET=<%= @maxallowedpacket %> DIR=<%= @backupdir %> ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %> # Create temporary mysql cnf file. TMPFILE=`mktemp /tmp/backup.XXXXXX` || exit 1 <%- if @kernel == 'SunOS' -%> echo "[client]\npassword=$PASS\nuser=$USER\nmax_allowed_packet=$MAX_ALLOWED_PACKET" > $TMPFILE <%- else -%> echo -e "[client]\npassword=$PASS\nuser=$USER\nmax_allowed_packet=$MAX_ALLOWED_PACKET" > $TMPFILE <%- end -%> <% if @prescript -%> <%- [@prescript].flatten.compact.each do |script|%> <%= script %> <%- end -%> <% end -%> # Ensure backup directory exist. mkdir -p $DIR PREFIX=mysql_backup_ <% if @ignore_events %> ADDITIONAL_OPTIONS="--ignore-table=mysql.event" <% else %> ADDITIONAL_OPTIONS="--events" <% end %> <%# Only include routines or triggers if we're doing a file per database -%> <%# backup. This happens if we named databases, or if we explicitly set -%> <%# file per database mode -%> <% if !@backupdatabases.empty? || @file_per_database -%> <% if @include_triggers -%> ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --triggers" <% else -%> ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-triggers" <% end -%> <% if @include_routines -%> ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --routines" <% else -%> ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-routines" <% end -%> <% end -%> <%- if @optional_args and @optional_args.is_a?(Array) -%> <%- @optional_args.each do |arg| -%> ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS <%= arg %>" <%- end -%> <%- end -%> ##### STOP CONFIG #################################################### PATH=<%= @execpath %> <%- if @kernel == 'Linux' -%> set -o pipefail <%- end -%> cleanup() { <%- if @kernel == 'SunOS' -%> gfind "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | gxargs -0 -r rm -f <%- else -%> find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f <%- end -%> } <% if @delete_before_dump -%> cleanup <% end -%> <% if @backupdatabases.empty? -%> <% if @file_per_database -%> mysql --defaults-extra-file=$TMPFILE -s -r -N -e 'SHOW DATABASES' | while read dbname do <%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \ ${ADDITIONAL_OPTIONS} \ ${dbname} <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %> done <% else -%> <%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \ ${ADDITIONAL_OPTIONS} \ --all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %> <% end -%> <% else -%> <% @backupdatabases.each do |db| -%> <%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \ ${ADDITIONAL_OPTIONS} \ <%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %> <% end -%> <% end -%> <% unless @delete_before_dump -%> if [ $? -eq 0 ] ; then cleanup - touch /tmp/mysqlbackup_success + touch <%= @backup_success_file_path %> fi <% end -%> <% if @postscript -%> <%- [@postscript].flatten.compact.each do |script|%> <%= script %> <%- end -%> <% end -%> # Remove temporary file rm -f $TMPFILE diff --git a/templates/xtrabackup.sh.erb b/templates/xtrabackup.sh.erb index de53197..bc01ba9 100644 --- a/templates/xtrabackup.sh.erb +++ b/templates/xtrabackup.sh.erb @@ -1,71 +1,71 @@ <%- if @kernel == 'Linux' -%> #!/bin/bash <%- else -%> #!/bin/sh <%- end -%> # # A wrapper for Xtrabackup ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %> DIR=<%= @backupdir %> # Ensure backup directory exist. mkdir -p $DIR <%- if @kernel == 'Linux' -%> set -o pipefail <%- end -%> <% if @prescript -%> <%- [@prescript].flatten.compact.each do |script| %> <%= script %> <%- end -%> <% end -%> cleanup() { <%- if @kernel == 'SunOS' -%> gfind "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | gxargs -0 -r rm -f <%- else -%> find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f <%- end -%> } <% if @delete_before_dump -%> cleanup <% end -%> <%- _innobackupex_args = '' -%> <%- if @backupuser and @backuppassword -%> <%- _innobackupex_args = '--user="' + @backupuser + '" --password="' + @backuppassword + '"' -%> <%- end -%> <%- if @backupdatabases and @backupdatabases.is_a?(Array) and !@backupdatabases.empty? -%> <%- _innobackupex_args = _innobackupex_args + ' --databases="' + @backupdatabases.join(' ') + '"' -%> <%- end -%> <%- if @optional_args and @optional_args.is_a?(Array) -%> <%- @optional_args.each do |arg| -%> <%- _innobackupex_args = _innobackupex_args + ' ' + arg -%> <%- end -%> <%- end -%> <%= @backupmethod -%> <%= _innobackupex_args %> $@ <% unless @delete_before_dump -%> if [ $? -eq 0 ] ; then cleanup - touch /tmp/mysqlbackup_success + touch <%= @backup_success_file_path %> fi <% end -%> <% if @postscript -%> <%- [@postscript].flatten.compact.each do |script| %> <%= script %> <%- end -%> <% end -%>