elasticsearch-snapshot 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3baa2e277201c3605f2acd8b2a91aa8f56cf6028
4
+ data.tar.gz: e7bc8ca73a82312a14f5e5aeb84c9c37bff0484f
5
+ SHA512:
6
+ metadata.gz: 6d82001e9b15ecc72efd886e30336856315b7afb3db50430795cd7e392e2908017a24c10710ba9b100b0a48d9204b51ae8f7997b4ca052cead70d83d2ab913b8
7
+ data.tar.gz: 25bac3a8f686bcc85904ca343b23ea4cafb1f3900ea0ce3dfc363a7c94288509048a80a7977adb09b5ea1aba156b92b0be1938091ded09f1322e31fca3137ba6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elasticsearch-snapshot.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Elasticsearch::Snapshot
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/elasticsearch/snapshot`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'elasticsearch-snapshot'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install elasticsearch-snapshot
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/elasticsearch-snapshot.
36
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "elasticsearch/snapshot"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elasticsearch/snapshot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elasticsearch-snapshot"
8
+ spec.version = Elasticsearch::Snapshot::VERSION
9
+ spec.authors = ["Vlad Gorodetsky"]
10
+ spec.email = ["v@gor.io"]
11
+
12
+ spec.summary = %q{Snapshots, restores, and migrates ElasticSearch indices.}
13
+ spec.description = %q{Snapshots, restores, and migrates ElasticSearch indices between clusters.}
14
+ spec.homepage = "https://github.com/bai/elasticsearch-snapshot"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "thor", "~> 0.19.1"
24
+ end
@@ -0,0 +1,50 @@
1
+ module Elasticsearch
2
+ module Snapshot
3
+ class Migrate < Task
4
+ attr_reader :source, :target, :repo
5
+
6
+ def initialize(source, target, repo)
7
+ @source = smart_add_url_protocol(source)
8
+ @target = smart_add_url_protocol(target)
9
+ @repo = repo
10
+ end
11
+
12
+ def perform
13
+ begin
14
+ list_indexes_to_backup = JSON(open(command_list_indexes_to_backup(source)).read)['indices'].keys.delete_if { |i| i =~ NON_RESTORABLE_INDICES_REGEX }.join(',')
15
+ rescue
16
+ puts "Unable to get indices from ElasticSearch."
17
+ exit(1)
18
+ end
19
+
20
+ begin
21
+ snapshots = JSON(open(command_get_last_successful_snapshot(source, repo)).read)
22
+ rescue
23
+ puts "Unable to get list of snapshots from ElasticSearch. Does `#{repo}` repository exist?"
24
+ exit(1)
25
+ end
26
+
27
+ if snapshots["snapshots"].last["state"] == "SUCCESS"
28
+ begin
29
+ snapshot_pipe = IO.popen command_close_indices(target, list_indexes_to_backup)
30
+ Process.waitall
31
+ rescue
32
+ puts "Unable to close indices"
33
+ exit(1)
34
+ end
35
+
36
+ snapshot_date = snapshots["snapshots"].last["snapshot"]
37
+ puts "Restoring from snapshot #{snapshot_date}"
38
+ snapshot_pipe = IO.popen command_restore_snapshot(target, repo, snapshot_date, list_indexes_to_backup)
39
+ Process.waitall
40
+
41
+ restore_result = snapshot_pipe.readlines
42
+ puts restore_result
43
+ else
44
+ puts "Last snapshot was not successful."
45
+ exit(1)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,36 @@
1
+ module Elasticsearch
2
+ module Snapshot
3
+ class Purge < Task
4
+ attr_reader :cluster, :repo, :keep
5
+
6
+ def initialize(cluster, repo, keep = 30)
7
+ @cluster = cluster
8
+ @repo = repo
9
+ @keep = keep
10
+ end
11
+
12
+ def perform
13
+ raise unless keep > 0
14
+
15
+ snapshot_pipe = IO.popen command_list_snapshots(cluster, repo)
16
+ Process.waitall
17
+
18
+ result = snapshot_pipe.readlines
19
+ raise if result.length > 1
20
+
21
+ result = JSON.load result.first
22
+ raise unless result['snapshots']
23
+
24
+ result['snapshots'][0...-keep].each do |s|
25
+ snapshot_to_delete = command_delete_a_snapshot(cluster, repo) + "/#{s['snapshot']}"
26
+ pipe = IO.popen snapshot_to_delete
27
+ Process.waitall
28
+ result = JSON.load(pipe.readlines.first)
29
+ info("Purged snapshot #{s['snapshot']}") if result['acknowledged'] == true
30
+ end
31
+ rescue
32
+ error "Cleanup aborted: #{$!}"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ module Elasticsearch
2
+ module Snapshot
3
+ class Snapshot < Task
4
+ attr_reader :cluster, :repo
5
+
6
+ def initialize(cluster, repo)
7
+ @cluster = cluster
8
+ @repo = repo
9
+ end
10
+
11
+ def perform
12
+ list_indexes_to_backup = JSON(open(command_list_indexes_to_backup(cluster)).read)['indices'].keys.delete_if { |i| i =~ NON_RESTORABLE_INDICES_REGEX }
13
+
14
+ info "Snapshotting indexes: #{list_indexes_to_backup.join(', ')}"
15
+
16
+ snapshot_pipe = IO.popen command_take_a_snapshot(cluster, repo) + %Q( -d '{"indices": "#{list_indexes_to_backup.join(',')}","include_global_state":false}')
17
+ Process.waitall
18
+
19
+ snapshot_result = snapshot_pipe.readlines
20
+ raise if snapshot_result.length > 1
21
+
22
+ snapshot_result = JSON.load snapshot_result.first
23
+
24
+ if snapshot_result.keys.include? 'error'
25
+ error snapshot_result['error']
26
+ else
27
+ raise unless snapshot_result['snapshot']
28
+ if snapshot_result['snapshot']['state'] == 'SUCCESS'
29
+ info "Snapshot took #{snapshot_result['snapshot']['duration_in_millis']} ms"
30
+ else
31
+ error "Snapshot failed: #{snapshot_result['failures'].join(' ')}"
32
+ end
33
+ end
34
+ rescue
35
+ error "Snapshot aborted: #{$!}"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,53 @@
1
+ module Elasticsearch
2
+ module Snapshot
3
+ class Task
4
+ NON_RESTORABLE_INDICES_REGEX = /marvel|kibana/
5
+
6
+ protected
7
+
8
+ def command_list_indexes_to_backup(cluster)
9
+ %Q(#{cluster}/_stats/indices)
10
+ end
11
+
12
+ def command_take_a_snapshot(cluster, repo)
13
+ %Q(curl -s -X PUT #{cluster}/_snapshot/#{repo}/$(date +"%Y%m%d-%H%M%S")?wait_for_completion=true)
14
+ end
15
+
16
+ def command_list_snapshots(cluster, repo)
17
+ %Q(curl -s #{cluster}/_snapshot/#{repo}/_all)
18
+ end
19
+
20
+ def command_delete_a_snapshot(cluster, repo)
21
+ %Q(curl -s -X DELETE #{cluster}/_snapshot/#{repo})
22
+ end
23
+
24
+ def command_get_last_successful_snapshot(cluster, repo)
25
+ %Q(#{cluster}/_snapshot/#{repo}/_all?pretty=true)
26
+ end
27
+
28
+ def command_close_indices(cluster, indices)
29
+ %Q(curl -XPOST #{cluster}/#{indices}/_close)
30
+ end
31
+
32
+ def command_restore_snapshot(cluster, repo, snapshot_name, indices)
33
+ %Q(curl -XPOST #{cluster}/_snapshot/#{repo}/#{snapshot_name}/_restore -d '{"partial":true,"indices":"#{indices}","include_global_state":false}')
34
+ end
35
+
36
+ def info(msg)
37
+ puts '[INFO]' + msg
38
+ end
39
+
40
+ def error(msg)
41
+ puts '[ERROR]' + msg
42
+ end
43
+
44
+ def smart_add_url_protocol(uri)
45
+ if uri[/\Ahttp:\/\//] || uri[/\Ahttps:\/\//]
46
+ uri
47
+ else
48
+ "http://#{uri}"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module Elasticsearch
2
+ module Snapshot
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'open-uri'
2
+
3
+ require "elasticsearch/snapshot/version"
4
+ require "elasticsearch/snapshot/task"
5
+ require "elasticsearch/snapshot/snapshot"
6
+ require "elasticsearch/snapshot/purge"
7
+ require "elasticsearch/snapshot/migrate"
8
+
9
+ module Elasticsearch
10
+ module Snapshot
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticsearch-snapshot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vlad Gorodetsky
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.19.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.19.1
55
+ description: Snapshots, restores, and migrates ElasticSearch indices between clusters.
56
+ email:
57
+ - v@gor.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - elasticsearch-snapshot.gemspec
70
+ - lib/elasticsearch/snapshot.rb
71
+ - lib/elasticsearch/snapshot/migrate.rb
72
+ - lib/elasticsearch/snapshot/purge.rb
73
+ - lib/elasticsearch/snapshot/snapshot.rb
74
+ - lib/elasticsearch/snapshot/task.rb
75
+ - lib/elasticsearch/snapshot/version.rb
76
+ homepage: https://github.com/bai/elasticsearch-snapshot
77
+ licenses: []
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.8
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Snapshots, restores, and migrates ElasticSearch indices.
99
+ test_files: []