bungee 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bungee.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jack Chen (chendo)
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.
@@ -0,0 +1,29 @@
1
+ # Bungee
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bungee'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bungee
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'commander/import'
5
+
6
+ $LOAD_PATH << File.join(File.dirname(__FILE__), "../lib")
7
+
8
+ require 'bundler/setup'
9
+ require 'bungee'
10
+
11
+ program :version, Bungee::VERSION
12
+ program :description, 'Backs up and restores Elasticsearch indexes'
13
+
14
+ command :backup do |c|
15
+ c.syntax = 'bungee backup [options] <index>'
16
+ c.summary = ''
17
+ c.description = ''
18
+ c.example 'Performs a backup from 3 nodes', 'bungee backup --backup-path="backup_host:index_foo" --data-path=/var/lib/elasticsearch/my_cluster --hosts="elastic1,elastic2,elastic3" index_foo'
19
+ c.option "--data-path PATH", String, "Path to Elasticsearch data directory on server"
20
+ c.option "--hosts HOSTS", Array, "List of elasticsearch hosts the backup should be done from"
21
+ c.option "--url ES_URL", String, "URL to Elasticsearch"
22
+ c.option "--backup-path PATH", String, "Path to backup destination reachable by all servers. <user>@<host>:<path> allowed"
23
+ c.action do |args, options|
24
+ opts = {
25
+ :index => args.first,
26
+ :hosts => options.hosts,
27
+ :data_path => options.data_path,
28
+ :backup_path => options.backup_path,
29
+ :elasticsearch_url => options.url,
30
+ }
31
+ backup = Bungee::Backup.new(opts)
32
+ backup.backup!
33
+ end
34
+ end
35
+
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bungee/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "bungee"
8
+ gem.version = Bungee::VERSION
9
+ gem.authors = ["Jack Chen (chendo)"]
10
+ gem.email = ["gems+bungee#chen.do"]
11
+ gem.description = %q{Performs hot backups and restores of Elasticsearch indexes}
12
+ gem.summary = %q{Performs hot backups and restores of Elasticsearch indexes}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('rye', '~> 0.9.2')
21
+ gem.add_dependency('commander', '~> 4.1.3')
22
+ gem.add_dependency('json')
23
+ end
@@ -0,0 +1,6 @@
1
+ require "bungee/version"
2
+ require "bungee/backup"
3
+
4
+ module Bungee
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,94 @@
1
+ require 'rye'
2
+ require 'benchmark'
3
+ require 'json'
4
+
5
+ module Bungee
6
+ class Backup
7
+ attr_reader :hosts, :index, :elasticsearch_url, :first_host, :cluster, :data_path
8
+
9
+ def initialize(options = {})
10
+ @index = options[:index]
11
+ @elasticsearch_url = options[:elasticsearch_url] || 'http://localhost:9200'
12
+ @data_path = options[:data_path]
13
+ @backup_path = options[:backup_path]
14
+
15
+ @hosts = Rye::Set.new('default', :safe => false)
16
+ boxes = options[:hosts].map { |host| Rye::Box.new(host, :safe => false) }
17
+ @first_host = boxes.first
18
+ @hosts.add_boxes(boxes)
19
+ end
20
+
21
+ def backup!
22
+ flush_index
23
+ disable_translog_flushing
24
+ rsync_index_locally
25
+ enable_translog_flushing
26
+ merge_shards_to_backup_path
27
+ end
28
+
29
+ protected
30
+
31
+ def flush_index
32
+ es_request('POST', "/#{index}/_flush")
33
+ end
34
+
35
+ def disable_translog_flushing
36
+ set_translog_disable_flush(true)
37
+ end
38
+
39
+ def enable_translog_flushing
40
+ set_translog_disable_flush(false)
41
+ end
42
+
43
+ def set_translog_disable_flush(status)
44
+ es_request("PUT", "/#{index}/_settings", {
45
+ 'index.translog.disable_flush' => status
46
+ })
47
+ end
48
+
49
+ def rsync_index_locally
50
+ puts "rsyncing to local copy"
51
+ hosts.parallel = true
52
+ time("Local Rsync") do
53
+ hosts.rsync("-rpvi", "--delete", "#{data_path}/nodes/0/indices/#{index}/", "/tmp/bungee_#{index}")
54
+ end
55
+ end
56
+
57
+ def merge_shards_to_backup_path
58
+ puts "merging shards"
59
+ first, *rest = hosts.boxes
60
+ # Delete to clean out the folder
61
+ time("First RSync") do
62
+ first.rsync("-rpvi", "--delete", "/tmp/bungee_#{index}/", @backup_path)
63
+ end
64
+ rest.each do |box|
65
+ time("Rsync") do
66
+ box.rsync("-rpvi", "/tmp/bungee_#{index}/", @backup_path)
67
+ end
68
+ end
69
+ end
70
+
71
+ def time(caption, &block)
72
+ puts caption
73
+ puts Benchmark.measure(&block)
74
+ end
75
+
76
+ def es_request(verb, path, data = nil)
77
+ args = ["-s", "-X", verb, "#{elasticsearch_url}#{path}"]
78
+ args << "-d" << "'#{JSON.dump(data)}'" if data
79
+ puts "Performing: #{args.join(' ')}"
80
+ ret = first_host.curl(*args)
81
+ if ret.exit_status.zero?
82
+ out = JSON.parse(ret.to_s)
83
+ else
84
+ $stderr.puts("Error performing command: #{ret.cmd}")
85
+ $stderr.puts("Stdout:")
86
+ $stderr.puts(ret.stdout)
87
+ $stderr.puts("Stderr:")
88
+ $stderr.puts(ret.stderr)
89
+ $stderr.puts("Exit Code: #{ret.exit_status}")
90
+ nil
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ module Bungee
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bungee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jack Chen (chendo)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rye
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: commander
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 4.1.3
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 4.1.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Performs hot backups and restores of Elasticsearch indexes
63
+ email:
64
+ - gems+bungee#chen.do
65
+ executables:
66
+ - bungee
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/bungee
76
+ - bungee.gemspec
77
+ - lib/bungee.rb
78
+ - lib/bungee/backup.rb
79
+ - lib/bungee/version.rb
80
+ homepage: ''
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Performs hot backups and restores of Elasticsearch indexes
104
+ test_files: []