- Install getmail (aptitude install getmail4)
- Set Up Your Imap Server (tl;dr)
- getmail
ruby date_based_archive.rb ~/Maildir/.Archive
Created
May 2, 2012 03:07
-
-
Save NZKoz/2573309 to your computer and use it in GitHub Desktop.
Back Up All Your Gmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
Argh, I just dig deeper into the above – OfflineIMAP will only mirror the Gmail account (as per this tweet), not do any date-based shuffling of emails. But you could adjust your date_based_archive.rb
script accordingly. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative:
apt-get install offlineimap
offlineimap
whenever you feel like syncing (it syncs both ways, so you can read/reply offline and then sync)