-
-
Save StephanMeijer/1320aae26eb87961fd3c680928a21f0d to your computer and use it in GitHub Desktop.
Revisions
-
jacobvosmaer revised this gist
Feb 22, 2013 . 1 changed file with 7 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -94,9 +94,13 @@ # def database_config(db) database = YAML::load_file("config/#{fetch(:db_file, 'database.yml')}") extract_database_parameters(database["#{db}"]) end def extract_database_parameters(database_config) default_session = database_config['sessions']['default'] return default_session['username'], default_session['password'], default_session['database'], default_session['hosts'].first end # # Reads the database credentials from the remote config/database.yml file @@ -105,8 +109,8 @@ def database_config(db) # def remote_database_config(db) remote_config = capture(". ~/.profile; cat #{current_path}/config/#{fetch(:db_file, 'database.yml')} | ruby -e 'require \"erb\"; puts ERB.new(ARGF.read).result'") database = YAML::load(remote_config) extract_database_parameters(database["#{db}"]) end # -
jacobvosmaer revised this gist
Feb 22, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -62,7 +62,7 @@ system "rm -f tmp/#{filename} | rm -rf dump" logger.important "sync database from the '#{stage}' to local finished" end desc <<-DESC -
jacobvosmaer revised this gist
Feb 22, 2013 . 1 changed file with 1 addition and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -106,12 +106,7 @@ def database_config(db) def remote_database_config(db) remote_config = capture(". ~/.profile; cat #{current_path}/config/#{fetch(:db_file, 'database.yml')} | ruby -e 'require \"erb\"; puts ERB.new(ARGF.read).result'") database = YAML::load(remote_config)["#{db}"]['sessions']['default'] return database['username'], database['password'], database['database'], database['hosts'].first end # -
jacobvosmaer revised this gist
Feb 22, 2013 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -105,13 +105,13 @@ def database_config(db) # def remote_database_config(db) remote_config = capture(". ~/.profile; cat #{current_path}/config/#{fetch(:db_file, 'database.yml')} | ruby -e 'require \"erb\"; puts ERB.new(ARGF.read).result'") database = YAML::load(remote_config)["#{db}"]['sessions']['default'] host = database['host'] host << ":#{database['port']}" if database['port'] if !host && database['hosts'] host = database['hosts'].first.join(':') end return database['username'], database['password'], database['database'], host end # -
jacobvosmaer revised this gist
Aug 14, 2012 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,5 @@ require 'yaml' require 'pathname' # Based on http://gist.github.com/111597 http://gist.github.com/339471 # -
jacobvosmaer revised this gist
Aug 14, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ based on: [Capistrano recipe to sync rails MongoDB and files](https://gist.githu Add this line to your application's Gemfile: gem 'mongoid-sync', :git => 'git://gist.github.com/3342186.git' And then execute: -
jacobvosmaer revised this gist
Aug 14, 2012 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ require 'yaml' require 'pathname' require 'pry' # Based on http://gist.github.com/111597 http://gist.github.com/339471 # @@ -44,8 +45,10 @@ username, password, database, host = remote_database_config(stage) production_database = database dumpcmd = "mongodump -db #{database}" {:username => username, :password => password, :host => host}.each do |key, value| dumpcmd << " --#{key} #{value}" if value end run dumpcmd run "tar -cjf #{shared_path}/sync/#{filename} dump/#{database}" run "rm -rf dump" -
jacobvosmaer revised this gist
Aug 14, 2012 . 1 changed file with 10 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,9 @@ username, password, database, host = remote_database_config(stage) production_database = database dumpcmd = "mongodump -db #{database} -u #{username} -p #{password}" dumpcmd << " -h #{host}" if host run dumpcmd run "tar -cjf #{shared_path}/sync/#{filename} dump/#{database}" run "rm -rf dump" purge_old_backups "database" @@ -100,9 +102,14 @@ def database_config(db) # Returns username, password, database # def remote_database_config(db) remote_config = capture(". ~/.profile; cat #{current_path}/config/#{fetch(:db_file, 'database.yml')} | ruby -e 'require \"erb\"; puts ERB.new(ARGF.read).result'") database = YAML::load(remote_config) host = database["#{db}"]['host'] host << ":#{database["#{db}"]['port']}" if database["#{db}"]['port'] if !host && database["#{db}"]['hosts'] host = database["#{db}"]['hosts'].first.join(':') end return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], host end # -
matsimitsu revised this gist
Aug 10, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ A microgem to sync your staging/production MongoDB database (and uploads) to your local environment. based on: [Capistrano recipe to sync rails MongoDB and files](https://gist.github.com/1108011) ## Installation Add this line to your application's Gemfile: -
matsimitsu revised this gist
Aug 10, 2012 . 1 changed file with 0 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +0,0 @@ -
matsimitsu revised this gist
Aug 10, 2012 . 3 changed files with 56 additions and 46 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,17 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ # Mongoid::Sync A microgem to sync your staging/production MongoDB database (and uploads) to your local environment. based on: [Capistrano recepie to sync rails MongoDB and files](https://gist.github.com/1108011) ## Installation Add this line to your application's Gemfile: gem 'mongoid-sync', :git => 'git://gist.github.com/3313055.git' And then execute: $ bundle Make sure you have capistrano and capistrano-ext installed and add the following to your `deploy.rb` require 'mongoid_sync' ## Configuration The following block should be present in your `deploy.rb` file: ## Syncer set :sync_directories, ["public/uploads"] set :sync_backups, 1 set :db_file, "mongoid.yml" set :db_drop, '--drop' # drop database (rewrites everything) * `:sync_directories` is an array with the directories you want to sync (uploads etc.) * `sync_backups` is the number of backups you want to keep * `:db_file` is the mongoid config file * `:db_drop` is the mongodb drop command, the default `--drop` command shoud be fine. ## Usage First make sure the sync dir is created by running cap sync:setup You can then sync your staging or production environment by running cap sync:down This will sync both the MongoDB database and uploaded files. You can run both commands separately. cap sync:db cap sync:fs # Credits * **[@jkreeftmeijer](http://twitter.com/jkreeftmeijer)** for coming up with the killer MicroGem concept<br/> (http://jeffkreeftmeijer.com/2011/microgems-five-minute-rubygems/) * **[@kevinold](http://twitter.com/kevinold)** for the inital gist where this microgem is based on -
matsimitsu revised this gist
Aug 10, 2012 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,6 +11,4 @@ Gem::Specification.new do |gem| gem.files = ['mongoid_sync.rb'] gem.require_path = '.' end -
matsimitsu revised this gist
Aug 10, 2012 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,5 +13,4 @@ Gem::Specification.new do |gem| gem.require_path = '.' gem.add_dependency 'yaml' end -
matsimitsu revised this gist
Aug 10, 2012 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ # -*- encoding: utf-8 -*- Gem::Specification.new do |gem| gem.name = 'mongoid-sync' gem.version = '0.1.0' gem.platform = Gem::Platform::RUBY gem.authors = ["Robert Beekman"] gem.email = ["[email protected]"] gem.description = %q{A simple mongdb syncer for capistrano} -
matsimitsu revised this gist
Aug 10, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,13 @@ # -*- encoding: utf-8 -*- Gem::Specification.new do |gem| gem.name = 'mongoid-sync' gem.authors = ["Robert Beekman"] gem.email = ["[email protected]"] gem.description = %q{A simple mongdb syncer for capistrano} gem.summary = %q{Syncs mongodb and uploads from staging/production to local} gem.homepage = "http://80beans.com" gem.files = ['mongoid_sync.rb'] gem.require_path = '.' gem.add_dependency 'yaml' -
matsimitsu revised this gist
Aug 10, 2012 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,4 @@ # -*- encoding: utf-8 -*- Gem::Specification.new do |gem| gem.authors = ["Robert Beekman"] gem.email = ["[email protected]"] -
matsimitsu revised this gist
Aug 10, 2012 . No changes.There are no files selected for viewing
-
matsimitsu created this gist
Aug 10, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ *.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ source 'https://rubygems.org' # Specify your gem's dependencies in mongoid-sync.gemspec gemspec This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ Copyright (c) 2012 Robert Beekman MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ # Mongoid::Sync TODO: Write a gem description ## Installation Add this line to your application's Gemfile: gem 'mongoid-sync' And then execute: $ bundle Or install it yourself as: $ gem install mongoid-sync ## Usage TODO: Write usage instructions here ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Added some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ # -*- encoding: utf-8 -*- require File.expand_path('../lib/mongoid-sync/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["Robert Beekman"] gem.email = ["[email protected]"] gem.description = %q{A simple mongdb syncer for capistrano} gem.summary = %q{Syncs mongodb and uploads from staging/production to local} gem.homepage = "http://80beans.com" gem.files = ['mongoid_sync.rb'] gem.test_file = 'mongoid_sync.rb' gem.require_path = '.' gem.add_dependency 'yaml' gem.add_dependency 'pathname' end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,131 @@ require 'yaml' require 'pathname' # Based on http://gist.github.com/111597 http://gist.github.com/339471 # # Capistrano sync.rb task for syncing databases and directories between the # local development environment and production environment # # Changes were made to sync MongoDB databases # tested with mongoid # Modified by Julius Pabrinkis Capistrano::Configuration.instance.load do namespace :sync do after "deploy:setup", "sync:setup" desc <<-DESC Creates the sync dir in shared path. The sync directory is used to keep backups of database dumps and archives from synced directories. This task will be called on 'deploy:setup' DESC task :setup do run "cd #{shared_path}; mkdir sync" end namespace :down do desc <<-DESC Syncs the database and declared directories from the selected 'production' environment to the local development environment. This task simply calls both the 'sync:down:db' and 'sync:down:fs' tasks. DESC task :default do db and fs end desc <<-DESC Sync the production database to local DESC task :db, :roles => :db, :only => { :primary => true } do filename = "database.production.#{Time.now.strftime '%Y-%m-%d_%H-%M-%S'}.sql.bz2" on_rollback { delete "#{shared_path}/sync/#{filename}" } username, password, database, host = remote_database_config(stage) production_database = database run "mongodump -db #{database}" run "tar -cjf #{shared_path}/sync/#{filename} dump/#{database}" run "rm -rf dump" purge_old_backups "database" download "#{shared_path}/sync/#{filename}", "tmp/#{filename}" username, password, database = database_config('development') system "tar -xjvf tmp/#{filename}" system "mongorestore #{fetch(:db_drop, '')} -db #{database} dump/#{production_database}" system "rm -f tmp/#{filename} | rm -rf dump" logger.important "sync database from the 'production' to local finished" end desc <<-DESC Sync the production files to local DESC task :fs, :roles => :web, :once => true do server, port = host_and_port Array(fetch(:sync_directories, [])).each do |syncdir| unless File.directory? "#{syncdir}" logger.info "create local '#{syncdir}' folder" Dir.mkdir "#{syncdir}" end logger.info "sync #{syncdir} from #{server}:#{port} to local" destination, base = Pathname.new(syncdir).split system "rsync --verbose --archive --compress --copy-links --delete --stats --rsh='ssh -p #{port}' #{username}@#{server}:#{current_path}/#{syncdir} #{destination.to_s}" end logger.important "sync filesystem from the '#{stage}' to local finished" end end # # Reads the database credentials from the local config/database.yml file # +db+ the name of the environment to get the credentials for # Returns username, password, database # def database_config(db) database = YAML::load_file("config/#{fetch(:db_file, 'database.yml')}") return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], database["#{db}"]['host'] end # # Reads the database credentials from the remote config/database.yml file # +db+ the name of the environment to get the credentials for # Returns username, password, database # def remote_database_config(db) remote_config = capture("cat #{current_path}/config/#{fetch(:db_file, 'database.yml')}") database = YAML::load(remote_config) return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], database["#{db}"]['host'] end # # Returns the actual host name to sync and port # def host_and_port return roles[:web].servers.first.host, ssh_options[:port] || roles[:web].servers.first.port || 22 end # # Purge old backups within the shared sync directory # def purge_old_backups(base) count = fetch(:sync_backups, 5).to_i backup_files = capture("ls -xt #{shared_path}/sync/#{base}*").split.reverse if count >= backup_files.length logger.important "no old backups to clean up" else logger.info "keeping #{count} of #{backup_files.length} sync backups" delete_backups = (backup_files - backup_files.last(count)).join(" ") try_sudo "rm -rf #{delete_backups}" end end end end -
matsimitsu created this gist
Aug 10, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Readme