Skip to content

Instantly share code, notes, and snippets.

@gerhard
Forked from NZKoz/aa_instructions.md
Created May 23, 2012 10:38
Show Gist options
  • Save gerhard/2774505 to your computer and use it in GitHub Desktop.
Save gerhard/2774505 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