Created
February 12, 2013 16:50
-
-
Save makevoid/4771283 to your computer and use it in GitHub Desktop.
Script to replace sensible database datas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # obfuscates the db removing sensible values | |
| # ruby obfuscate.rb | |
| # mysqldump -u root gbc > gbc_safe.sql | |
| # tar cvfz gbc_safe.tgz gbc_safe.sql | |
| require 'bundler/setup' | |
| Bundler.require :default | |
| class Obfuscate | |
| def initialize(db_name, columns) | |
| db_name, @columns = db_name, columns.split(" ") | |
| @db = Sequel.connect "mysql://root@localhost/#{db_name}" | |
| end | |
| def run | |
| @columns.each do |column| | |
| table, column = column.split(".") | |
| table = table.to_sym | |
| dataset = @db[table] | |
| schema = @db.schema(table) | |
| col_info = schema.find{ |col, infos| col == column.to_sym } | |
| col_info = { name: col_info[0], infos: col_info[1] } | |
| @db[table].each do |row| | |
| # p row[col_info[:name]] | |
| value = case col_info[:infos][:type] | |
| when :string | |
| "#{col_info[:name]}_#{row[:id]}" | |
| when :decimal | |
| 12345 | |
| end | |
| elem = @db[table].where id: row[:id] | |
| elem.update col_info[:name] => value | |
| # p row[col_info[:name]] | |
| end | |
| end | |
| end | |
| end | |
| task = Obfuscate.new "db_name", "tableA.column1 tableA.column2 tableB.column1 ..." | |
| task.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment