Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Created July 25, 2008 03:44
Show Gist options
  • Save deadprogram/2376 to your computer and use it in GitHub Desktop.
Save deadprogram/2376 to your computer and use it in GitHub Desktop.
=begin rdoc
PoolParty plugin to install and configure mysql on any instance designated as a database instance.
Written by Ron Evans (http://deadprogrammersociety.com)
Features of the current plugin:
- Installs current Mysql
- Does live database backup to S3 once every hour
- If the instance goes down, will automatically bring up another one, and restore latest backup
Limitations of the current plugin:
- Allows for one and only one database instance.
- The size of the database cannot exceed the amount of disk storage available to that instance size
- Backups are going against the same production database, so performance may be impacted
=end
require 'poolparty'
class Mysqler < PoolParty::Plugin
before_configure_cloud :upload_config_files
after_configure_cloud :load_db_settings, :restore_database, :start_monit, :setup_backup
attr_reader :db_settings
define_custom_package :mysql do
# Install mysql
package :mysql, :provides => :database do
description 'MySQL Database'
apt %w( mysql-server mysql-client )
requires :monit
end
# Install monit
package :monit do
description 'Monit monitoring tool'
apt 'monit'
end
end
define_global_file "mysql-backup" do
load_db_settings
"00 * * * * /usr/bin/mysqldump -u #{@db_settings[:user]} -p#{@db_settings[:password]} -x --all-databases > /data/#{@db_settings[:backup_dir]}/db_dump.sql"
end
# uploads mysql and monit config files to server instance
def upload_config_files(c)
c.scp("#{File.dirname(File.expand_path(__FILE__))}/my.cnf", "/etc/my.cnf")
c.scp("#{File.dirname(File.expand_path(__FILE__))}/mysqler.monit.conf", "/etc/monit.d/mysqler.monit.conf")
end
# load settings for plugin
def load_db_settings(c=nil)
@db_settings ||= read_config_file("#{PoolParty.user_dir}/config/mysql.yml")
end
# restore the mysql database from backups
def restore_database(c)
c.ssh "mysql -u #{@db_settings[:user]} -p#{@db_settings[:password]} < /data/#{@db_settings[:backup_dir]}/db_dump.sql"
end
# setup cron job to perform mysql database backup to "/data/#{db_settings[:backup_dir]}/db_dump.sql"
def setup_backup(c)
c.ssh "cp #{c.base_tmp_dir}/mysql-backup /etc/cron.d/mysql-backup"
end
# start monit, which will also start mysql
def start_monit(c)
c.ssh "/etc/init.d/monit start"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment