Skip to content

Instantly share code, notes, and snippets.

@rottenbytes
Created September 4, 2014 13:48
Show Gist options
  • Save rottenbytes/37bc212e85260c36e30a to your computer and use it in GitHub Desktop.
Save rottenbytes/37bc212e85260c36e30a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "elasticsearch"
require "getoptlong"
def help()
puts "Usage : snapshot.rb [options]"
puts "Options :"
puts " --host : specifies host to connect to"
puts " --repository : repository for the snapshot. must exist before calling this script"
puts " --snapshot : snapshot name"
puts " --indices : indices to snapshot, if several use the format 'indice1,indice2'"
puts " --help : this help"
end
opts = GetoptLong.new(
[ "--host", "-H", GetoptLong::OPTIONAL_ARGUMENT ],
[ "--repository", "-r", GetoptLong::REQUIRED_ARGUMENT ],
[ "--snapshot", "-s", GetoptLong::REQUIRED_ARGUMENT ],
[ "--indices", "-i", GetoptLong::REQUIRED_ARGUMENT ],
[ "--help", "-h", GetoptLong::NO_ARGUMENT ]
)
host = "localhost"
repository = nil
snapshot = nil
indices = nil
opts.each do |opt, arg|
case opt
when "--host"
host = arg
when "--repository"
repository = arg
when "--snapshot"
snapshot = arg
when "--indices"
indices = arg
when "--help"
help()
end
end
if repository.nil? or snapshot.nil? or indices.nil?
puts "--repository, --snapshot and --indices cannot be empty"
exit 1
end
client = Elasticsearch::Client.new(:host => host)
repositories = client.snapshot.get_repository.keys
unless repositories.include?(repository)
puts "repository #{repository} does not exist !"
puts "Available repositories :"
repositories.each do |r|
puts " * #{r}"
end
exit 1
end
result = client.snapshot.create(:repository => repository, :snapshot => snapshot, :body => { :indices => indices}, :wait_for_completion => true)
unless result["snapshot"]["state"] == "SUCCESS"
puts "Something went wrong during snapshot"
puts "full ES answer below :"
puts result.inspect
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment