<commit_before>require 'fog'
require 'active_support/all'
require 'dotenv'
require 'mysql2'
require 'tempfile'
require 'yaml'

Dotenv.load(".env", ".mysql.env")

class Cloudfile
  attr_reader :host, :db, :filename, :key
  def initialize f
    @cfile = f
    @key = f.key
    bits = @key.split "/"
    @host = bits[0]
    @db = bits[1]
    @filename = bits[2]
  end

  def download
    tmpfile = Dir::Tmpname.make_tmpname Dir.tmpdir+File::Separator, nil
    derp = File.open tmpfile, "w"
    derp.write @cfile.body

    tmpfile
  end
end

class Hoppler

  def self.hostname
    `hostname`.strip
  end    
  
  def self.perform      
    results = self.connection.query("show databases")
    databases = results.map{|row| row['Database']}
    
    databases.each do |database|
      begin
        # Dump to tempfile
        tmpfile = Dir::Tmpname.make_tmpname Dir.tmpdir+File::Separator, nil
        dump_cmd = "mysqldump #{database} -u #{ENV['MYSQL_USERNAME']} --single-transaction"
        dump_cmd << " --password=#{ENV['MYSQL_PASSWORD']}" if ENV['MYSQL_PASSWORD']
        system "#{dump_cmd} | bzip2 > #{tmpfile}"
        # Write to rackspace
        dir = self.rackspace.directories.get ENV['RACKSPACE_DB_CONTAINER']
        filename = "#{hostname}/#{database}/#{DateTime.now.strftime("%F")}.sql.bz2"
        dir.files.create :key => filename, :body => File.open(tmpfile)
      ensure
        # Clean up tempfile
        FileUtils.rm tmpfile if tmpfile && File.exists?(tmpfile)
      end
    end
  end

  def self.restore<commit_msg>Renamed `perform` to `backup`<commit_after>require 'fog'
require 'active_support/all'
require 'dotenv'
require 'mysql2'
require 'tempfile'
require 'yaml'

Dotenv.load(".env", ".mysql.env")

class Cloudfile
  attr_reader :host, :db, :filename, :key
  def initialize f
    @cfile = f
    @key = f.key
    bits = @key.split "/"
    @host = bits[0]
    @db = bits[1]
    @filename = bits[2]
  end

  def download
    tmpfile = Dir::Tmpname.make_tmpname Dir.tmpdir+File::Separator, nil
    derp = File.open tmpfile, "w"
    derp.write @cfile.body

    tmpfile
  end
end

class Hoppler

  def self.hostname
    `hostname`.strip
  end    
  
  def self.backup      
    results = self.connection.query("show databases")
    databases = results.map{|row| row['Database']}
    
    databases.each do |database|
      begin
        # Dump to tempfile
        tmpfile = Dir::Tmpname.make_tmpname Dir.tmpdir+File::Separator, nil
        dump_cmd = "mysqldump #{database} -u #{ENV['MYSQL_USERNAME']} --single-transaction"
        dump_cmd << " --password=#{ENV['MYSQL_PASSWORD']}" if ENV['MYSQL_PASSWORD']
        system "#{dump_cmd} | bzip2 > #{tmpfile}"
        # Write to rackspace
        dir = self.rackspace.directories.get ENV['RACKSPACE_DB_CONTAINER']
        filename = "#{hostname}/#{database}/#{DateTime.now.strftime("%F")}.sql.bz2"
        dir.files.create :key => filename, :body => File.open(tmpfile)
      ensure
        # Clean up tempfile
        FileUtils.rm tmpfile if tmpfile && File.exists?(tmpfile)
      end
    end
  end

  def self.restore
{'fraction_non_alphanumeric': 0.13540654356980888, 'fraction_numerical': 0.003887269193391642, 'mean_word_length': 2.879396984924623, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '453722', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1064, 'n_words': 238}