Class: CKnife::CKnifeMysql

Inherits:
Thor
  • Object
show all
Defined in:
lib/cknife/cknife_mysql.rb

Instance Method Summary collapse

Instance Method Details

#captureObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cknife/cknife_mysql.rb', line 70

def capture
  file_name = "db" + Time.now.strftime("%Y%m%d%H%M%S") + ".sql"

  if File.exists?(file_name)
    say("File already exists: #{file_name}.", :red)
  end

  command_line.with_option_file do |c|
    c.execute "mysqldump #{connection_options} #{conf[:database]} --add-drop-database --result-file=#{file_name}" do
      say("Captured #{file_name}.")
    end
  end
end

#consoleObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cknife/cknife_mysql.rb', line 45

def console
  if !File.exists?(option_file)
    if !options[:myfile]
      say("You must prepare a #{option_file} file for this command, or use --myfile to have this tool create it for you. Alternatively, you can create a #{option_file} file with the myfile command and delete it later with the dmyfile command.")
      return
    end

    command_line.write_option_file
  end

  dc(mysql_easy) if options[:verbose]
  exec(mysql_easy)
end

#dmyfileObject



65
66
67
# File 'lib/cknife/cknife_mysql.rb', line 65

def dmyfile
  command_line.delete_opt_file
end

#myfileObject



60
61
62
# File 'lib/cknife/cknife_mysql.rb', line 60

def myfile
  command_line.create_opt_file("Connect command: #{mysql_easy}")
end

#restoreObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cknife/cknife_mysql.rb', line 86

def restore
  to_restore = options[:filename] if options[:filename]
  if to_restore.nil?
    files = Dir["db*.sql"]
    with_mtime = files.map { |f| [f, File.mtime(f)] }
    with_mtime.sort! { |a,b| a.last <=> b.last }
    files = with_mtime.map(&:first)
    to_restore = files.last
  end

  if to_restore.nil?
    say("No backups file to restore. None given on the command line and none could be found in the CWD.", :red)
    return
  else
    if !yes?("Restore #{to_restore}?", :green)
      return
    end
  end

  command_line.with_option_file do |c|
    say("Doing restore...")

    c.execute("mysql #{connection_options} #{conf[:database]}", "source #{to_restore};") do
      say("Restored #{to_restore}")
    end
  end
end