remove_backup_emails.rb 1.92 KB
#!/bin/ruby

require 'fileutils'
require 'find'

$gitlab_tag = Find.find('.').grep(/_gitlab_backup.tar/)[0].match(/[0-9]+/)
def decompress
  system 'mkdir -p gitlab'
  system 'mkdir -p noosfero'
  system 'mkdir -p mailman'
  system "tar -xpf #{$gitlab_tag}_gitlab_backup.tar -C gitlab"
  system 'tar -xzf noosfero_backup.tar.gz -C noosfero'
  system 'tar -xzf mailman_backup.tar.gz -C mailman'
end

def recompress
  system "pushd $PWD && cd gitlab && tar -cpf #{$gitlab_tag}_gitlab_backup.tar * &&  mv #{$gitlab_tag}_gitlab_backup.tar ../ && popd && rm -rf gitlab"
  system 'pushd $PWD && cd noosfero && tar -czpf noosfero_backup.tar.gz * &&  mv noosfero_backup.tar.gz ../ && popd && rm -rf noosfero'
  system 'pushd $PWD && cd mailman && tar -czpf mailman_backup.tar.gz * &&  mv mailman_backup.tar.gz ../ && popd && rm -rf mailman'
end

$email_hash = Hash.new
$n=1

def create_hashes_from_file file
  feed_fd = File.open(file)
  feed_fd.each_line do |line|
    emails = line.scan(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/)
    emails.each do |email|
      $email_hash[email] = "email#{$n}@example.com" unless $email_hash.has_key?(email)
      $n+=1
    end
  end
  feed_fd.close
end

def substitute_hashes_in_file file
  tmp_file = 'tmp_file'
  File.open(file) { |source_file|
    contents = source_file.read
    $email_hash.each do |key,value|
      contents.gsub!(key, value)
    end
    File.open(tmp_file, "w+") { |f| f.write(contents) }
  } 
FileUtils.mv(tmp_file, file)
end

decompress

noosfero_sql = File.basename(Find.find('./noosfero/tmp/').grep(/\.sql/)[0])
noosfero_sql = Find.find('./noosfero/tmp/').grep(/\.sql/)[0]
deEmailedFiles = ['colab.dump', 'gitlab/db/database.sql', noosfero_sql]

deEmailedFiles.each do |deEmailedFile|
  create_hashes_from_file(deEmailedFile)
end

deEmailedFiles += Find.find('./mailman/lists/').grep(/config\.pck/)

deEmailedFiles.each do |deEmailedFile|
  substitute_hashes_in_file(deEmailedFile)
end
recompress