Skip to content

Instantly share code, notes, and snippets.

@rdamen
Created September 16, 2014 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdamen/71c9e3b6a14484ae2236 to your computer and use it in GitHub Desktop.
Save rdamen/71c9e3b6a14484ae2236 to your computer and use it in GitHub Desktop.
require 'pp'
require 'fileutils'
require 'securerandom'
STORE="/srv/store/mail"
ARCHIVE="/root/archive"
domains = Dir.glob(File.join(STORE, '*/'))
users = []
folders = []
emls = []
msgs = []
domains.each do |d|
Dir.glob(File.join(d, '*/')).each do |u|
users << u unless File.basename(u) == "#public"
end
end
users.each do |u|
Dir.glob(File.join(u, '*/status.fld')).each do |f|
type = File.open(f).first
dir = File.expand_path('..', f)
next unless type == "T0\r\n"
next unless File.basename(dir) != "Drafts"
folders << dir
end
end
folders.each do |f|
emls.concat Dir.glob(File.join(f, "**/*eml"))
end
Dir.glob(emls).each do |fname|
msgs << {
:fname => fname,
:mtime => File.mtime(fname)
}
end
msgs = msgs.sort_by do |msg|
msg[:mtime]
end
total = msgs.count
i = 1
last_month_dir = ""
msgs.each_with_index do |msg, j|
month_dir = msg[:mtime].strftime('%Y-%b')
if last_month_dir != month_dir
last_month_dir = month_dir
i = 1
else
i = i + 1
end
dst = File.join(ARCHIVE, month_dir)
if !Dir.exists? dst
FileUtils.mkdir_p(File.join(dst, "#msgs"))
FileUtils.touch(File.join(dst, 'status.fld'))
end
dst = File.join(dst, "#msgs", "%08x.eml" % i)
puts "#{j}/#{total}: #{dst}"
FileUtils.cp msg[:fname], dst
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment