Skip to content

Instantly share code, notes, and snippets.

@Veraticus
Created May 30, 2014 21:27
Show Gist options
  • Save Veraticus/e78039819ada0b4ecb9e to your computer and use it in GitHub Desktop.
Save Veraticus/e78039819ada0b4ecb9e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
dump_blacklist = %w[
authentications
activity_interactions
activity_participants
campaign_participants
campaign_participants_archive
challenge_participants
challenge_participants_archive
oauth_access_grants
oauth_access_tokens
moments
usage_snapshots
leaders
users
winners
].map { |table_name| "'#{table_name}'" }.join(',')
dump_binary = `which mysqldump`
mysql_binary = `which mysql`
env = stack.chef_environment
user = stack.custom_json[:database][:user]
password = stack.custom_json[:database][:password]
host = stack.custom_json[:database][:host]
creds = "-h #{host} -u #{user} --password=#{password}"
database = stack.custom_json[:database][:name]
dump_file = Tempfile.new
commands = []
# Select the set complement of the blacklist tables;
# i.e. all the other tables in the database that we want to dump.
commands << "TO_DUMP=`#{mysql_binary} #{creds} -AN -e\"SELECT group_concat(table_name separator ' ') "\
"FROM information_schema.tables WHERE table_schema='#{database}' AND "\
"table_name NOT IN (#{dump_blacklist})\"`"
# Dump the schema.
commands << "#{dump_binary} #{creds} -d #{database} > #{dump_file}"
# Dump out tables (i.e. generate INSERT statements) excluding blacklisted tables.
commands << "#{dump_binary} #{creds} -t #{database} ${TO_DUMP} >> #{dump_file}"
commands << "cat /tmp/schema.sql | /bin/gzip > /tmp/#{env}_foo_db_dump.sql.gz"
command = commands.join('; ')
`#{command}`
puts 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment