Guest User

Untitled

a guest
Mar 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'optparse'
  3.  
  4. def get_user_param(prompt_text, default = '')
  5.  
  6. prompt = prompt_text
  7. prompt += default.length > 0 ? ' [' + default + ']' : ''
  8.  
  9. puts prompt
  10.  
  11. param = gets.chomp
  12. return param.length > 0 ? param : default
  13. end
  14.  
  15. options = {}
  16.  
  17. # configure command-line arguments
  18. optparse = OptionParser.new do|opts|
  19.  
  20. opts.banner = "import.rb will import a directory full of MySQL SQL dumps into a database\n\n"
  21. opts.banner += "Usage: import.rb [options]"
  22.  
  23. options[:verbose] = false
  24. opts.on( '-v', '--verbose', 'Output more information' ) do
  25. options[:verbose] = true
  26. end
  27.  
  28. options[:host] = nil
  29. opts.on('-h', '--host HOST', 'import to MySQL HOST') do |host|
  30. options[:host] = host
  31. end
  32.  
  33. options[:user] = nil
  34. opts.on('-u', '--user USER', 'import using MySQL USER') do |user|
  35. options[:user] = user
  36. end
  37.  
  38. options[:password] = nil
  39. opts.on('-p', '--password PASSWORD', 'import using MySQL PASSWORD') do |password|
  40. options[:password] = password
  41. end
  42.  
  43. options[:database] = nil
  44. opts.on('-d', '--database DATABASE', 'import to MySQL DATABASE') do |database|
  45. options[:database] = database
  46. end
  47.  
  48. options[:sleep] = nil
  49. opts.on('-s', '--sleep SECONDS', 'sleep for SECONDS between table imports') do |sleep|
  50. options[:sleep] = sleep
  51. end
  52.  
  53. opts.on('-H', '--help', 'Display this screen' ) do
  54. puts opts
  55. exit
  56. end
  57. end
  58.  
  59.  
  60. # parse out command-line arguments
  61. begin
  62. optparse.parse!
  63. rescue OptionParser::InvalidOption, OptionParser::MissingArgument
  64. puts 'ERROR: ' + $!.to_s + "\n\n"
  65. puts optparse
  66. exit
  67. end
  68.  
  69. # if options not specified via command-line, ask for them
  70. if options[:host] == nil:
  71. host = get_user_param('Host?', 'localhost')
  72. else
  73. host = options[:host]
  74. end
  75.  
  76. if options[:user] == nil:
  77. user = get_user_param('User?')
  78. else
  79. user = options[:user]
  80. end
  81.  
  82. if options[:password] == nil:
  83. password = get_user_param('Password?')
  84. else
  85. password = options[:password]
  86. end
  87.  
  88. if options[:database] == nil:
  89. database = get_user_param('Database?')
  90. else
  91. database = options[:database]
  92. end
  93.  
  94. # do import
  95. mysql_import_command = 'mysql -u ' + user + ' -p' + password + ' -D ' + database + ' -h ' + host + ' < '
  96.  
  97. puts "\nImporting...\n"
  98.  
  99. for file in Dir.entries('.')
  100.  
  101. if file != '.' && file != '..'
  102. import_command = mysql_import_command + file
  103. system(import_command)
  104.  
  105. if options[:verbose] == true:
  106. print file + "\n"
  107. else
  108. print '.'
  109. end
  110.  
  111. if options[:sleep] != nil:
  112. sleep options[:sleep].to_i
  113. end
  114. end
  115. end
Add Comment
Please, Sign In to add comment