<commit_before>namespace :db do
  desc <<-DESC
    Loads one of the backup made with the db:export:remote task
  DESC
  task :import_to_local do
    confirmation = Capistrano::CLI.ui.ask "You are about to replace your local database by a remote backup (selected stage = #{stage}). Are you sure ? y/n (n)"
    if confirmation == "n" or confirmation == ""
      abort "Aborted"
    end
    backup_dir = File.join( get_backup_dir, "#{stage}" )
    backup_list = `ls #{backup_dir}`
    backup_files = backup_list.split( /\n/ );
    files = Hash.new
    i = 1
    backup_files.each { |value|
      puts "#{i}: #{value}"
      files[i] = value
      i += 1
    }
    file_to_import = Capistrano::CLI.ui.ask "Which one ?"
    if files.has_key?( file_to_import.to_i )
      filename = File.join( backup_dir, files[file_to_import.to_i] )
      system( "gunzip < #{filename} | mysql -u#{database_uname} -p#{database_passd} #{database_name} ")
    else
      abort "Bad index"
    end
  end

  # Should use :db as :roles
  desc <<-DESC
    Creates a backup from a remote database server
  DESC
  task :backup, :roles => :web, :only => { :primary => true } do
    filename = generate_backup_name
    file = File.join( "/tmp", filename )
    backup_dir_for_this_stage = File.join( get_backup_dir, "#{stage}" )
    on_rollback do
      run "rm #{file}"
    end
    dump_result = nil
    run "mysqldump -h#{database_server} -u#{database_uname} -p #{database_name} | gzip > #{file}" do |ch, stream, out|
      ch.send_data "#{database_passd}\n" if out =~ /^Enter password:/
      dump_result = out
    end
    if dump_result =~ /.*error.*1045/i
      puts "Access denied on '#{database_server}' with user '#{database_uname}'".red
    else
      create_backup_dir( backup_dir_for_this_stage )
      get( file, File.join( backup_dir_for_this_stage, filename ), :via => :scp )
    end
    run "rm #{file}"
  end

  desc <<-DESC
    Create a backup from your local instance
  DESC
  task :backup_local do
    backup_dir = File.join( get_backup_dir, 'local' )
    create_backup_dir( backup_dir )
    filename = File.join( backup_dir, generate_backup_name )
    system("mysqldump -u#{database_uname} -p#{database_passd} #{database_name} | gzip -9 > #{filename}")
  end

  def generate_backup_name
    return "#{Time.now.strftime '%Y-%m%d-%H%M%S'}.sql.gz"
  end

  def create_backup_dir( path )
    FileUtils.mkdir_p( path )
  end

  def get_backup_dir
    return "#{ezp_legacy_path('extension/alcapon/backups/database')}"
  end
end<commit_msg>Add database name to the generated dump<commit_after>namespace :db do
  desc <<-DESC
    Loads one of the backup made with the db:export:remote task
  DESC
  task :import_to_local do
    confirmation = Capistrano::CLI.ui.ask "You are about to replace your local database by a remote backup (selected stage = #{stage}). Are you sure ? y/n (n)"
    if confirmation == "n" or confirmation == ""
      abort "Aborted"
    end
    backup_dir = File.join( get_backup_dir, "#{stage}" )
    backup_list = `ls #{backup_dir}`
    backup_files = backup_list.split( /\n/ );
    files = Hash.new
    i = 1
    backup_files.each { |value|
      puts "#{i}: #{value}"
      files[i] = value
      i += 1
    }
    file_to_import = Capistrano::CLI.ui.ask "Which one ?"
    if files.has_key?( file_to_import.to_i )
      filename = File.join( backup_dir, files[file_to_import.to_i] )
      system( "gunzip < #{filename} | mysql -u#{database_uname} -p#{database_passd} #{database_name} ")
    else
      abort "Bad index"
    end
  end

  # Should use :db as :roles
  desc <<-DESC
    Creates a backup from a remote database server
  DESC
  task :backup, :roles => :web, :only => { :primary => true } do
    filename = generate_backup_name( database_name )
    file = File.join( "/tmp", filename )
    backup_dir_for_this_stage = File.join( get_backup_dir, "#{stage}" )
    on_rollback do
      run "rm #{file}"
    end
    dump_result = nil
    run "mysqldump -h#{database_server} -u#{database_uname} -p #{database_name} | gzip > #{file}" do |ch, stream, out|
      ch.send_data "#{database_passd}\n" if out =~ /^Enter password:/
      dump_result = out
    end
    if dump_result =~ /.*error.*1045/i
      puts "Access denied on '#{database_server}' with user '#{database_uname}'".red
    else
      create_backup_dir( backup_dir_for_this_stage )
      get( file, File.join( backup_dir_for_this_stage, filename ), :via => :scp )
    end
    run "rm #{file}"
  end

  desc <<-DESC
    Create a backup from your local instance
  DESC
  task :backup_local do
    backup_dir = File.join( get_backup_dir, 'local' )
    create_backup_dir( backup_dir )
    filename = File.join( backup_dir, generate_backup_name( database_name ) )
    system("mysqldump -u#{database_uname} -p#{database_passd} #{database_name} | gzip -9 > #{filename}")
  end

  def generate_backup_name(dbname)
    return "#{Time.now.strftime '%Y-%m%d-%H%M%S'}-#{dbname}.sql.gz"
  end

  def create_backup_dir( path )
    FileUtils.mkdir_p( path )
  end

  def get_backup_dir
    return "#{ezp_legacy_path('extension/alcapon/backups/database')}"
  end
end
{'fraction_non_alphanumeric': 0.12890625, 'fraction_numerical': 0.002734375, 'mean_word_length': 3.1838235294117645, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6901354', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1779, 'n_words': 475}