Saturday, March 8, 2008

libgmail. Python binding for Google's Gmail service


I´ve spent some time thinking how to backup the data I´m currently storing in my linux server. First of all, I faced the problem of coding the script to backup and then configuring the cron services to execute it daily. When all was working I had to decide where to put those backup files, because leaving them in the server hard disk wasn´t a solution. So I bought an USB hard disk and from time to time I move the backup files there. I was more or less happy with the solution. But some months ago I talk to a friend and co-worker who was starting to use a gmail account as a backup storage. I thought that was a great idea so I started to find a python library that helps me to send all my backups to my auxiliary gmail account, and I found it, libgmail .

libgmail does everything you might want to do with gmail, and it has a nice method to send files. It creates a draft email and attaches the file you want to store in the gmail account. It comes with a nice amount of examples and I´ve tested the storeFile that works great.

Here is the python script that I use to upload the backup files. It´s almost an example provided by libgmail, with little modifications.

import sys
import libgmail

def main(argv):
accountName = 'yourAccountName'
accountPassw = 'yourPassword'

gmailAccount = libgmail.GmailAccount(accountName, accountPassw)
except libgmail.GmailLoginFailure:
print "\nLogin failed. (Wrong username/password?)"
print "Log in successful.\n"

if gmailAccount.storeFile(argv[1], label='serverBackup'):
print 'File stored successfully'
print 'Could not store'

if __name__ == '__main__':

No comments:

Post a Comment