Skip to content

Instantly share code, notes, and snippets.

@aeris
Created October 14, 2018 15:42
Show Gist options
  • Save aeris/d908c3214b7038a566f657a2b9cc0f07 to your computer and use it in GitHub Desktop.
Save aeris/d908c3214b7038a566f657a2b9cc0f07 to your computer and use it in GitHub Desktop.
Migrate PrivateBin from filesystem to PostgreSQL
#!/usr/bin/env ruby
require 'awesome_print'
require 'pg'
require 'json'
con = PG.connect host: 'localhost', user: 'postgres', password: 'postgres', dbname: 'privatebin'
files = Dir['**/*.php'].sort
con.exec 'TRUNCATE TABLE paste'
pastes = files.reject { |f| f.include? '.discussion/' }
pastes.each do |paste|
id = File.basename paste, '.php'
paste = File.read paste
#paste = paste.split("\n")[1..-1].join "\n"
paste = paste.split("\n")[1]
paste = JSON.parse paste
meta = paste['meta']
postdate = meta.delete 'postdate'
expiredate = meta.delete 'expire_date'
opendiscussion = meta.delete 'opendiscussion' == true
burnafterreading = meta.delete 'burnafterreading' == true
attachment = meta.delete 'attachment'
attachmentname = meta.delete 'attachmentname'
meta = JSON.dump meta
data = paste['data']
puts "Migrate paste #{id}"
con.exec_params 'INSERT INTO paste(dataid, data, postdate, expiredate, opendiscussion, burnafterreading, meta, attachment, attachmentname) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)', [id, data, postdate, expiredate, opendiscussion, burnafterreading, meta, attachment, attachmentname]
rescue => e
ap e
end
con.exec 'TRUNCATE TABLE comment'
comments = files.select { |f| f.include? '.discussion/' }
comments.each do |comment|
id = File.basename comment, '.php'
id = id.split('.')[1]
comment = File.read comment
#comment = comment.split("\n")[1..-1].join "\n"
comment = comment.split("\n")[1]
comment = JSON.parse comment
meta = comment['meta']
pasteid = meta.delete 'pasteid'
parentid = meta.delete 'parentid'
nickname = meta.delete 'nickname'
vizhash = meta.delete 'vizhash'
postdate = meta.delete 'postdate'
meta = JSON.dump meta
data = comment['data']
puts "Migrate comment #{id}"
con.exec_params 'INSERT INTO comment(dataid, pasteid, parentid, data, nickname, vizhash, postdate) VALUES($1, $2, $3, $4, $5, $6, $7)', [id, pasteid, parentid, data, nickname, vizhash, postdate]
rescue => e
ap e
end
con.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment