2010-05-01

Downloading your e-mail on a server to process

You know all the great features of craigslist and Facebook, that use anonymous, standalone e-mail for forwarding request to an ad owner or replying to a message via e-mail.

Well, I will show you how to do something like these.

Scenario: An chronological script will download new e-mails, then a Python script will process on these e-mails.

For downloading e-mails, I used getmail4. The best tutorial for it can be found here.

For my case, /home/manchurian/.getmail/getmailrc looks like this:

[retriever]
type = SimplePOP3Retriever
server = [my_mail_server]
username = [my_mail]
password = [my_mail_password]

[destination]
type = Mboxrd
path = /home/manchurian/inbox

Make the mail downloading automatic using crontab -e:

* * * * * getmail -n
-n is for retrieving new mails only.

Now it is time to process the downloaded mails anyway you want:

import mailbox
mails = mailbox.mbox("/home/manchurian/inbox")

mails.lock()

try:
   for m in mailbox:
      if m['from'] == 'abc@abc.org':
         print m['subject']
finally:
   mails.unlock()

Cheers,

Backing up MySQL database

Losing data on your business application's database would be a real disaster. Here's a way simple script to back up your MySQL database.

Scenario

We have a remote Debian server with the database, and an Ubuntu server (Intel Xeon) in our office right beneath my table. :)

We wanted to have our database backed up daily. So here we go:

1-st step. Save SQL script of the database in the remote server.


#!/bin/bash
export d=$(date +'%Y-%m-%d')
mkdir -p /root/backup/$d
mysqldump --add-drop-table --allow-keywords -q -a -c -u root -p[password_for_mysql_root] [databasename] > /root/backup/$d/[databasename].sql

Save it as /root/mysqlbackup.sh

2-nd step. Automize it with crontab -e to run daily at 05:30.

30 05 * * * /root/mysqlbackup.sh

3-rd step. Fetch the SQL from the local server and save.

#!/bin/bash
export d=$(date +'%Y-%m-%d')
mkdir -p /root/backup/$d
pscp -sftp -l root -pw [password_for_remote_server_root] root@[server_ip]:/root/backup/$d/[databasename].sql /root/backup/$d
exit 0