Commit a9629926e6f991ba71fa395f683823f22a3e4d60

Authored by Athos
1 parent a7978199

add script to substitute emails from backups retrieved from prod

Showing 1 changed file with 61 additions and 0 deletions   Show diff stats
utils/remove_backup_emails.rb 0 → 100755
@@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
  1 +#!/bin/ruby
  2 +
  3 +require 'fileutils'
  4 +require 'find'
  5 +
  6 +$gitlab_tag = Find.find('.').grep(/_gitlab_backup.tar/)[0].match(/[0-9]+/)
  7 +def decompress
  8 + system 'mkdir -p gitlab'
  9 + system 'mkdir -p noosfero'
  10 + system 'mkdir -p mailman'
  11 + system "tar -xpf #{$gitlab_tag}_gitlab_backup.tar -C gitlab"
  12 + system 'tar -xzf noosfero_backup.tar.gz -C noosfero'
  13 + system 'tar -xzf mailman_backup.tar.gz -C mailman'
  14 +end
  15 +
  16 +def recompress
  17 + system "pushd $PWD && cd gitlab && tar -cpf #{$gitlab_tag}_gitlab_backup.tar * && mv #{$gitlab_tag}_gitlab_backup.tar ../ && popd && rm -rf gitlab"
  18 + system 'pushd $PWD && cd noosfero && tar -czpf noosfero_backup.tar.gz * && mv noosfero_backup.tar.gz ../ && popd && rm -rf noosfero'
  19 + system 'pushd $PWD && cd mailman && tar -czpf mailman_backup.tar.gz * && mv mailman_backup.tar.gz ../ && popd && rm -rf mailman'
  20 +end
  21 +
  22 +$email_hash = Hash.new
  23 +$n=1
  24 +
  25 +def create_hashes_from_file file
  26 + feed_fd = File.open(file)
  27 + feed_fd.each_line do |line|
  28 + emails = line.scan(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/)
  29 + emails.each do |email|
  30 + $email_hash[email] = "email#{$n}@example.com" unless $email_hash.has_key?(email)
  31 + $n+=1
  32 + end
  33 + end
  34 + feed_fd.close
  35 +end
  36 +
  37 +def substitute_hashes_in_file file
  38 + tmp_file = 'tmp_file'
  39 + File.open(file) { |source_file|
  40 + contents = source_file.read
  41 + $email_hash.each do |key,value|
  42 + contents.gsub!(key, value)
  43 + end
  44 + File.open(tmp_file, "w+") { |f| f.write(contents) }
  45 + }
  46 +FileUtils.mv(tmp_file, file)
  47 +end
  48 +
  49 +decompress
  50 +
  51 +noosfero_sql = File.basename(Find.find('./noosfero/tmp/').grep(/\.sql/)[0])
  52 +noosfero_sql = Find.find('./noosfero/tmp/').grep(/\.sql/)[0]
  53 +deEmailedFiles = ['colab.dump', 'gitlab/db/database.sql', noosfero_sql]
  54 +deEmailedFiles += Find.find('./mailman/lists/').grep(/config\.pck/)
  55 +
  56 +deEmailedFiles.each do |deEmailedFile|
  57 + create_hashes_from_file(deEmailedFile)
  58 + substitute_hashes_in_file(deEmailedFile)
  59 +end
  60 +recompress
  61 +