Class: EverTools::ElasticsearchS3Backup

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/elasticsearch_s3_backup.rb,
lib/elasticsearch_s3_backup/version.rb

Constant Summary collapse

VERSION =
'2.0.4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeElasticsearchS3Backup

rubocop:enable Metrics/AbcSize, Lint/RescueException



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/elasticsearch_s3_backup.rb', line 68

def initialize
  @conf = OpenStruct.new(YAML.load_file('/etc/s3_backup.yml'))

  if sentry_dsn
    Raven.configure do |config|
      config.dsn = sentry_dsn
      config.logger = logger
    end
  end

  now                 = Time.new.utc
  @backup_test_index  = "backup_test_#{now.to_i}"
  @restore_test_index = "restore_test_#{now.to_i}"
  @backup_repo        = now.strftime '%m-%Y'
  @snapshot_label     = now.strftime '%m-%d_%H%M'
end

Instance Attribute Details

#backup_repoObject (readonly)

Returns the value of attribute backup_repo.



25
26
27
# File 'lib/elasticsearch_s3_backup.rb', line 25

def backup_repo
  @backup_repo
end

#confObject (readonly)

Returns the value of attribute conf.



25
26
27
# File 'lib/elasticsearch_s3_backup.rb', line 25

def conf
  @conf
end

#snapshot_labelObject (readonly)

Returns the value of attribute snapshot_label.



25
26
27
# File 'lib/elasticsearch_s3_backup.rb', line 25

def snapshot_label
  @snapshot_label
end

Instance Method Details

#runObject

rubocop:disable Metrics/AbcSize, Lint/RescueException



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/elasticsearch_s3_backup.rb', line 28

def run
  unless master?
    logger.info 'This node is not the currently elected master. Exiting.'
    exit
  end

  wait_for_cluster_state!

  cleanup_test_indexes
  insert_test_data

  # Create a new repo if none exists (typically at beginning of month)
  create_repo unless es_api.snapshot.get_repository[backup_repo]
  create_snapshot

  restore_test_index
  # Compare each doc in the original backup_test index to the restored index
  logger.info "Verifying the newly-restored #{@backup_test_index}"
  test_size.times { |i| compare_index_item! i }
  logger.info 'Successfully verified the test data!'
  delete_test_indexes

  remove_expired_backups
  logger.info 'Finished'
rescue Interrupt => e
  puts "Received #{e.class}"
  exit 99
rescue SignalException => e
  logger.info "Received: #{e.signm} (#{e.signo})"
  exit 2
rescue SystemExit => e
  exit e.status
rescue Exception => e # Need to rescue "Exception" so that Sentry gets it
  notify e
  logger.fatal e.message
  logger.fatal e.backtrace.join("\n")
  raise e
end