Skip to content

Instantly share code, notes, and snippets.

@kreeger
Last active December 17, 2015 08:08
Show Gist options
  • Select an option

  • Save kreeger/5577654 to your computer and use it in GitHub Desktop.

Select an option

Save kreeger/5577654 to your computer and use it in GitHub Desktop.
MySQL import for split files!
#!/usr/bin/env ruby
# MySQL import for split files!
#
# Usage:
# mysql-split-import <USERNAME> <PASSWORD> <DATABASE> <BACKUP_DIR>
# mysql-split-import a_user their_password some_database /path/to/backup
username = ARGV[0]
password = ARGV[1]
db_name = ARGV[2]
directory = ARGV[3]
exit unless username && password && db_name && directory
directory = File.absolute_path(directory)
Dir.entries(directory).select { |f| f =~ /\.sql$/ }.each do |file|
sql_file = File.join(directory, file)
txt_file = sql_file.gsub(/\.sql$/, '.txt')
puts "Processing #{file}."
`mysql -u #{username} -p#{password} #{db_name} < #{sql_file}`
if File.exists?(txt_file)
`mysqlimport -u #{username} -p#{password} #{db_name} #{txt_file}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment