Sha256: 71113fe624fe2b608c448267ceed998e04ea09543daf16b10be98bf74da7d181

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

module Outback
  class MysqlSource < Source
    attr_setter :user, :password, :host, :port, :socket
    
    def databases
      @databases ||= []
    end
    
    def excludes
      @excludes ||= []
    end
    
    def database(*names)
      databases.concat(names.map(&:to_s)).uniq!
    end
    
    def exclude(*names)
      excludes.concat(names.map(&:to_s)).uniq!
    end
    
    def valid?
      user && password
    end
    
    def create_archives(backup_name, timestamp, tmpdir)
      mysql_host = host || 'localhost'
      mysql_port = (port || 3306) unless socket
      if databases.empty?
        #        (host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=nil)
        mysql = Mysql.connect(mysql_host, user, password, nil, mysql_port, socket)
        @databases = mysql.databases - excludes
        mysql.close
      end
      
      archives = databases.collect do |database|
        archive_name = Pathname.new(tmpdir).join("#{backup_name}_#{timestamp}_#{database}.sql.gz")
        mysql_conf_file = Pathname.new(tmpdir).join('outback_my.cnf')
        File.open(mysql_conf_file, 'w') { |f| f << "[client]\npassword=#{password}\n" }
        FileUtils.chmod 0600, mysql_conf_file
        Outback.debug "MysqlSource: dumping database '#{database}'"
        commandline = "mysqldump --defaults-extra-file=#{mysql_conf_file} --opt --user=#{user} --host=#{mysql_host} --port=#{mysql_port} #{database} | gzip > #{archive_name}"
        result = `#{commandline}`.strip
        Outback.debug(result) unless result.blank?
        TempArchive.new(archive_name, self).tap { |archive| Outback.debug "dumped #{archive.filename.basename} with #{archive.size} bytes" }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
outback-0.0.6 lib/outback/mysql_source.rb
outback-0.0.5 lib/outback/mysql_source.rb
outback-0.0.4 lib/outback/mysql_source.rb
outback-0.0.3 lib/outback/mysql_source.rb