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 all 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
33 changes: 22 additions & 11 deletions cookbooks/ey-backup/recipes/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,46 @@
@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'])

# The backup target is the first db-slave, we do this to avoid multiple db-slaves doing backups
is_backup_target = 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_backup_target || !db_slaves_available && is_db_master_or_solo)
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
52 changes: 19 additions & 33 deletions cookbooks/ey-backup/recipes/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,27 @@
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'])

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
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
# The backup target is the first db-slave, we do this to avoid multiple db-slaves doing backups
is_backup_target = node.dna['db_slaves'].first == (node['ec2'] && node['ec2']['local_hostname'] ? node['ec2']['local_hostname'] : hostname.stdout)

hostname = Mixlib::ShellOut.new("hostname")
hostname.run_command
if has_backups_enabled && (db_slaves_available && is_backup_target || !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']
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
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
else
cron 'postgresql' do
user cronjob_user
action :delete
end
end