Class: MysqlBackup::Install

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_backup/install.rb

Class Method Summary collapse

Class Method Details

.ask_for_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mysql_backup/install.rb', line 8

def ask_for_options
  require "highline/import"

  puts <<-PROMPT

  Please enter your MySQL username and password.

  PROMPT

  user     = ask("MySQL username   : ")
  password = ask("MySQL password   : ") { |q| q.echo = false }
  host     = ask("MySQL host       : ") { |q| q.default = 'localhost' }
  dir      = ask("Backup directory : ") { |q| q.default = '/tmp' }

  [user, password, host, dir]
end

.installObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mysql_backup/install.rb', line 25

def install
  begin
    if File.exist?(CONF_FILE)
      puts <<-WARN

      #{CONF_FILE} already exists. Remove it and try again.
      
      WARN

      exit
    end

    user, password, host, dir = ask_for_options

    File.open(CONF_FILE, 'w') do |file|
      template = File.read(File.join(File.dirname(__FILE__), 'mysql_backup.yml.example'))
      template.gsub!('USER', user)
      template.gsub!('PASS', password)
      template.gsub!('HOST', host)
      template.gsub!('DIR', dir)
      
      file << template
    end

    FileUtils.chmod(750, CONF_FILE)

    puts <<-ERR


      Configuration file #{CONF_FILE} written.

      You can now backup all databases by running the following command:

        $ mysql_backup

    ERR

  rescue Errno::EACCES => e
    puts <<-ERR

      You need to run this command with sudo or as root.

    ERR
    exit
  end
end