Skip to content

Instantly share code, notes, and snippets.

@tobym
Created June 6, 2010 05:13
Show Gist options
  • Save tobym/427318 to your computer and use it in GitHub Desktop.
Save tobym/427318 to your computer and use it in GitHub Desktop.
Secure and easy database transfers using Taps
# From Scott Wheeler: http://blog.directededge.com/2010/06/05/using-taps-without-running-a-taps-server/
# Tunnels the connections over SSH, initiates a pull from the production side, then kills the Taps server.
# Saved for posterity
#!/usr/bin/env ruby
require 'rubygems'
require 'active_support/secure_random'
require 'net/ssh'
SSH_USER = '[sshuser]'
SSH_HOST = '[dbhost]'
LOCAL_DB = 'mysql://[dbuser]:[dbpass]@localhost/[dbname]'
REMOTE_DB = 'mysql://[dbuser]:[dbpass]@localhost/[dbname]'
TAPS_USER = ActiveSupport::SecureRandom.hex(16)
TAPS_PASS = ActiveSupport::SecureRandom.hex(16)
URL = "http://#{TAPS_USER}:#{TAPS_PASS}@localhost:5000"
Net::SSH.start(SSH_HOST, SSH_USER, :compression => true) do |ssh|
ssh.forward.local(5000, 'localhost', 5000)
ready = false
channel = ssh.open_channel do |c|
c.request_pty
c.on_data { |c, data| ready = true if data =~ /port=5000/ }
c.exec("taps server #{REMOTE_DB} #{TAPS_USER} #{TAPS_PASS}")
end
finished = false
Thread.new do
sleep 0.1 until ready
system "taps pull #{LOCAL_DB} #{URL}"
finished = true
end
ssh.loop(0.1) do
channel.send_data(Net::SSH::Connection::Term::VINTR) if finished
!finished
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment