Permalink
Browse files

add fog support;

  • Loading branch information...
1 parent a81da4b commit 494bbec35a87bd52097eb60de7e12cc57ca961c6 Alex Kravets committed Oct 2, 2012
Showing with 129 additions and 40 deletions.
  1. +1 −0 .gitignore
  2. +7 −2 heroku-mongo-backup.gemspec
  3. +28 −3 lib/heroku-mongo-backup.rb
  4. +92 −34 lib/s3_helpers.rb
  5. +1 −1 lib/tasks/heroku_mongo_backup.rake
View
@@ -1,3 +1,4 @@
pkg
*.swp
*.swo
+.DS_Store
@@ -1,15 +1,20 @@
Gem::Specification.new do |s|
s.name = 'heroku-mongo-backup'
- s.version = '0.4.0'
+ s.version = '0.4.1'
s.summary = 'Rake task backups mongo database on Heroku and push gzipped file to Amazon S3 or FTP.'
s.description = 'Rake task for backing up mongo database on heroku and push it to S3 or FTP. Library can be used as rake task or be easily integrated into daily cron job.'
s.authors = ['Alex Kravets', 'matyi', 'Stef Lewandowski', 'David Hall', 'Dan Porter']
s.email = 'santyor@gmail.com'
s.homepage = 'https://github.com/alexkravets/heroku-mongo-backup'
- s.files = Dir['lib/**/*']
+ s.require_paths = ["lib"]
+ s.files = `git ls-files`.split($\)
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
# Supress the warning about no rubyforge project
s.rubyforge_project = 'nowarning'
+
+ s.add_runtime_dependency 'mongo'
end
View
@@ -1,7 +1,6 @@
# encoding: UTF-8
require 'mongo'
-require 'bson'
require 'json'
require 'zlib'
require 'uri'
@@ -110,18 +109,30 @@ def ftp_download
def s3_connect
bucket = ENV['S3_BACKUPS_BUCKET']
if bucket.nil?
+ bucket = ENV['S3_BACKUP_BUCKET']
+ end
+ if bucket.nil?
+ bucket = ENV['S3_BACKUP']
+ end
+ if bucket.nil?
bucket = ENV['S3_BUCKET']
end
access_key_id = ENV['S3_KEY_ID']
if access_key_id.nil?
access_key_id = ENV['S3_KEY']
end
+ if access_key_id.nil?
+ access_key_id = ENV['AWS_ACCESS_KEY_ID']
+ end
secret_access_key = ENV['S3_SECRET_KEY']
if secret_access_key.nil?
secret_access_key = ENV['S3_SECRET']
end
+ if secret_access_key.nil?
+ secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
+ end
@bucket = HerokuMongoBackup::s3_connect(bucket, access_key_id, secret_access_key)
end
@@ -145,10 +156,17 @@ def initialize
#config_template = ERB.new(IO.read("config/mongoid.yml"))
#uri = YAML.load(config_template.result)['production']['uri']
uri = ENV['MONGO_URL']
+
+ if uri.nil?
+ uri = ENV['MONGOHQ_URL']
+ end
+ if uri.nil?
+ uri = ENV['MONGOLAB_URI']
+ end
else
mongoid_config = YAML.load_file("config/mongoid.yml")
config = {}
- defaults = mongoid_config['defaults']
+ defaults = mongoid_config['defaults']
dev_config = mongoid_config['development']
config.merge!(defaults) unless defaults.nil?
@@ -158,6 +176,14 @@ def initialize
port = config['port']
database = config['database']
uri = "mongodb://#{host}:#{port}/#{database}"
+
+ if uri == 'mongodb://:/' # new mongoid version 3.x
+ mongoid_config = YAML.load_file("config/mongoid.yml")
+ dev_config = mongoid_config['development']['sessions']['default']
+ host_port = dev_config['hosts'].first
+ database = dev_config['database']
+ uri = "mongodb://#{host_port}/#{database}"
+ end
end
@url = uri
@@ -200,4 +226,3 @@ def restore file_name
end
end
end
-
View
@@ -1,10 +1,11 @@
begin
require 's3'
+
rescue LoadError
- #
- # There is no 's3' gem in Gmefile
- #
- #puts "There is no 's3' gem in Gemfile."
+ #
+ # There is no 's3' gem in Gmefile
+ #
+ #puts "There is no 's3' gem in Gemfile."
end
if defined?(S3)
@@ -18,11 +19,13 @@ def HerokuMongoBackup::s3_connect(bucket, key, secret)
bucket = service.buckets.find(bucket)
return bucket
end
+
def HerokuMongoBackup::s3_upload(bucket, filename)
object = bucket.objects.build("backups/#{filename}")
object.content = open(filename)
object.save
end
+
def HerokuMongoBackup::s3_download(bucket, filename)
object = bucket.objects.find("backups/#{filename}")
content = object.content(reload=true)
@@ -35,36 +38,91 @@ def HerokuMongoBackup::s3_download(bucket, filename)
return content
end
- else
- begin
- require 'aws/s3'
- rescue LoadError
- #
- # There is no 'aws/s3' in Gemfile
- #
- #puts "There is no 'aws/s3' gem in Gemfile."
- end
+end
- if defined?(AWS)
- #
- # Using 'aws/s3' gem as Amazon S3 interface
- #
- #puts "Using \'aws/s3\' gem as Amazon S3 interface."
- def HerokuMongoBackup::s3_connect(bucket, key, secret)
- AWS::S3::Base.establish_connection!(:access_key_id => key,
- :secret_access_key => secret)
- return bucket
- end
- def HerokuMongoBackup::s3_upload(bucket, filename)
- AWS::S3::S3Object.store("backups/#{filename}", open(filename), bucket)
- end
- def HerokuMongoBackup::s3_download(bucket, filename)
- content = AWS::S3::S3Object.value("backups/#{filename}", bucket)
- return content
- end
- else
- logging = Logger.new(STDOUT)
- logging.error "heroku-mongo-backup: Please include 's3' or 'aws/s3' gem in applications Gemfile."
- end
+
+
+begin
+ require 'aws/s3'
+rescue LoadError
+ #
+ # There is no 'aws/s3' in Gemfile
+ #
+ #puts "There is no 'aws/s3' gem in Gemfile."
end
+if defined?(AWS)
+ #
+ # Using 'aws/s3' gem as Amazon S3 interface
+ #
+ #puts "Using \'aws/s3\' gem as Amazon S3 interface."
+ def HerokuMongoBackup::s3_connect(bucket, key, secret)
+ AWS::S3::Base.establish_connection!(:access_key_id => key,
+ :secret_access_key => secret)
+ # This is probably doesn't work
+ return bucket
+ end
+
+ def HerokuMongoBackup::s3_upload(bucket, filename)
+ AWS::S3::S3Object.store("backups/#{filename}", open(filename), bucket)
+ end
+
+ def HerokuMongoBackup::s3_download(bucket, filename)
+ content = AWS::S3::S3Object.value("backups/#{filename}", bucket)
+ return content
+ end
+end
+
+
+
+
+begin
+ require 'fog'
+rescue LoadError
+ #
+ # There is no 'fog' in Gemfile
+ #
+ #puts "There is no 'fog' gem in Gemfile."
+end
+
+if defined?(Fog)
+ #
+ # Using 'aws/s3' gem as Amazon S3 interface
+ #
+ #puts "Using \'aws/s3\' gem as Amazon S3 interface."
+ def HerokuMongoBackup::s3_connect(bucket, key, secret)
+ connection = Fog::Storage.new({
+ :provider => 'AWS',
+ :aws_access_key_id => key,
+ :aws_secret_access_key => secret
+ })
+ directory = connection.directories.new(:key => bucket)
+ return directory
+ end
+
+ def HerokuMongoBackup::s3_upload(directory, filename)
+ file = directory.files.create(
+ :key => "backups/#{filename}",
+ :body => open(filename),
+ :public => true
+ )
+ end
+
+ def HerokuMongoBackup::s3_download(directory, filename)
+ file = directory.files.get("backups/#{filename}")
+ return file.body
+ end
+
+
+
+
+
+
+else
+ logging = Logger.new(STDOUT)
+ logging.error "\n\nheroku-mongo-backup: Please include 's3', 'aws/s3' or 'fog' gem in applications Gemfile for uploading backup to S3 bucket. (ignore this if using FTP)\n\n"
+end
+
+
+
+
@@ -13,7 +13,7 @@ namespace :mongo do
if ENV['FILE']
HerokuMongoBackup::Backup.new.restore ENV['FILE']
else
- puts "Please provide backup file to restore from. Thanks!"
+ puts "Please provide backup file to restore from, format: rake mongo:restore FILE=<backup-file.gz>"
end
end
end

0 comments on commit 494bbec

Please sign in to comment.