<commit_before>require 'tempfile'

module Backup
  class Storage
    extend Backup::Attribute
    attr_accessor :name, :config

    def initialize(name, config)
      self.name   = name
      self.config = config
    end

    def backup(server)
      @server        = server
      @server_config = @server.config
      @backup_path   = "#{config.path}/#{@server.name}"

      @ssh_host      = "'#{@server_config.host}'"
      @scp_host      = @server_config.port ? "-P#{@server_config.port} #{@ssh_host}" : @ssh_host
      @rsync_host    = @server_config.port ? " -e 'ssh -p#{@server_config.port}' #{@ssh_host}" : @ssh_host
      @ssh_host      = "'-p#{@server_config.port}' #{@ssh_host}" if @server_config.port


      backup_rsync
      backup_mysql
      commit_changes
    end

    def backup_rsync
      target_path = File.join(@backup_path, "rsync")
      FileUtils.mkdir_p target_path

      @server_config.rsync.to_a.map do |path|
        remote_path = path.is_a?(Hash) ? path.first[0] : path
        target_name = File.basename(path.is_a?(Hash) ? path.first[1] : path)
        Backup::Main.run "rsync -rav #{@rsync_host}:#{remote_path.sub(/\/?$/,'/')} '#{File.join(target_path, target_name)}'"
      end
    end

    def backup_mysql
      target_path = File.join(@backup_path, "mysql")<commit_msg>add rsync parameter<commit_after>require 'tempfile'

module Backup
  class Storage
    extend Backup::Attribute
    attr_accessor :name, :config

    def initialize(name, config)
      self.name   = name
      self.config = config
    end

    def backup(server)
      @server        = server
      @server_config = @server.config
      @backup_path   = "#{config.path}/#{@server.name}"

      @ssh_host      = "'#{@server_config.host}'"
      @scp_host      = @server_config.port ? "-P#{@server_config.port} #{@ssh_host}" : @ssh_host
      @rsync_host    = @server_config.port ? " -e 'ssh -p#{@server_config.port}' #{@ssh_host}" : @ssh_host
      @ssh_host      = "'-p#{@server_config.port}' #{@ssh_host}" if @server_config.port


      backup_rsync
      backup_mysql
      commit_changes
    end

    def backup_rsync
      target_path = File.join(@backup_path, "rsync")
      FileUtils.mkdir_p target_path

      @server_config.rsync.to_a.map do |path|
        remote_path = path.is_a?(Hash) ? path.first[0] : path
        target_name = File.basename(path.is_a?(Hash) ? path.first[1] : path)
        Backup::Main.run "rsync -ravk #{@rsync_host}:#{remote_path.sub(/\/?$/,'/')} '#{File.join(target_path, target_name)}'"
      end
    end

    def backup_mysql
      target_path = File.join(@backup_path, "mysql")
{'fraction_non_alphanumeric': 0.1457458985120183, 'fraction_numerical': 0.0015261350629530714, 'mean_word_length': 2.8277372262773723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '314727', 'n_tokens_mistral': 933, 'n_tokens_neox': 908, 'n_words': 163}