Class: Dbsync::Sync
- Inherits:
-
Object
- Object
- Dbsync::Sync
- Defined in:
- lib/dbsync/sync.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#clone_dump ⇒ Object
Copy the remote dump file to a local destination.
-
#fetch ⇒ Object
Update the local dump file from the remote source (using rsync).
-
#initialize(ssh_config, db_config, options = {}) ⇒ Sync
constructor
A new instance of Sync.
-
#merge ⇒ Object
Update the local database with the local dump file.
-
#pull ⇒ Object
Update the local dump file, and update the local database.
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, ={}) ssh_config = symbolize_keys(ssh_config) db_config = symbolize_keys(db_config) @verbose = !![: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.(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.(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(="") $stdout.puts "[#{Time.now.strftime('%T')}] [dbsync] #{}" end |
Instance Method Details
#clone_dump ⇒ Object
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 |
#fetch ⇒ Object
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 |
#merge ⇒ Object
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}'..." = "" += "-u :username " if @db_username += "-p:password " if @db_password += "-h :host " if @db_host line = Cocaine::CommandLine.new('mysql', "#{} :database < :local") line.run({ :username => @db_username, :password => @db_password, :host => @db_host, :database => @db_database, :local => @local }) end |
#pull ⇒ Object
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 |