From a9629926e6f991ba71fa395f683823f22a3e4d60 Mon Sep 17 00:00:00 2001 From: Athos Ribeiro Date: Wed, 26 Aug 2015 13:15:41 -0300 Subject: [PATCH] add script to substitute emails from backups retrieved from prod --- utils/remove_backup_emails.rb | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+), 0 deletions(-) create mode 100755 utils/remove_backup_emails.rb diff --git a/utils/remove_backup_emails.rb b/utils/remove_backup_emails.rb new file mode 100755 index 0000000..fd473cf --- /dev/null +++ b/utils/remove_backup_emails.rb @@ -0,0 +1,61 @@ +#!/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 += Find.find('./mailman/lists/').grep(/config\.pck/) + +deEmailedFiles.each do |deEmailedFile| + create_hashes_from_file(deEmailedFile) + substitute_hashes_in_file(deEmailedFile) +end +recompress + -- libgit2 0.21.2