public
Description: Some Ruby Tools to work on Amazon S3 and EC2, such as buckets list, creation, mysql backup to S3, cron jobs, and Gentoo specific little hacks... ;)
Homepage:
Clone URL: git://github.com/fred/amazon_ruby_tools.git
amazon_ruby_tools / mysql_backup_S3.rb
100755 372 lines (318 sloc) 9.029 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#######################################
#### WRITEN by Frederico de Souza #####
#### fred.the.master@gmail.com #####
#### Free to use as in free beer #####
#######################################
 
 
require 'rubygems'
require 'aws/s3'
require 'fastthread'
require 'pathname'
require 'ftools'
require "fileutils"
 
 
#########################
## SCRIPT SETTINGS ##
#########################
 
@unattended_mode = true
@access_key_id = ENV['AMAZON_ACCESS_KEY_ID']
@secret_access_key = ENV['AMAZON_SECRET_ACCESS_KEY']
@bucket_name = ENV['AMAZON_MYSQL_BUCKET_NAME']
@home = ENV['HOME']
 
@time = Time.now
 
# nice value: -19 to 19
# default 0
@nice = 18
 
# lzma compression rates: 1-2 (fast) 3-9 (slow)
# default 7
@lzma_compress_rate = 2
 
@data_dir = "#{@home}/backup/mysql/tmp/#{@time.strftime("%Y")}/#{@time.strftime("%m")}"
@done_data_dir = "#{@home}/backup/mysql/#{@time.strftime("%Y")}/#{@time.strftime("%m")}"
@filename = "#{@time.strftime("%Y%m%d_%H%M%S")}"
 
# TODO
# Array of databases to backup
@databases = [
  {:name => "ul2", :dump_options => "--no-create-info", :append_name => ""},
  {:name => "ul2_dev", :dump_options => "--no-create-info", :append_name => ""},
  {:name => "ul2_prod", :dump_options => "--no-create-info", :append_name => ""}
]
 
# If this is true, backup all databases
@all_databases = true
 
# Extra options to append to mysqldump
@extra_dump_options = ""
 
# Username / Password to access DB
# it's good to create a user with READ only access to all databases.
# for example: GRANT SELECT ON *.* TO 'fred'@'localhost' IDENTIFIED by 'fred'
if ENV['DB_USERNAME']
  @db_username = ENV['DB_USERNAME']
else
  @db_username = "root"
end
if ENV['DB_PASSWORD']
  @db_password = ENV['DB_PASSWORD']
else
  @db_password = ""
end
 
@lines = "\n----------------------------------------------------------"
 
if @unattended_mode == false
  puts "Welcome!"
  puts "--------"
  puts "Program Variables:"
  puts "------------------"
  puts "- Server Username: #{@server_username}"
  puts "- Bucket: #{@bucket_name}"
  puts "- Local Dump Dir: #{@data_dir}"
  puts "- Local Time of Dump: #{@time}"
  puts "- Remote Path: #{@remote_destination}"
  puts "- All Databases? #{@all_databases}"
  puts "- DB Username: #{@db_username}"
  puts "- DB Password: Not Shown"
  puts "- Unattended mode: #{@unattended_mode}"
  puts @lines
  puts "Is this Information correct? will continue in 5 seconds"
end
 
def to_file_size(num)
  case num
  when 0
    return "0 byte"
  when 1..1024
    return "1K"
  when 1025..1048576
    kb = num/1024.0
    return "#{f_to_dec(kb)} Kb"
  when 1024577..1049165824
    kb = num/1024.0
    mb = kb / 1024.0
    return "#{f_to_dec(mb)} Mb"
  else
    kb = num/1024.0
    mb = kb / 1024.0
    gb = mb / 1024.0
    return "#{f_to_dec(gb)} Gb"
  end
end
 
def f_to_dec(f, prec=2,sep='.')
  num = f.to_i.to_s
  dig = ((prec-(post=((f*(10**prec)).to_i%(10**prec)).to_s).size).times do post='0'+post end; post)
  return num+sep+dig
end
 
def check_directories
  begin
    FileUtils.mkdir_p(@data_dir,:mode => 0700)
    FileUtils.mkdir_p(@done_data_dir, :mode => 0700)
  rescue
    puts "Cannot create local directory #{@data_dir}"
    puts "Going to use '/tmp/#{@data_dir}' folder instead."
    @data_dir = "/tmp/#{@data_dir}"
  end
  begin
    FileUtils.mkdir_p(@done_data_dir)
  rescue
    puts "Cannot create local directory #{@done_data_dir}"
    puts "Going to use '/tmp/#{@done_data_dir}' folder instead."
    @data_dir = "/tmp/#{@done_data_dir}"
  end
end
 
 
def check_answer
  if @unattended_mode == false
    puts "Press Y to Continue or N no Cancel."
    yes_no = gets
    yes_no.chomp!
    case yes_no
      when "Y","y","Yes","yes"
        puts "Continuing"
      when "N", "n", "No", "no"
        puts "You chose to CANCEL, bye bye."
        exit
      when 'q', 'quit'
        puts "You chose to QUIT, bye bye."
        exit
      else
        puts "Invalid Answer."
        check_answer
    end
  else
    return true
  end
