Skip to content

Instantly share code, notes, and snippets.

@StephanMeijer
Forked from jacobvosmaer/LICENSE
Created August 13, 2019 15:01
Show Gist options
  • Select an option

  • Save StephanMeijer/1320aae26eb87961fd3c680928a21f0d to your computer and use it in GitHub Desktop.

Select an option

Save StephanMeijer/1320aae26eb87961fd3c680928a21f0d to your computer and use it in GitHub Desktop.

Revisions

  1. @jacobvosmaer jacobvosmaer revised this gist Feb 22, 2013. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions mongoid_sync.rb
    Original 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')}")
    return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], database["#{db}"]['host']
    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)["#{db}"]['sessions']['default']
    return database['username'], database['password'], database['database'], database['hosts'].first
    database = YAML::load(remote_config)
    extract_database_parameters(database["#{db}"])
    end

    #
  2. @jacobvosmaer jacobvosmaer revised this gist Feb 22, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongoid_sync.rb
    Original 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 'production' to local finished"
    logger.important "sync database from the '#{stage}' to local finished"
    end

    desc <<-DESC
  3. @jacobvosmaer jacobvosmaer revised this gist Feb 22, 2013. 1 changed file with 1 addition and 6 deletions.
    7 changes: 1 addition & 6 deletions mongoid_sync.rb
    Original 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']
    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
    return database['username'], database['password'], database['database'], database['hosts'].first
    end

    #
  4. @jacobvosmaer jacobvosmaer revised this gist Feb 22, 2013. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions mongoid_sync.rb
    Original 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)
    host = database["#{db}"]['host']
    host << ":#{database["#{db}"]['port']}" if database["#{db}"]['port']
    if !host && database["#{db}"]['hosts']
    host = database["#{db}"]['hosts'].first.join(':')
    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["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], host
    return database['username'], database['password'], database['database'], host
    end

    #
  5. @jacobvosmaer jacobvosmaer revised this gist Aug 14, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion mongoid_sync.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    require 'yaml'
    require 'pathname'
    require 'pry'

    # Based on http://gist.github.com/111597 http://gist.github.com/339471
    #
  6. @jacobvosmaer jacobvosmaer revised this gist Aug 14, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _README.md
    Original 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/3313055.git'
    gem 'mongoid-sync', :git => 'git://gist.github.com/3342186.git'

    And then execute:

  7. @jacobvosmaer jacobvosmaer revised this gist Aug 14, 2012. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions mongoid_sync.rb
    Original 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} -u #{username} -p #{password}"
    dumpcmd << " -h #{host}" if host
    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"
  8. @jacobvosmaer jacobvosmaer revised this gist Aug 14, 2012. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions mongoid_sync.rb
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,9 @@
    username, password, database, host = remote_database_config(stage)
    production_database = database

    run "mongodump -db #{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("cat #{current_path}/config/#{fetch(:db_file, 'database.yml')}")
    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)
    return database["#{db}"]['username'], database["#{db}"]['password'], database["#{db}"]['database'], database["#{db}"]['host']
    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

    #
  9. @matsimitsu matsimitsu revised this gist Aug 10, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _README.md
    Original 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 recepie to sync rails MongoDB and files](https://gist.github.com/1108011)
    based on: [Capistrano recipe to sync rails MongoDB and files](https://gist.github.com/1108011)
    ## Installation

    Add this line to your application's Gemfile:
  10. @matsimitsu matsimitsu revised this gist Aug 10, 2012. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -1,4 +0,0 @@
    source 'https://rubygems.org'

    # Specify your gem's dependencies in mongoid-sync.gemspec
    gemspec
  11. @matsimitsu matsimitsu revised this gist Aug 10, 2012. 3 changed files with 56 additions and 46 deletions.
    17 changes: 0 additions & 17 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -1,17 +0,0 @@
    *.gem
    *.rbc
    .bundle
    .config
    .yardoc
    Gemfile.lock
    InstalledFiles
    _yardoc
    coverage
    doc/
    lib/bundler/man
    pkg
    rdoc
    spec/reports
    test/tmp
    test/version_tmp
    tmp
    29 changes: 0 additions & 29 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,29 +0,0 @@
    # 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
    56 changes: 56 additions & 0 deletions _README.md
    Original 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

  12. @matsimitsu matsimitsu revised this gist Aug 10, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions mongoid-sync.gemspec
    Original 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 = '.'

    gem.add_dependency 'yaml'
    end
  13. @matsimitsu matsimitsu revised this gist Aug 10, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion mongoid-sync.gemspec
    Original 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'
    gem.add_dependency 'pathname'
    end
  14. @matsimitsu matsimitsu revised this gist Aug 10, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions mongoid-sync.gemspec
    Original 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}
  15. @matsimitsu matsimitsu revised this gist Aug 10, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongoid-sync.gemspec
    Original 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.test_file = 'mongoid_sync.rb'
    gem.require_path = '.'

    gem.add_dependency 'yaml'
  16. @matsimitsu matsimitsu revised this gist Aug 10, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions mongoid-sync.gemspec
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    # -*- 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]"]
  17. @matsimitsu matsimitsu revised this gist Aug 10, 2012. No changes.
  18. @matsimitsu matsimitsu created this gist Aug 10, 2012.
    17 changes: 17 additions & 0 deletions .gitignore
    Original 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
    4 changes: 4 additions & 0 deletions Gemfile
    Original 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
    22 changes: 22 additions & 0 deletions LICENSE
    Original 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.
    29 changes: 29 additions & 0 deletions README.md
    Original 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
    17 changes: 17 additions & 0 deletions mongoid-sync.gemspec
    Original 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
    131 changes: 131 additions & 0 deletions mongoid_sync.rb
    Original 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
  19. @matsimitsu matsimitsu created this gist Aug 10, 2012.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Readme