Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Created February 21, 2014 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cupakromer/9144881 to your computer and use it in GitHub Desktop.
Save cupakromer/9144881 to your computer and use it in GitHub Desktop.
😧 Mongo BSON dump restore script
#!/usr/bin/env ruby
#
# Little ditty to untar a bunch of mongo bson dumps,
# and then restore them.
#
# Remembers if something was processed before and doesn't
# process it again. (Though mongo will basically due this
# for you 😬)
#
# I'm a bad person and I should feel bad for writing this
# abomination of a script. 😞
#
require "fileutils"
require "pathname"
session_dir = Pathname.new(ARGV[0])
data_dir = session_dir.join("data")
completed_dir = session_dir.join("completed")
[data_dir, completed_dir].select{ |dir| !(dir.exist? && dir.directory?) }
.each do |dir|
FileUtils.mkdir dir
end
Pathname.glob(session_dir.join("*.tar.gz"))
.each do |file|
completed_file = completed_dir.join(file.basename)
if completed_file.exist?
if completed_file.size == file.size
FileUtils.rm_f file
else
warn "WARNING: #{file} already imported but was a different size!"
warn "WARNING: Manual intervention required!"
end
else
puts `tar xzvf #{file.expand_path} -C #{data_dir}`
if $? == 0
puts `mongorestore -d godzilla_ces #{data_dir.join(file.basename(".sessions.tar.gz"), "godzilla_production")}`
if $? == 0
FileUtils.mv file, completed_dir, force: true
else
warn "There was a problem restoring #{data_dir.join(file.basename(".sessions.tar.gz"))}"
end
else
warn "There was a problem untaring #{file.expand_path}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment