Tuesday, August 09, 2005

libgmail adjustments and database backup

So today I want to share my little adjustments to libgmail, Python's binding to access gmail. libgmail comes with lots of excellent little programs. Most notably is gcp which copies a file from the local filesystem to gmail. Hence it's the ideal system to perfrom for instance nightly backups of a database to gmail.

Unfortunately gcp asks you every time for the password/login so it can't really run as a cron job -- at leat not that easy. That's why I chnaged some lines and dubbed the new program gecp:

-----------------------------------------------------------------------------------
#!/usr/bin/python2.3
#
# gcp.py -- Demo to copy a file to Gmail using libgmail
#
# $Revision: 1.1 $ ($Date: 2004/10/17 13:34:04 $)
#
# Author: follower@myrealbox.com
#
# License: GPL 2.0
#
import os
import sys
import logging

# Allow us to run using installed `libgmail` or the one in parent directory.
try:
import libgmail
logging.warn("Note: Using currently installed `libgmail` version.")
except ImportError:
# Urghhh...
sys.path.insert(1,
os.path.realpath(os.path.join(os.path.dirname(__file__),
os.path.pardir)))
import libgmail


if __name__ == "__main__":
import sys
from getpass import getpass

# TODO: Allow copy from account.

try:
filename = sys.argv[1]
destination = sys.argv[2]
pw = sys.argv[3]
except IndexError:
print "Usage: %s :[

---------------------------------------------------------------------------------

You can use gecp <filename> <gmail-account>[:label] <password> to copy a file to your gamil account. Be careful: GMail limits the filesize to 10MB so you have to split longer files accordingly. (To reassemble them again juts use cat file1 file2 file3 > targetfile;-)

I am using the following little script in a cronjob to backup the content of a MySQL database:

---------------------------------------------------------------------------------
#!/bin/bash

cd ~german/backup/
#remove all files
rm -f ~german/backup/survey-*
rm -f ~german/backup/xsurvey-*

#dump database
date=`date -I`
mysqldump --opt -u admin --password="MyDBPassword" MyDatabaseName | bzip2 -c > ~german/backup/survey-$date.sql.bz2

#split
split -b10m survey-$date.sql.bz2 xsurvey-$date

#copy to gmail
for file in xsurvey-*;do python ~/libgmail/demos/gecp.py $file MyGMailAccount:Backup MyPassword; done

-------------------------------------------------------------------------------------

I hope this is of some use for somebody;-)

0 Comments:

Post a Comment

<< Home