-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock-and-snap.rb
More file actions
executable file
·49 lines (35 loc) · 1.34 KB
/
lock-and-snap.rb
File metadata and controls
executable file
·49 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
require 'cucloud'
require_relative 'mysql_util'
# required
db_host = ENV['DB_HOST']
db_user = ENV['DB_USER']
db_password = ENV['DB_PASSWORD']
rds_id = ENV['DB_RDS_ID']
# optional
creator_tag = ENV['CREATOR_TAG']
application_tag = ENV['APPLICATION_TAG']
environement_tag = ENV['ENVIRONMENT_TAG']
if db_host.nil? || db_user.nil? || db_password.nil? || rds_id.nil?
abort('Expecting the following environment variables to be set: ' \
'DB_HOST, DB_USER, DB_PASSWORD, DB_RDS_ID')
end
mysql2_client = get_mysql2_client(db_host, db_user, db_password)
tables = get_myisam_tables(mysql2_client)
puts "Number of target MyISAM tables: #{tables.size}"
puts 'Locking tables.'
flush_and_lock_tables(mysql2_client, tables)
puts 'Creating snapshot, and waiting for completion.'
rds_utils = Cucloud::RdsUtils.new
rds_instance = rds_utils.get_instance(rds_id)
# Add tags if values are present
tags = []
tags << { key: 'Creator', value: creator_tag } unless creator_tag.nil?
tags << { key: 'Application', value: application_tag } unless application_tag.nil?
tags << { key: 'Environment', value: environement_tag } unless environement_tag.nil?
snap = rds_utils.create_snapshot_and_wait_until_available(rds_instance, tags)
puts 'Unlocking tables.'
unlock_tables(mysql2_client)
mysql2_client.close
abort('Unable to create snapshot.') if snap.nil?
exit