Skip to content
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

Backup on MySQL DB slave if it is available #249

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions cookbooks/ey-backup/recipes/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,45 @@
@encryption_command = ""

node.engineyard.environment['apps'].each do |app|
app['components'].each do |component|
if component['key'] == 'encrypted_backup'
include_recipe 'ey-backup::encrypted'
app['components'].each do |component|
if component['key'] == 'encrypted_backup'
include_recipe 'ey-backup::encrypted'

@long_key_id = ""
@long_key_id = ""

gpg_keys = Mixlib::ShellOut.new("gpg --list-keys --with-colon")
gpg_keys.run_command
gpg_keys.stdout.each_line do |line|
gpg_keys = Mixlib::ShellOut.new("gpg --list-keys --with-colon")
gpg_keys.run_command
gpg_keys.stdout.each_line do |line|
if line[0..2] == 'pub'
@long_key_id = line.split(':')[4]
end
end

@encryption_command = "-k #{@long_key_id}" unless @long_key_id.empty?
@encryption_command = "-k #{@long_key_id}" unless @long_key_id.empty?
end
end
end
end

if node.dna['backup_window'] != 0 && ['db_master','solo'].include?(node.dna['instance_role'])
has_backups_enabled = node.dna['backup_window'] != 0
db_slaves_available = node.dna['db_slaves'].any?
is_db_master_or_solo = ['db_master','solo'].include?(node.dna['instance_role'])
is_db_slave = 'db_slave' == node.dna['instance_role']
is_first_slave = node.dna['db_slaves'].first == (node['ec2'] && node['ec2']['local_hostname'] ? node['ec2']['local_hostname'] : hostname.stdout)

if has_backups_enabled && (db_slaves_available && is_db_slave && is_first_slave || !db_slaves_available && is_db_master_or_solo)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_db_slave is redundant in this test - we can just check:

if has_backups_enabled && (db_slaves_available && is_first_slave || !db_slaves_available && is_db_master_or_solo)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been changed.

encryption_command = @encryption_command
backup_command = "eybackup -e mysql #{encryption_command} >> /var/log/eybackup.log 2>&1"

cron "mysql" do
cron 'mysql' do
command backup_command
month '*'
weekday '*'
day '*'
hour node['backup_hour'] # this attribute is set by ey-base/attributes/snapshot_and_backup_intervals.rb
minute node['backup_minute']
end
else
cron 'mysql' do
action :delete
end
end
51 changes: 18 additions & 33 deletions cookbooks/ey-backup/recipes/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,26 @@
end
end

if node.dna['db_slaves'].empty? && node.dna['backup_window'] != 0
# No slaves detected, put the backup on the solo/db_master if backups are enabled
if ['db_master','solo'].include?(node.dna['instance_role'])
encryption_command = @encryption_command
has_backups_enabled = node.dna['backup_window'] != 0
db_slaves_available = node.dna['db_slaves'].any?
is_db_master_or_solo = ['db_master','solo'].include?(node.dna['instance_role'])
is_db_slave = 'db_slave' == node.dna['instance_role']
is_first_slave = node.dna['db_slaves'].first == (node['ec2'] && node['ec2']['local_hostname'] ? node['ec2']['local_hostname'] : hostname.stdout)

backup_cron "postgresql" do
command "eybackup -e postgresql #{encryption_command} >> /var/log/eybackup.log 2>&1"
month '*'
weekday '*'
day '*'
hour node['backup_hour'] # this attribute is set by ey-base/attributes/snapshot_and_backup_intervals.rb
minute node['backup_minute']
end
if has_backups_enabled && (db_slaves_available && is_db_slave && is_first_slave || !db_slaves_available && is_db_master_or_solo)
encryption_command = @encryption_command

cron 'postgresql' do
command "eybackup -e postgresql #{encryption_command} >> /var/log/eybackup.log 2>&1"
month '*'
weekday '*'
day '*'
hour node['backup_hour'] # this attribute is set by ey-base/attributes/snapshot_and_backup_intervals.rb
minute node['backup_minute']
end
# Slaves detected, put them on the db_slave if backups are enabled
else
if ['db_slave'].include?(node.dna['instance_role']) && node.dna['backup_window'] != 0
db_slave1_fqdn=node.dna['db_slaves'].first
encryption_command = @encryption_command

hostname = Mixlib::ShellOut.new("hostname")
hostname.run_command

cron "postgresql" do
command "eybackup -e postgresql #{encryption_command} >> /var/log/eybackup.log 2>&1"
month '*'
weekday '*'
day '*'
hour node['backup_hour'] # this attribute is set by ey-base/attributes/snapshot_and_backup_intervals.rb
minute node['backup_minute']
only_if {(node['ec2'] && node['ec2']['local_hostname'] ? node['ec2']['local_hostname'] : hostname.stdout) == db_slave1_fqdn}
end
else
cron "postgresql" do
action :delete
end
cron 'postgresql' do
user cronjob_user
action :delete
end
end