Guest User

Untitled

a guest
Jan 8th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. require 'fileutils'
  2.  
  3. start_time = Time.now
  4.  
  5.  
  6. SOURCE_DB = {
  7. :name => 'db_name',
  8. :user => 'db_user',
  9. :password => 'db_pass',
  10. :host => 'localhost'
  11. }
  12. TARGET_DB = {
  13. :name => 'db_name',
  14. :user => 'db_user',
  15. :password => 'db_pass',
  16. :host => 'foo.abcdef.us-east-1.rds.amazonaws.com'
  17. }
  18.  
  19. DUMP_DIR = '/mnt/db_dump'
  20.  
  21.  
  22. # These tables can be moved beforehand, and the diff later
  23. INSERT_ONLY_TABLES = %w[click_tracking logs]
  24.  
  25.  
  26. def benchmark(label = nil)
  27. puts label unless label.nil?
  28. before = Time.now
  29. yield
  30. after = Time.now
  31. puts "Took %.3fs" % (after - before)
  32. end
  33.  
  34. def host_user_password(db)
  35. "--host='#{db[:host]}' --user='#{db[:user]}' --password='#{db[:password]}'"
  36. end
  37.  
  38. def show_tables(db)
  39. `mysql --execute='SHOW TABLES' --silent #{host_user_password(db)} #{db[:name]}`.split("\n")
  40. end
  41.  
  42. def dump_db_tables(db, tables, dir, where = nil)
  43. FileUtils.mkdir_p(dir, :mode => 0777)
  44. where_arg = where.nil? ? '' : %(--where="#{where}")
  45.  
  46. benchmark("Dumping tables [#{tables.join(', ')}] to #{dir} #{where_arg}") do
  47. system "mysqldump --verbose #{host_user_password(db)} --tab=#{dir} #{where_arg} #{LOCK_ARG} #{db[:name]} #{tables.join(' ')}"
  48. end
  49. end
  50.  
  51. def dump_db_before(db, migrate_before = [])
  52. dir = "#{DUMP_DIR}/#{db[:name]}/before"
  53. dump_db_tables(db, migrate_before, dir)
  54. end
  55.  
  56. def dump_db_hot(db, migrate_before = [])
  57. dir = "#{DUMP_DIR}/#{db[:name]}/hot"
  58. tables = show_tables(db) - migrate_before
  59. dump_db_tables(db, tables, dir)
  60. end
  61.  
  62. def dump_db_diff(source, target, migrate_before = [])
  63. dir = "#{DUMP_DIR}/#{source[:name]}/diff"
  64.  
  65. migrate_before.each do |table|
  66. last_max_id = `mysql --execute='SELECT MAX(id) FROM #{table}' --silent #{host_user_password(target)} #{target[:name]}`.strip
  67. dump_db_tables(source, [table], dir, "id > #{last_max_id}")
  68. end
  69. end
  70.  
  71. def dump_db_structure(db)
  72. dump_file = "#{DUMP_DIR}/#{db[:name]}_structure.sql"
  73. FileUtils.mkdir_p(DUMP_DIR, :mode => 0777)
  74.  
  75. benchmark("Dumping structure of #{db[:name]} to #{dump_file}") do
  76. system "mysqldump --verbose #{host_user_password(db)} --no-data #{db[:name]} > #{dump_file}"
  77. end
  78. end
  79.  
  80. def import_db_structure(source, target)
  81. dump_file = "#{DUMP_DIR}/#{source[:name]}_structure.sql"
  82.  
  83. benchmark("Importing structure of #{source[:name]} from #{dump_file}") do
  84. system "mysql #{host_user_password(target)} #{target[:name]} < #{dump_file}"
  85. end
  86. end
  87.  
  88. def import_db_tables(target, tables, dir)
  89. benchmark("Importing tables from #{dir}") do
  90. dump_files = tables.map { |table| File.join(dir, "#{table}.txt") }
  91. system "mysqlimport --local --compress --verbose #{host_user_password(target)} #{target[:name]} #{dump_files.join(' ')}"
  92. end
  93. end
  94.  
  95. def import_db_before(source, target, migrate_before = [])
  96. dir = "#{DUMP_DIR}/#{source[:name]}/before"
  97. import_db_tables(target, migrate_before, dir)
  98. end
  99.  
  100. def import_db_hot(source, target, migrate_before = [])
  101. dir = "#{DUMP_DIR}/#{source[:name]}/hot"
  102. tables = show_tables(source) - migrate_before
  103. import_db_tables(target, tables, dir)
  104. end
  105.  
  106. def import_db_diff(source, target, migrate_before = [])
  107. dir = "#{DUMP_DIR}/#{source[:name]}/diff"
  108. import_db_tables(target, migrate_before, dir)
  109. end
  110.  
  111.  
  112. if ARGV.include?('--before')
  113. # Don't lock tables while the site is still up
  114. LOCK_ARG = "--skip-lock-tables"
  115. # Run these before
  116. dump_db_structure(SOURCE_DB)
  117. dump_db_before(SOURCE_DB, INSERT_ONLY_TABLES)
  118. import_db_structure(SOURCE_DB, TARGET_DB)
  119. import_db_before(SOURCE_DB, TARGET_DB, INSERT_ONLY_TABLES)
  120. else
  121. LOCK_ARG = ""
  122. # Run these when the site is offline
  123. dump_db_hot(SOURCE_DB, INSERT_ONLY_TABLES)
  124. dump_db_diff(SOURCE_DB, TARGET_DB, INSERT_ONLY_TABLES)
  125. import_db_hot(SOURCE_DB, TARGET_DB, INSERT_ONLY_TABLES)
  126. import_db_diff(SOURCE_DB, TARGET_DB, INSERT_ONLY_TABLES)
  127. end
  128.  
  129. puts "Script ran for: %.3fs" % (Time.now - start_time)
Add Comment
Please, Sign In to add comment