cloudant_backup 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +19 -0
- data/LICENSE.txt +22 -0
- data/README.md +87 -0
- data/Rakefile +1 -0
- data/cloudant_backup.gemspec +28 -0
- data/console +26 -0
- data/lib/cloudant_backup/backup.rb +71 -0
- data/lib/cloudant_backup/version.rb +3 -0
- data/lib/cloudant_backup.rb +6 -0
- data/spec/lib/cloudant_backup_spec.rb +89 -0
- data/spec/spec_helper.rb +4 -0
- metadata +157 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :cli => "--color", :version => 2 do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { "spec" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
watch(%r{^spec/dummy/app/models/(.+)\.rb$}) { "spec" }
|
9
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
10
|
+
watch('spec/dummy/config/routes.rb') { "spec/routing" }
|
11
|
+
watch('spec/dummy/app/controllers/application_controller.rb') { "spec/controllers" }
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
guard 'bundler' do
|
16
|
+
watch('Gemfile')
|
17
|
+
# Uncomment next line if Gemfile contain `gemspec' command
|
18
|
+
# watch(/^.+\.gemspec/)
|
19
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jannis Hermanns
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# CloudantBackup
|
2
|
+
|
3
|
+
Easily backup a cloudant db using replication. This gem wraps
|
4
|
+
the two http requests you need to make to backup a cloudant db
|
5
|
+
(1. create a new db, 2. backup/replicate your main db to it)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'cloudant_backup'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install cloudant_backup
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Most simple case: replicate your production db to one automatically
|
24
|
+
named backup db on cloudant:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
cb = CloudantBackup.new
|
28
|
+
cb.user = 'username'
|
29
|
+
cb.password = 'password'
|
30
|
+
cb.source = 'my_production_db'
|
31
|
+
|
32
|
+
cb.replicate # Backup my_production_db to my_production_db_backup_2013_09_10
|
33
|
+
```
|
34
|
+
|
35
|
+
Create only one db per month (you can still update it daily):
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
cb = CloudantBackup.new
|
39
|
+
[...]
|
40
|
+
cb.date_pattern = '%Y_%m' # Any strftime string works
|
41
|
+
|
42
|
+
cb.replicate
|
43
|
+
```
|
44
|
+
|
45
|
+
Choose the target db name yourself:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
cb = CloudantBackup.new
|
49
|
+
[...]
|
50
|
+
cb.target = 'custom_target_db_name'
|
51
|
+
|
52
|
+
cb.replicate
|
53
|
+
```
|
54
|
+
|
55
|
+
Use another user instead of your cloudant main user:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
cb = CloudantBackup.new
|
59
|
+
[...]
|
60
|
+
cb.user = 'generated_api_key'
|
61
|
+
cb.cloudant_host = 'mainuser'
|
62
|
+
|
63
|
+
cb.replicate
|
64
|
+
```
|
65
|
+
|
66
|
+
Don't use cloudant at all, just copy one couchdb over to another:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
cb = CloudantBackup.new
|
70
|
+
[...]
|
71
|
+
cb.host = 'my-couchdb-instance.com'
|
72
|
+
|
73
|
+
cb.replicate
|
74
|
+
```
|
75
|
+
|
76
|
+
Thank you for your business!
|
77
|
+
|
78
|
+
![](https://dl.dropboxusercontent.com/u/1953503/gifs/5eNxg9u.jpg)
|
79
|
+
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
1. Fork it
|
84
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
87
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cloudant_backup/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cloudant_backup"
|
8
|
+
spec.version = CloudantBackup::VERSION
|
9
|
+
spec.authors = ["Jannis Hermanns"]
|
10
|
+
spec.email = ["jannis@gmail.com"]
|
11
|
+
spec.description = %q{Makes a backup of a cloudant db}
|
12
|
+
spec.summary = %q{Why yes, I made a gem that does nothing but two http requests}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_dependency "rest-client"
|
25
|
+
spec.add_dependency "guard-rspec"
|
26
|
+
spec.add_dependency "guard-bundler"
|
27
|
+
spec.add_dependency "guard"
|
28
|
+
end
|
data/console
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
|
3
|
+
|
4
|
+
require 'bundler'
|
5
|
+
Bundler.require
|
6
|
+
|
7
|
+
require 'irb'
|
8
|
+
require 'cloudant_backup'
|
9
|
+
|
10
|
+
def reload!
|
11
|
+
@loaded_files ||= {}
|
12
|
+
count = 0
|
13
|
+
|
14
|
+
Dir['./lib/**/*.rb'].each do |file|
|
15
|
+
mtime = File.stat(file).mtime
|
16
|
+
if !@loaded_files.has_key?(file) or mtime > @loaded_files[file]
|
17
|
+
STDERR.puts "mtime for #{file} changed, reloading"
|
18
|
+
load file
|
19
|
+
@loaded_files[file] = mtime
|
20
|
+
count += 1
|
21
|
+
end
|
22
|
+
end
|
23
|
+
"reloaded #{count} files"
|
24
|
+
end
|
25
|
+
|
26
|
+
IRB.start
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'json'
|
3
|
+
require 'rest_client'
|
4
|
+
|
5
|
+
class CloudantBackup
|
6
|
+
module Backup
|
7
|
+
attr_accessor :user, :password, :source, :target, :host, :date_pattern, :cloudant_host
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
end
|
11
|
+
|
12
|
+
def backup
|
13
|
+
create_target_db
|
14
|
+
replicate
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_target_db
|
18
|
+
make_request(:put, target_db)
|
19
|
+
rescue RestClient::PreconditionFailed
|
20
|
+
# When the db existed already, we're fine with that
|
21
|
+
end
|
22
|
+
|
23
|
+
def replicate
|
24
|
+
data = {
|
25
|
+
source: source_db_url,
|
26
|
+
target: target_db_url
|
27
|
+
}.to_json
|
28
|
+
make_request(:post, "_replicate", data)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def date_string
|
34
|
+
Date.today.strftime(@date_pattern || '%Y_%m_%d')
|
35
|
+
end
|
36
|
+
|
37
|
+
def host
|
38
|
+
return @host if @host
|
39
|
+
subdomain = @cloudant_host || @user # Cloudant's default is username==hostname (for the main user)
|
40
|
+
"#{subdomain}.cloudant.com"
|
41
|
+
end
|
42
|
+
|
43
|
+
def validate_options!
|
44
|
+
raise "Need to set source" unless @source
|
45
|
+
end
|
46
|
+
|
47
|
+
def target_db
|
48
|
+
@target || "#{@source}-backup_#{date_string}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def target_db_url
|
52
|
+
db_url(target_db)
|
53
|
+
end
|
54
|
+
|
55
|
+
def source_db_url
|
56
|
+
db_url(@source)
|
57
|
+
end
|
58
|
+
|
59
|
+
def db_url(name)
|
60
|
+
"https://#{user}:#{password}@#{host}/#{name}"
|
61
|
+
end
|
62
|
+
|
63
|
+
def make_request(method, url, data = nil)
|
64
|
+
options = {
|
65
|
+
content_type: :json,
|
66
|
+
accept: :json
|
67
|
+
}
|
68
|
+
RestClient.send(*[method, db_url(url), data, options].compact)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CloudantBackup do
|
4
|
+
let(:options) do
|
5
|
+
{ content_type: :json, accept: :json }
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:date){ Date.today.strftime('%Y_%m_%d') }
|
9
|
+
|
10
|
+
let(:cb) do
|
11
|
+
cb = CloudantBackup.new
|
12
|
+
cb.user = 'backup-user'
|
13
|
+
cb.password = 'password'
|
14
|
+
cb.source = 'production-db'
|
15
|
+
cb.cloudant_host = 'main-user'
|
16
|
+
cb
|
17
|
+
end
|
18
|
+
|
19
|
+
context "automatic target db name" do
|
20
|
+
it "creates the backup db" do
|
21
|
+
url = "https://backup-user:password@main-user.cloudant.com/production-db-backup_#{date}"
|
22
|
+
RestClient.should_receive(:put).with(url, options)
|
23
|
+
cb.create_target_db
|
24
|
+
end
|
25
|
+
|
26
|
+
it "does not complain when the target existed already" do
|
27
|
+
url = "https://backup-user:password@main-user.cloudant.com/production-db-backup_#{date}"
|
28
|
+
RestClient.should_receive(:put).with(url, options).and_raise(RestClient::PreconditionFailed)
|
29
|
+
cb.create_target_db
|
30
|
+
end
|
31
|
+
|
32
|
+
it "triggers the replication" do
|
33
|
+
url = "https://backup-user:password@main-user.cloudant.com/_replicate"
|
34
|
+
source_db = "https://backup-user:password@main-user.cloudant.com/production-db"
|
35
|
+
data = {
|
36
|
+
source: source_db,
|
37
|
+
target: "#{source_db}-backup_#{date}"
|
38
|
+
}.to_json
|
39
|
+
RestClient.should_receive(:post).with(url, data, options)
|
40
|
+
cb.replicate
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "target db override" do
|
45
|
+
it "creates the backup db" do
|
46
|
+
cb.target = 'custom-name'
|
47
|
+
url = "https://backup-user:password@main-user.cloudant.com/custom-name"
|
48
|
+
RestClient.should_receive(:put).with(url, options)
|
49
|
+
cb.create_target_db
|
50
|
+
end
|
51
|
+
|
52
|
+
it "triggers the replication" do
|
53
|
+
cb.target = 'custom-name'
|
54
|
+
url = "https://backup-user:password@main-user.cloudant.com/_replicate"
|
55
|
+
source_db = "https://backup-user:password@main-user.cloudant.com/production-db"
|
56
|
+
target_db = "https://backup-user:password@main-user.cloudant.com/custom-name"
|
57
|
+
data = {
|
58
|
+
source: source_db,
|
59
|
+
target: target_db
|
60
|
+
}.to_json
|
61
|
+
RestClient.should_receive(:post).with(url, data, options)
|
62
|
+
cb.replicate
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "not using cloudant at all" do
|
67
|
+
it "creates the backup db" do
|
68
|
+
cb.host = 'my-couchdb.com'
|
69
|
+
cb.target = 'custom-name'
|
70
|
+
url = "https://backup-user:password@my-couchdb.com/custom-name"
|
71
|
+
RestClient.should_receive(:put).with(url, options)
|
72
|
+
cb.create_target_db
|
73
|
+
end
|
74
|
+
|
75
|
+
it "triggers the replication" do
|
76
|
+
cb.target = 'custom-name'
|
77
|
+
cb.host = 'my-couchdb.com'
|
78
|
+
url = "https://backup-user:password@my-couchdb.com/_replicate"
|
79
|
+
source_db = "https://backup-user:password@my-couchdb.com/production-db"
|
80
|
+
target_db = "https://backup-user:password@my-couchdb.com/custom-name"
|
81
|
+
data = {
|
82
|
+
source: source_db,
|
83
|
+
target: target_db
|
84
|
+
}.to_json
|
85
|
+
RestClient.should_receive(:post).with(url, data, options)
|
86
|
+
cb.replicate
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloudant_backup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jannis Hermanns
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
type: :development
|
29
|
+
prerelease: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rest-client
|
48
|
+
version_requirements: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
type: :runtime
|
61
|
+
prerelease: false
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard-rspec
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: guard-bundler
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
type: :runtime
|
93
|
+
prerelease: false
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: guard
|
96
|
+
version_requirements: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirement: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
type: :runtime
|
109
|
+
prerelease: false
|
110
|
+
description: Makes a backup of a cloudant db
|
111
|
+
email:
|
112
|
+
- jannis@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- Gemfile
|
119
|
+
- Guardfile
|
120
|
+
- LICENSE.txt
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- cloudant_backup.gemspec
|
124
|
+
- console
|
125
|
+
- lib/cloudant_backup.rb
|
126
|
+
- lib/cloudant_backup/backup.rb
|
127
|
+
- lib/cloudant_backup/version.rb
|
128
|
+
- spec/lib/cloudant_backup_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
homepage: ''
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
none: false
|
145
|
+
requirements:
|
146
|
+
- - ! '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 1.8.25
|
152
|
+
signing_key:
|
153
|
+
specification_version: 3
|
154
|
+
summary: Why yes, I made a gem that does nothing but two http requests
|
155
|
+
test_files:
|
156
|
+
- spec/lib/cloudant_backup_spec.rb
|
157
|
+
- spec/spec_helper.rb
|