Class: Dbsync::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/dbsync/sync.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssh_config, db_config, options = {}) ⇒ Sync

Returns a new instance of Sync.



13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/dbsync/sync.rb', line 13

def initialize(ssh_config, db_config, options={})
  ssh_config  = symbolize_keys(ssh_config)
  db_config   = symbolize_keys(db_config)

  @verbose = !!options[:verbose]

  @db_username  = db_config[:username]
  @db_password  = db_config[:password]
  @db_host      = db_config[:host]
  @db_database  = db_config[:database]

  @remote   = ssh_config[:remote]
  @local    = File.expand_path(ssh_config[:local]) if ssh_config[:local]

  if !@remote
    $stdout.puts "DEPRECATED: The remote_host, remote_dir, and filename " \
      "options will be removed. " \
      "Instead, combine remote_host, remote_dir, and filename into a " \
      "single 'remote' configuration. Example: " \
      "'{ remote: \"[email protected]:~/dbuser/yourdb.dump\" }'"

    remote_host = ssh_config[:remote_host]
    remote_dir  = ssh_config[:remote_dir]
    filename    = ssh_config[:filename]
    @remote = "#{remote_host}:#{File.join(remote_dir, filename)}"
  end

  if !@local
    $stdout.puts "DEPRECATED: The local_dir and filename " \
      "options will be removed. " \
      "Instead, combine local_dir and filename into a " \
      "single 'local' configuration. Example: " \
      "'{ local: \"../dbsync/yourdb.dump\" }'"

    local_dir = ssh_config[:local_dir]
    filename  = ssh_config[:filename]
    @local  = File.expand_path(File.join(local_dir, filename))
  end
end

Class Method Details

.notify(message = "") ⇒ Object



7
8
9
# File 'lib/dbsync/sync.rb', line 7

def notify(message="")
  $stdout.puts "[#{Time.now.strftime('%T')}] [dbsync] #{message}"
end

Instance Method Details

#clone_dumpObject

Copy the remote dump file to a local destination. Instead of requiring two different tools (rsync and scp) for this library, instead we’ll just remove the local file if it exists then run rsync, which is essentially a full copy.



99
100
101
102
103
# File 'lib/dbsync/sync.rb', line 99

def clone_dump
  notify "Copying '#{@remote}' into '#{@local}' via rsync..."
  FileUtils.rm_f(@local)
  fetch
end

#fetchObject

Update the local dump file from the remote source (using rsync).



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dbsync/sync.rb', line 55

def fetch
  notify "Updating '#{@local}' from '#{@remote}' via rsync..."

  FileUtils.mkdir_p(File.dirname(@local))

  line = Cocaine::CommandLine.new('rsync', '-v :remote :local')
  line.run({
    :remote => @remote,
    :local  => @local
  })
end

#mergeObject

Update the local database with the local dump file.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dbsync/sync.rb', line 69

def merge
  notify "Dumping data from '#{@local}' into '#{@db_database}'..."

  options = ""
  options += "-u :username " if @db_username
  options += "-p:password "  if @db_password
  options += "-h :host "     if @db_host

  line = Cocaine::CommandLine.new('mysql', "#{options} :database < :local")
  line.run({
    :username   => @db_username,
    :password   => @db_password,
    :host       => @db_host,
    :database   => @db_database,
    :local      => @local
  })
end

#pullObject

Update the local dump file, and update the local database.



89
90
91
92
# File 'lib/dbsync/sync.rb', line 89

def pull
  fetch
  merge
end