Class: Backup::Database::Elasticsearch
- Inherits:
-
Base
- Object
- Base
- Backup::Database::Elasticsearch
- Defined in:
- lib/elasticsearch/extensions/backup.rb
Overview
Integration with the Backup gem [github.com/meskyanichi/backup/]
This extension allows to backup Elasticsearch indices as flat JSON files on the disk.
Use the Backup gem’s DSL to configure the backup:
require 'elasticsearch/extensions/backup'
Model.new(:elasticsearch_backup, 'Elasticsearch') do
database Elasticsearch do |db|
# db.url = 'http://localhost:9200'
# db.indices = 'articles,people'
# db.size = 500
# db.scroll = '10m'
end
store_with Local do |local|
local.path = '/usr/local/var/backups'
local.keep = 3
end
compress_with Gzip
end
Perform the backup with the Backup gem’s command line utility:
$ backup perform -t elasticsearch_backup
A simple recover script could look like this:
PATH = '/path/to/backup/'
require 'elasticsearch'
client = Elasticsearch::Client.new log: true
payload = []
Dir[ File.join( PATH, '**', '*.json' ) ].each do |file|
document = MultiJson.load(File.read(file))
item = document.merge(data: document['_source'])
document.delete('_source')
document.delete('_score')
payload << { index: item }
if payload.size == 100
client.bulk body: payload
payload = []
end
client.bulk body: payload
end
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#indices ⇒ Object
Returns the value of attribute indices.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#scroll ⇒ Object
Returns the value of attribute scroll.
-
#size ⇒ Object
Returns the value of attribute size.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #__perform_single ⇒ Object
- #client ⇒ Object
-
#initialize(model, database_id = nil, &block) ⇒ Elasticsearch
constructor
A new instance of Elasticsearch.
- #logger ⇒ Object
- #path ⇒ Object
- #perform! ⇒ Object
Constructor Details
#initialize(model, database_id = nil, &block) ⇒ Elasticsearch
Returns a new instance of Elasticsearch.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/elasticsearch/extensions/backup.rb', line 81 def initialize(model, database_id = nil, &block) super @url ||= 'http://localhost:9200' @indices ||= '_all' @size ||= 100 @scroll ||= '10m' @mode ||= 'single' instance_eval(&block) if block_given? end |
Instance Attribute Details
#indices ⇒ Object
Returns the value of attribute indices.
74 75 76 |
# File 'lib/elasticsearch/extensions/backup.rb', line 74 def indices @indices end |
#mode ⇒ Object
Returns the value of attribute mode.
79 80 81 |
# File 'lib/elasticsearch/extensions/backup.rb', line 79 def mode @mode end |
#scroll ⇒ Object
Returns the value of attribute scroll.
74 75 76 |
# File 'lib/elasticsearch/extensions/backup.rb', line 74 def scroll @scroll end |
#size ⇒ Object
Returns the value of attribute size.
74 75 76 |
# File 'lib/elasticsearch/extensions/backup.rb', line 74 def size @size end |
#url ⇒ Object
Returns the value of attribute url.
74 75 76 |
# File 'lib/elasticsearch/extensions/backup.rb', line 74 def url @url end |
Instance Method Details
#__perform_single ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/elasticsearch/extensions/backup.rb', line 125 def __perform_single r = client.search index: indices, search_type: 'scan', scroll: scroll, size: size raise Error, "No scroll_id returned in response:\n#{r.inspect}" unless r['_scroll_id'] while r = client.scroll(scroll_id: r['_scroll_id'], scroll: scroll) and not r['hits']['hits'].empty? do r['hits']['hits'].each do |hit| FileUtils.mkdir_p "#{path.join hit['_index'], hit['_type']}" File.open("#{path.join hit['_index'], hit['_type'], hit['_id']}.json", 'w') do |file| file.write MultiJson.dump(hit) end end end end |
#client ⇒ Object
106 107 108 |
# File 'lib/elasticsearch/extensions/backup.rb', line 106 def client @client ||= ::Elasticsearch::Client.new url: url, logger: logger end |
#logger ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/elasticsearch/extensions/backup.rb', line 114 def logger logger = Backup::Logger.__send__(:logger) logger.instance_eval do def debug(*args);end # alias :debug :info alias :fatal :warn end logger end |
#path ⇒ Object
110 111 112 |
# File 'lib/elasticsearch/extensions/backup.rb', line 110 def path Pathname.new File.join(dump_path , dump_filename.downcase) end |
#perform! ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/elasticsearch/extensions/backup.rb', line 93 def perform! super case mode when 'single' __perform_single else raise Error, "Unsupported mode [#{mode}]" end log!(:finished) end |