Skip to content

Instantly share code, notes, and snippets.

@hoff2
Created April 5, 2011 19:00
Show Gist options
  • Save hoff2/904266 to your computer and use it in GitHub Desktop.
Save hoff2/904266 to your computer and use it in GitHub Desktop.
save BLOBs to files
# Recently I was involved in rebuilding a client’s site that had previously used
# some old CMS system (in Java, source code not to be found) of our company’s,
# with our new one (in Ruby On Rails). I needed to reuse some of the original
# images. Turns out the old system kept images and various other files used on the
# site in the MySQL database, as BLOB objects. I came up with this quickie little
# script to pull the files out of the database and save then as good old fashioned
# files.
#!/usr/bin/env ruby
require 'rubygems'
require 'mysql'
require 'fileutils'
include FileUtils
mkdir ('files')
db = Mysql.new('server', 'user', 'password', 'database')
db.query("select * from table_with_blobs_in_it") do |res|
res.each_hash do |rec|
File.open("files/#{rec['filename_column']}", 'w') do |f|
f.write(rec['blob_column'])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment