Created
July 13, 2009 18:13
-
-
Save jimmybaker/146350 to your computer and use it in GitHub Desktop.
A custom backup script for mysql/ruby on rails, written in ruby.
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'yaml' | |
require 'logger' | |
require 'rubygems' | |
require 'net/scp' | |
class Backup | |
def self.directory | |
@@directory ||= File.dirname(__FILE__) | |
end | |
attr_reader :apps, :logger, :remote_dir, :remote_user, :remote_host | |
attr_accessor :app_prefix | |
def initialize(*args) | |
@remote_dir = args.pop if args.last.class.to_s != 'App' | |
@remote_user = args.pop if args.last.class.to_s != 'App' | |
@remote_host = args.pop if args.last.class.to_s != 'App' | |
raise 'Please provide remote host, user and directory as last arguments' if @remote_host.nil? || @remote_user.nil? || @remote_dir.nil? | |
for_apps(args) | |
@logger = Logger.new('%s/database.log' % Backup.directory) | |
end | |
def for_apps(*args) | |
@apps = args.flatten | |
end | |
def move_backup_cmd(app) | |
"scp #{self.class.directory}/#{app.dump_name} #{remote_user}@#{remote_host}:#{remote_dir}/#{app.dump_name}" | |
end | |
def remove_local_backup_cmd(app) | |
"rm -f #{self.class.directory}/#{app.dump_name}" | |
end | |
def execute! | |
apps.each do |app| | |
logger.info 'Getting ready to create a backup' | |
logger.info %x[#{app.mysql_dump_cmd(self.class.directory)}] | |
logger.info 'Backup created, starting the transfer offsite' | |
logger.info %x[#{move_backup_cmd(app)}] | |
logger.info 'Finished transferring backup offsite' | |
logger.info 'Removing local file' | |
logger.info %x[#{remove_local_backup_cmd(app)}] | |
logger.info 'Local file removed' | |
end | |
end | |
end | |
class App | |
attr_accessor :name, :stamp | |
attr_writer :path_prefix | |
def initialize(name, path_prefix=nil) | |
@name = name | |
@path_prefix = path_prefix | |
@stamp = Time.now.strftime('%Y%m%d%H%M%S') | |
end | |
def dump_name | |
'%s_%s.sql.gz' % [name, stamp] | |
end | |
def full_path | |
"#{path_prefix}/#{name}/current" | |
end | |
def path_prefix | |
@path_prefix ||= '/var/www/apps' | |
end | |
def db | |
@db_config ||= YAML::load(open(full_path + "/config/database.yml"))['production'] | |
end | |
def mysql_dump_cmd(backup_dir) | |
cmd = "mysqldump -u #{db['username']} -p#{db['password']}" | |
cmd += " -h #{db['host']}" if db['host'] | |
cmd += " --add-drop-table --add-locks --extended-insert --lock-tables #{db['database']} | gzip -cf9 > #{backup_dir}/#{dump_name}" | |
cmd | |
end | |
end | |
if $0 == __FILE__ | |
Backup.new(App.new('appname'), 'server.com', 'user', 'directory').execute! | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment