Winnie | 05.10.2008
Wie schon erwähnt, bin ich auf der Suche nach der perfekten Lösung, meine Googlemail-E-Mails zu sichern. Und ich denke ich habe sie gefunden: ein Ruby-Script, das die Synchronisation zwischen meinem Googlemail-Account und einem zweiten IMAP-Account übernimmt. Synchronisation ist dabei vielleicht das falsche Wort, es ist eher ein Backup, was den zweiten IMAP-Account auf dem gleichen Stand wie den Googlemail-Account hält.
Die Basis für das Script kommt von Ryan Grove. Ich habe das Löschen von Nachrichten und die Ordner-Auflistung hinzugefügt.
Mehr »
Wie schon erwähnt, bin ich auf der Suche nach der perfekten Lösung, meine Googlemail-E-Mails zu sichern. Und ich denke ich habe sie gefunden: ein Ruby-Script, das die Synchronisation zwischen meinem Googlemail-Account und einem zweiten IMAP-Account übernimmt. Synchronisation ist dabei vielleicht das falsche Wort, es ist eher ein Backup, was den zweiten IMAP-Account auf dem gleichen Stand wie den Googlemail-Account hält.
Die Basis für das Script kommt von Ryan Grove. Ich habe das Löschen von Nachrichten und die Ordner-Auflistung hinzugefügt.
#!/usr/bin/env ruby
require 'net/imap'
# Source server connection info.
SOURCE_HOST = 'imap.googlemail.com'
SOURCE_PORT = 993
SOURCE_SSL = true
SOURCE_USER = 'EMAIL@googlemail.com'
SOURCE_PASS = 'PASSWORT'
# Destination server connection info.
DEST_HOST = 'imap.SERVER.com'
DEST_PORT = 143
DEST_SSL = false
DEST_USER = 'USER'
DEST_PASS = 'PASSWORT'
# List of all Folder that should not be synced
FOLDERS_EXCLUDE = [
'[Google Mail]',
'[Google Mail]/Sent Mail',
'[Google Mail]/Spam',
'[Google Mail]/Trash'
]
# Utility methods.
def dd(message)
puts "[#{DEST_HOST}] #{message}"
end
def ds(message)
puts "[#{SOURCE_HOST}] #{message}"
end
# Connect and log into both servers.
ds 'connecting...'
source = Net::IMAP.new(SOURCE_HOST, SOURCE_PORT, SOURCE_SSL)
ds 'logging in...'
source.login(SOURCE_USER, SOURCE_PASS)
dd 'connecting...'
dest = Net::IMAP.new(DEST_HOST, DEST_PORT, DEST_SSL)
dd 'logging in...'
dest.login(DEST_USER, DEST_PASS)
# Getting the folders to sync
folders = Array.new
source.list("", "%").each do |mailbox|
if FOLDERS_EXCLUDE.include?(mailbox.name) == false
folders e
begin
dd "folder not found; creating..."
dest.create(foldername)
dest.select(foldername)
rescue => ee
dd "error: could not create folder: #{e}"
next
end
end
# Build a lookup hash of all message ids present in the destination folder.
dest_info = {}
dd 'analyzing existing messages...'
uids = dest.uid_search(['ALL'])
if uids.length > 0
dest.uid_fetch(uids, ['ENVELOPE']).each do |data|
dest_info[data.attr['ENVELOPE'].message_id] = true
end
end
# Loop through all messages in the source folder.
uids = source.uid_search(['ALL'])
if uids.length > 0
source.uid_fetch(uids, ['ENVELOPE']).each do |data|
mid = data.attr['ENVELOPE'].message_id
# If this message is already in the destination folder, skip it.
next if dest_info[mid]
# Download the full message body from the source folder.
ds "downloading message #{mid}..."
msg = source.uid_fetch(data.attr['UID'], ['RFC822', 'FLAGS',
'INTERNALDATE']).first
# Append the message to the destination folder, preserving flags and
# internal timestamp.
dd "storing message #{mid}..."
dest.append(foldername, msg.attr['RFC822'], msg.attr['FLAGS'],
msg.attr['INTERNALDATE'])
end
end
# Build a lookup hash of all message ids present in the source folder.
source_info = {}
dd 'analyzing source messages...'
uids = source.uid_search(['ALL'])
if uids.length > 0
source.uid_fetch(uids, ['ENVELOPE']).each do |data|
source_info[data.attr['ENVELOPE'].message_id] = true
end
end
# Loop through all messages in the source folder.
uids = dest.uid_search(['ALL'])
if uids.length > 0
dest.uid_fetch(uids, ['ENVELOPE']).each do |data|
mid = data.attr['ENVELOPE'].message_id
# If this message is already in the destination folder, skip it.
next if source_info[mid]
# Setting flag for deletion
ds "deleting message #{mid}..."
dest.store(data.seqno, "+FLAGS", [:Deleted])
end
end
dest.expunge
source.close
dest.close
end
puts 'done'
Kategorien: Blog | 12 Kommentare