Guest User

Untitled

a guest
Apr 21st, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #! /usr/bin/env ruby
  2. #
  3. # Dump a MySQL database and upload to S3, splitting the dump
  4. # if needed as S3 has a 5GB file size limit.
  5. #
  6. # Simply run "./dump_to_s3.rb database_name" to dump and
  7. # upload to S3 all in one go.
  8. #
  9.  
  10. require "rubygems"
  11. require "aws/s3"
  12.  
  13. # Amazon S3 configuration
  14. AWS::S3::Base.establish_connection!(
  15. :access_key_id => 'YOUR_ACCESS_KEY',
  16. :secret_access_key => 'YOUR_SECRET_KEY'
  17. )
  18.  
  19. # MySQL configuration
  20. $mysql_user = "root"
  21. $mysql_pass = ""
  22.  
  23. # Local temporary folder to store MySQL dumps in
  24. $local = "/mnt"
  25.  
  26. # Bucket (and folder) to store backups in on S3
  27. $remote = "MY_BUCKET/FOLDER"
  28.  
  29. # Split the dump files at 2GB
  30. $split_size = 2*1000*1000*1000
  31.  
  32. class DbStore < AWS::S3::S3Object
  33. set_current_bucket_to $remote.to_s
  34. end
  35.  
  36. if !ARGV[0].nil?
  37. time = Time.new
  38. year = time.year.to_s.rjust(4, '0')
  39. month = time.month.to_s.rjust(2, '0')
  40. day = time.day.to_s.rjust(2, '0')
  41. hour = time.hour.to_s.rjust(2, '0')
  42. min = time.min.to_s.rjust(2, '0')
  43. sec = time.sec.to_s.rjust(2, '0')
  44. file = "#{ARGV[0]}-#{year}-#{month}-#{day}_#{hour}-#{min}-#{sec}.sql"
  45. puts "dumping #{ARGV[0]} database..."
  46. `mysqldump -u#{$mysql_user} -p#{$mysql_pass} #{ARGV[0]} | split -b #{$split_size} - #{$local}/#{file}_`
  47. files = Dir.glob("#{$local}/#{file}_*")
  48. split_count = files.size
  49. files.each do |partial|
  50. target = (split_count > 1) ? File.basename(partial) : file
  51. puts "uploading #{target} to S3."
  52. if File.exist?(partial)
  53. DbStore.store(target, open(partial))
  54. File.delete(partial)
  55. end
  56. end
  57. end
Add Comment
Please, Sign In to add comment