Sha256: af56933ec3304305638068c72886d80c1ad741ec825ca4b9a16280badaf37e6d

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 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?
        Outback.info "Archived database #{database}"
        TempArchive.new(archive_name, self).tap { |archive| Outback.debug "dumped #{archive.filename.basename} with #{archive.size} bytes" }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
outback-0.0.14 lib/outback/mysql_source.rb
outback-0.0.13 lib/outback/mysql_source.rb
outback-0.0.12 lib/outback/mysql_source.rb
outback-0.0.11 lib/outback/mysql_source.rb
outback-0.0.10 lib/outback/mysql_source.rb
outback-0.0.9 lib/outback/mysql_source.rb
outback-0.0.8 lib/outback/mysql_source.rb