Skip to content

Instantly share code, notes, and snippets.

@mattfawcett
Created March 1, 2009 14:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattfawcett/72340 to your computer and use it in GitHub Desktop.
Save mattfawcett/72340 to your computer and use it in GitHub Desktop.
Sync your development database and file upload folders with whatever your staging/production server currently has
# Task has the following dependencies
# net/ssh gem [gem install net-ssh]
# net/sftp gem [gem install net-sftp]
# rsync command line utility
# yamb_db rails plugin [ruby script/plugin install git://github.com/adamwiggins/yaml_db.git]
desc "This task will make your development environment download and import a fresh copy of your staging database and also sync any folders such as image uploads so you can quickly replicate your staging/production server content locally"
task :sync_with_staging => :environment do
@remote_host = 'yourremoteserver.com'
@remote_user = 'yoursshuser'
@remote_password = "yoursshpassword"
@remote_environment = "staging"
@remote_path_to_rails = "/home/rails/myproject/current"
@directories_to_sync = {"/home/rails/myproject/shared/system" => "#{RAILS_ROOT}/public"}
require 'net/ssh'
require 'net/sftp'
#Delete our old local data.yml file
File.delete("#{RAILS_ROOT}/db/data.yml") if File::exists?("#{RAILS_ROOT}/db/data.yml")
#Create a new data.yml file on the staging server
Net::SSH.start(@remote_host, @remote_user, :password => @remote_password) do |ssh|
output = ssh.exec!("hostname")
ssh.exec "rake -f #{@remote_path_to_rails}/Rakefile RAILS_ENV=#{@remote_environment} db:data:dump"
#Now download the data.yml file over the ssh connection
ssh.sftp.connect do |sftp|
sftp.download!("#{@remote_path_to_rails}/db/data.yml", "#{RAILS_ROOT}/db/data.yml")
end
end
#Import the new data.yml file
Rake::Task["db:data:load"].invoke
#Sync the uploads
@directories_to_sync.each do |remote_path, local_path|
sh("rsync -vra #{@remote_user}@#{@remote_host}:#{remote_path} #{local_path}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment