-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from SUNET/jocar-mariadb-backup
Replicated backup node for MariaDB
- Loading branch information
Showing
10 changed files
with
166 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,12 @@ | ||
# frozen_string_literal: true | ||
|
||
|
||
# Convert a single IPv4 address to an int | ||
|
||
require 'ipaddr' | ||
|
||
Puppet::Functions.create_function(:ipv4_to_int) do | ||
def ipv4_to_int(*arguments) | ||
IPAddr.new(arguments[0]).to_i | ||
end | ||
end |
This file contains 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 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,80 @@ | ||
# This is a asyncronous replica of the Maria DB Cluster for SUNET | ||
class sunet::mariadb::backup( | ||
String $mariadb_version=latest, | ||
Array[String] $dns = [], | ||
Boolean $backup_to_baas = true, | ||
Boolean $nrpe = true, | ||
) { | ||
|
||
sunet::mariadb { 'sunet_mariadb_simple': | ||
mariadb_version => $mariadb_version, | ||
ports => [3306], | ||
dns => $dns, | ||
galera => false, | ||
} | ||
|
||
$cluster_nodes = lookup('mariadb_cluster_nodes', undef, undef,[]) | ||
$replicate_from = $cluster_nodes[0] | ||
|
||
# Secrets from local.eyaml | ||
$mariadb_root_password = safe_hiera('mariadb_root_password') | ||
$mariadb_backup_password = safe_hiera('mariadb_root_password') | ||
$mariadb_user_password = safe_hiera('mariadb_user_password') | ||
|
||
file { '/opt/mariadb/scripts/start_replica_from_init.sh': | ||
ensure => present, | ||
content => template('sunet/mariadb/backup/start_replica_from_init.erb.sh'), | ||
mode => '0744', | ||
} | ||
file { '/opt/mariadb/scripts/do_backup.sh': | ||
ensure => present, | ||
content => template('sunet/mariadb/backup/do_backup.erb.sh'), | ||
mode => '0744', | ||
} | ||
|
||
if $backup_to_baas { | ||
file { '/usr/local/bin/backup2baas': | ||
ensure => present, | ||
content => template('sunet/mariadb/backup/backup2baas.erb'), | ||
mode => '0744', | ||
} | ||
|
||
sunet::scriptherder::cronjob { 'backup2baas': | ||
cmd => '/usr/local/bin/backup2baas', | ||
hour => '6', | ||
minute => '10', | ||
ok_criteria => ['exit_status=0', 'max_age=24h'], | ||
warn_criteria => ['exit_status=1'], | ||
} | ||
} | ||
|
||
file { '/usr/lib/nagios/plugins/check_mariadb-replication': | ||
ensure => present, | ||
content => template('sunet/mariadb/backup/check_replication.erb'), | ||
mode => '0744', | ||
} | ||
file { '/usr/local/bin/mariadb-replication-status': | ||
ensure => present, | ||
content => template('sunet/mariadb/backup/replication-status.erb'), | ||
mode => '0744', | ||
} | ||
file { '/etc/sudoers.d/99-mariadb-replication-test': | ||
ensure => file, | ||
content => "script ALL=(root) NOPASSWD: /usr/local/bin/mariadb-replication-status", | ||
mode => '0440', | ||
owner => 'root', | ||
group => 'root', | ||
} | ||
|
||
if $nrpe { | ||
sunet::sudoer {'nagios_run_replication_command': | ||
user_name => 'nagios', | ||
collection => 'nrpe_replication_check', | ||
command_line => '/usr/lib/nagios/plugins/check_mariadb-replication' | ||
} | ||
sunet::nagios::nrpe_command {'check_async_replication': | ||
command_line => '/usr/bin/sudo /usr/lib/nagios/plugins/check_mariadb-replication' | ||
} | ||
} | ||
|
||
} |
This file contains 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,11 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
docker exec mariadb-db-1 /scripts/do_backup.sh | ||
|
||
BACKUPDIR=/opt/mariadb/backups | ||
find "${BACKUPDIR}" -type f -mtime +31 -exec rm -f {} \; | ||
find "${BACKUPDIR}" -empty -type d -delete | ||
|
||
/usr/bin/dsmc backup |
This file contains 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,10 @@ | ||
#!/bin/bash | ||
|
||
result="$(/usr/local/bin/mariadb-replication-status)" | ||
if [[ "${result}" == "Slave_running ON" ]]; then | ||
echo "OK: Replica running" | ||
exit 0 | ||
else | ||
echo "CRITICAL: Replica not running" | ||
exit 2 | ||
fi |
This file contains 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,12 @@ | ||
#!/bin/bash | ||
stream_name="mariadb-stream-$(date +%Y-%m-%dT%H.%M.%S).gz" | ||
dump_name="mariadb-dump-$(date +%Y-%m-%dT%H.%M.%S).sql.gz" | ||
backup_dir="/backups/$(date +%Y/%m/%d)" | ||
mkdir -p "${backup_dir}" | ||
|
||
buopts="--slave-info --safe-slave-backup" | ||
dumpopts="--dump-slave" | ||
mysql -p"${MYSQL_ROOT_PASSWORD}" -e "stop slave" | ||
mariadb-backup --backup ${buopts} -u root -p"${MYSQL_ROOT_PASSWORD}" --stream=xbstream | gzip >"${backup_dir}/${stream_name}" | ||
mysqldump --all-databases --single-transaction ${dumpopts} -u root -p${MYSQL_ROOT_PASSWORD} | gzip >"${backup_dir}/${dump_name}" | ||
mysql -p${MYSQL_ROOT_PASSWORD} -e "start slave" |
This file contains 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,3 @@ | ||
#!/bin/bash | ||
|
||
docker exec mariadb-db-1 mysql -u root -p'<%= @mariadb_root_password %>' -N -B -e "show status like 'Slave_running'" |
This file contains 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,17 @@ | ||
#!/usr/bin/env bash | ||
mysql="mysql -u root -p${MYSQL_ROOT_PASSWORD}" | ||
init_file='/backups/init.sql.gz' | ||
if [[ -f ${init_file} ]]; then | ||
${mysql} -e "STOP SLAVE;RESET SLAVE;" | ||
master_command=$(zgrep 'CHANGE MASTER TO MASTER_LOG_FILE' ${init_file} | sed -e 's/^-- //' -e 's/;$//') | ||
master_command="${master_command}, MASTER_HOST='<%= @replicate_from %>', MASTER_USER='backup'" | ||
master_command="${master_command}, MASTER_PASSWORD='<%= @mariadb_backup_password%>', MASTER_SSL=1" | ||
master_command="${master_command}, MASTER_CONNECT_RETRY=20" | ||
zcat ${init_file} | ${mysql} | ||
${mysql} -e "${master_command}" | ||
${mysql} -e "START SLAVE" | ||
sleep 3s | ||
${mysql} -e "SHOW SLAVE STATUS\G" | ||
fi | ||
|
||
exit 0 |
This file contains 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 |
---|---|---|
|
@@ -11,7 +11,12 @@ services: | |
- /opt/mariadb/datadir:/var/lib/mysql | ||
- /opt/mariadb/init:/docker-entrypoint-initdb.d | ||
- /opt/mariadb/scripts:/scripts | ||
<%- if @backup -%> | ||
- /opt/mariadb_backup/start_replica_from_init.sh:/start_replica_from_init.sh | ||
<% end -%> | ||
<%- if @galera -%> | ||
network_mode: host | ||
<% end -%> | ||
<%- if [email protected]? -%> | ||
dns: | ||
<% @dns.each do |resolver| -%> | ||
|
@@ -29,8 +34,10 @@ services: | |
<%- if @mariadb_database -%> | ||
- MYSQL_DATABASE=<%= @mariadb_database %> | ||
<%- end -%> | ||
<%- if @galera -%> | ||
- BOOTSTRAP=<%= @bootstrap %> | ||
- FORCE_BOOTSTRAP=0 | ||
command: "--wsrep_cluster_address=gcomm://<%= @cluster_nodes.join(',') %>" | ||
tty: true | ||
<%- end -%> | ||
|
This file contains 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