Skip to content

Instantly share code, notes, and snippets.

@resistorsoftware
Forked from NZKoz/aa_instructions.md
Created May 2, 2012 04:14
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 resistorsoftware/2573586 to your computer and use it in GitHub Desktop.
Save resistorsoftware/2573586 to your computer and use it in GitHub Desktop.
Back Up All Your Gmail
  • Install getmail (aptitude install getmail4)
  • Set Up Your Imap Server (tl;dr)
  • getmail
  • ruby date_based_archive.rb ~/Maildir/.Archive
require 'pathname'
require 'fileutils'
require 'rubygems'
require 'mail'
class Message
attr_reader :filename
def initialize(filename)
@filename = filename
@mail = Mail.read(filename)
end
def archive_name
@mail.date.strftime("%Y-%m")
rescue
p @mail
# Everything with an invalid date came from this month for me, ymmv
"2004-07"
end
end
class MessageSource
attr_reader :dir
def initialize(dir = Pathname.new('.'))
@dir = dir
@maildirs = {}
end
def each_message
%w(new cur).each do |sub|
Dir[@dir + "#{sub}/*"].each do |fn|
yield Message.new(fn)
end
end
end
def maildir(sub_folder_name)
@maildirs[sub_folder_name] ||= Maildir.new(self, sub_folder_name)
end
def destination_for(message)
maildir(message.archive_name)
end
end
class Maildir
def initialize(message_source, archivename)
@directory = "#{message_source.dir}.#{archivename}"
if !File.directory?(@directory)
FileUtils.mkdir(@directory)
%w(cur new tmp).each {|d| FileUtils.mkdir(@directory + d) }
end
end
def accept_message(message)
FileUtils.mv(message.filename, @directory + 'new')
end
end
archive = MessageSource.new(Pathname.new(ARGV[0]))
$stdout.sync=true
archive.each_message do |msg|
maildir = archive.destination_for(msg)
maildir.accept_message(msg)
print '.'
end
[retriever]
type = SimpleIMAPSSLRetriever
server = imap.gmail.com
mailboxes = ("[Gmail]/All Mail",)
username = EMAIL
password = PASSWORD
[destination]
type = Maildir
path = ~/Maildir/.Archive/
[options]
#verbose = 2
message_log = ~/.getmail/log
read_all = false
# do not alter messages
delivered_to = false
received = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment