Class: Backup::Tasks::MySQL

Inherits:
Object
  • Object
show all
Defined in:
lib/backup-agent/tasks/mysql.rb

Defined Under Namespace

Classes: Credentials

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, credentials:, databases: :all, **options) ⇒ MySQL

Returns a new instance of MySQL.



8
9
10
11
12
13
# File 'lib/backup-agent/tasks/mysql.rb', line 8

def initialize(host:, credentials:, databases: :all, **options)
  @host        = host
  @credentials = credentials
  @databases   = databases
  @options     = options
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



6
7
8
# File 'lib/backup-agent/tasks/mysql.rb', line 6

def credentials
  @credentials
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/backup-agent/tasks/mysql.rb', line 6

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/backup-agent/tasks/mysql.rb', line 6

def options
  @options
end

Instance Method Details

#databasesObject



30
31
32
33
34
35
36
37
38
# File 'lib/backup-agent/tasks/mysql.rb', line 30

def databases
  if @databases == :all
    command("mysql", *credentials.to_options, *host_options, "-e", "SHOW DATABASES;")
        .split("\n")
        .reject { |el| el =~ /database|information_schema|mysql|performance_schema|test|phpmyadmin|pma|sys/i }
  else
    [@databases].flatten.compact
  end
end

#dump_optionsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/backup-agent/tasks/mysql.rb', line 40

def dump_options
  @options.fetch(:dump_options) do
    %W( --add-drop-table
        --add-locks
        --allow-keywords
        --comments
        --complete-insert
        --create-options
        --disable-keys
        --extended-insert
        --lock-tables
        --quick
        --quote-names
        --routines
        --set-charset
        --dump-date
        --tz-utc
        --verbose )
  end
end

#host_optionsObject



61
62
63
# File 'lib/backup-agent/tasks/mysql.rb', line 61

def host_options
  ["--host=#{@host}"]
end

#perform(storage) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/backup-agent/tasks/mysql.rb', line 15

def perform(storage)
  databases.map do |db|
    Tempfile.open do |tempfile|
      command("mysqldump", *credentials.to_options, *host_options, *dump_options, "--databases", db).tap do |dump_sql|
        stdin dump_sql do
          command("xz", "--compress", "-9", "--format=xz", "--keep", "--threads=0", "--verbose", "--check=sha256").tap do |dump_xz|
            tempfile.write(dump_xz)
            storage.store(construct_filename(db, ".sql.xz"), tempfile.path)
          end
        end
      end
    end
  end
end