end
 
 
def check_settings
  if !ENV['DB_USERNAME']
    puts "WARNING: Database Username not set, using 'root'"
  end
  if !ENV['DB_PASSWORD']
    puts "WARNING: Database User Password not set, using '' (blank)"
  end
  if !ENV['AMAZON_ACCESS_KEY_ID']
    puts "FATAL: AMAZON_ACCESS_KEY_ID not set, quiting now."
    exit
  end
  if !ENV['AMAZON_SECRET_ACCESS_KEY']
    puts "FATAL: AMAZON_SECRET_ACCESS_KEY not set, quiting now."
    exit
  end
end
 
 
# Function to make the Database Dumps
def mysqldump(options)
    name = options[:name].to_s
    append_name = options[:append_name].to_s
    dump_options = options[:dump_options].to_s
    if @db_password.to_s.empty?
      db_password = ""
    else
      db_password = "-p#{@db_password}"
    end
    file_name = "#{@data_dir}/#{append_name}#{name}_#{dump_options}_#{@filename}.sql"
    puts "Dumping #{options[:name]} into #{file_name}\n"
    command = " nice -n #{@nice} mysqldump -u #{@db_username} #{dump_options} #{db_password} #{@extra_dump_options} #{name} > #{file_name}"
    puts "\nEXECUTING:\n #{command}"
    system(command)
    return file_name
end
 
 
def compress_file(file_name)
  puts "Compressing file #{file_name}."
  # command = " nice tar -cjpf #{file_name}.tar.bz2 #{file_name}"
  command = " nice -n #{@nice} lzma -#{@lzma_compress_rate} -z #{file_name}"
  puts "\nEXECUTING:\n #{command}"
  system(command)
end
 
 
# Function to run the actual mysqldump command
def make_mysql_backup
  if @all_databases
    options = {
      :name => "--all-databases",
      :dump_options => "",
      :append_name => ""
    }
    file_name = mysqldump(options)
    compress_file(file_name)
  end
  if @databases && !@databases.empty?
    @databases.each do |db|
      options = {
        :name => db[:name].to_s,
        :dump_options => db[:dump_options].to_s,
        :append_name => db[:append_name].to_s
      }
      file_name = mysqldump(options)
      compress_file(file_name)
    end
  end
end
 
# Function to stablish connection
def stablish_connection
  begin
    AWS::S3::Base.establish_connection!(
      :access_key_id => @access_key_id,
      :secret_access_key => @secret_access_key
    )
  rescue => exception
    puts @lines
    puts "There was an error: "
    puts exception.to_s
    exit
  end
  puts "Good, Connection Stablished."
end
 
 
def list_buckets
  puts "Current buckets:"
  buckets = AWS::S3::Bucket.list
  if buckets.empty?
    puts "[]"
  else
    buckets.each do |t|
      puts @lines
      puts "Name: #{t.name}"
      puts "Date: #{t.creation_date}"
      puts @lines
    end
  end
end
 
 
# Function to find or create a bucket
def find_bucket(bucket_name)
  if bucket = AWS::S3::Bucket.find(bucket_name)
    puts "Bucket #{bucket_name} found."
    bucket
  else
    puts "The bucket #{bucket_name} could not be found"
    nil
  end
end
 
 
# Function to find or create a bucket
def create_bucket(bucket_name)
  begin
    puts 'Creating the bucket now.'
    if AWS::S3::Bucket.create(bucket_name)
      puts "Good, bucket #{bucket_name} created."
    end
  rescue
    puts "The bucket #{bucket_name} could not be created"
    return
  end
end
 
# Function to find or create a bucket
def find_or_create_bucket
  begin
    AWS::S3::Bucket.find(@bucket_name)
  rescue
    puts "Bucket #{@bucket_name} not found."
    puts 'Creating the bucket now.'
    AWS::S3::Bucket.create(@bucket_name)
    retry
  end
  puts "Good, bucket #{@bucket_name} found."
end
 
 
# Function to send data to bucket
def send_data
  puts "Full Pathname localy is #{@data_dir}."
  @files_count = 0
  @data_transferred = 0
  
  p = Pathname.new(@data_dir)
  p.children.each do |item|
    file_name = item.relative_path_from(Pathname.new(@data_dir)).to_s
    @files_count += 1
    @data_transferred += item.size
    puts "Putting Local File: '#{item}'"
    puts "To bucket: '#{@bucket_name}/#{file_name}'"
    AWS::S3::S3Object.store(file_name, open(item), @bucket_name)
    puts @lines
    puts "Moving #{file_name} to #{@done_data_dir}"
    FileUtils.mv(item, @done_data_dir, :noop => false, :verbose => false)
  end
 
  puts @lines
  puts "Files Copied: #{@files_count}"
  puts "Data Transfered: #{to_file_size(@data_transferred)}"
  puts "done!"
end
 
 
#############
## START ##
#############
 
## Execution Start here ##
def main_program
  
  check_directories
  
  check_answer
  
  ### START MYSQL DUMP ###
  puts @lines
  puts "Starting MYSQL Dump \n"
  sleep 1
  if @all_databases
    puts "INFO: Going to dump all databases into"
    puts " '#{@data_dir}'"
    check_answer
  else
    puts "INFO: Going to dump '#{@databases.join(", ")}' databases into"
    puts " '#{@data_dir}'"
  end
  
  puts @lines
  puts "Starting MYSQL dump..."
  make_mysql_backup
  sleep 1
  
  puts @lines
  puts "Stablishing Connection to S3 account."
  stablish_connection
  
  find_or_create_bucket
  
  puts @lines
  puts "Now Going to copy Data to S3 bucket #{@bucket_name}."
  send_data
  
  puts @lines
  puts "#{@time} -- DONE"
  puts @lines
 
end
 
main_program