remove_backup_emails.rb
1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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