Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Sunday, January 13, 2013

Linux Server Disk Space Email Notification

I ran into a situation the other day where a virtual server I have hosted with limited disk space just running a couple of MySQL databases and a web server crashed (so to speak) because it ran out of disk space. It's not a very important machine that gets little use so it wasn't a big issue but it did prompt me to write a very basic script to simply email me the report on available disk space. This way if the disk space gets low then I can go to the server and clear some of the old data out to free up space before it causes the machine to stop functioning.

I'm using python because it's quick and easy, but similar scripts could be done in just about any language from bash scripts to PHP or c++. Although it seems pointless to use c++ for something so simple.
(If anyone would like to see this in some other language then let me know in the comments and I'll see what I can do)

Here is a screenshot of the python script. Click here to access the code and copy & paste. (Please excuse the advert, it will continue to the site after a few seconds.)
(I am aware that this is simple and lacking things such as error checking. I am also aware that this might not be the best way to achieve this task, but it's similar to other scripts I've done in the past so it was easy just to stick with it.)


You can run this script on the server in a terminal window to confirm it works and then it's a simple case of configure a cron job to send it to you on a specified timeframe such as daily or weekly. The actual setup of the cron job is up to you as it mostly involves you choosing when to run the script.

Also, don't forget to change the to and from email addresses in the script before you run it. It's up to you to add error checking and anything else you might require.

An example of functionality you might like to add to it is the parsing of the disk space report and having the script only email you if the available space reaches a specified low value.


I hope someone else finds this simple script of use. Please feel free to contribute any additions, fixes or comments in the comments section below.


Thursday, March 8, 2012

Mysql backup using Python

It's been a long while since I posted so I am back with a relatively simple post today and will try to keep the posts coming.

This post will show a simple python script that will go through the databases available to a MySQL user. It will check each database for a configuration table which tells the script if it is to be backed up and who to notify when the backup is complete.

This has been built and tested using a Linux system but shouldn't take much to convert to other platforms as well.

An idea to extend this script could be to add in the ability to transfer the backup to an ftp location. This is a very simple solution that is really only useful if you have a number of MySQL databases that need to be backed up separately and even have different people notified when a backup is complete. It could easily be modified to just backup all the databases and then when done send an email to yourself to let you know it's been done. You could even add in checking to see if any of the backups failed and notify of failure.

It should be pretty easy to extend upon this script to suit your needs.

Here it is :
#!/usr/bin/env python
import os
import time
import MySQLdb
import sys

SENDMAIL = "/usr/sbin/sendmail" # sendmail location

username = "backupuser"
password = "backuppass"
hostname = "localhost"

filestamp = time.strftime('%d-%m-%Y')

# Get a list of databases on the mysql server
database_list_command="mysql -u %s -p%s -h %s --silent -N -e 'show databases'" % (username, password, hostname)
for database in os.popen(database_list_command).readlines():
    database = database.strip()
   
    # Check if the auto backup module is enabled and get the notification data
    notify_db = MySQLdb.connect( user=username,passwd=password,db=database)
    c = notify_db.cursor()
   
    try:
        c.execute("SELECT `notify_email`,`enabled` FROM `backup_config` WHERE 1 LIMIT 1")
        result = c.fetchone()
        notify_to = result[0]
        enabled = result[1]
    except MySQLdb.Error, e:
        continue
   
    if enabled == 1:
        filename = "/backups/mysql/%s-%s.sql" % (database, filestamp)
        os.popen("mysqldump -u %s -p%s -h %s -e --opt -c %s | gzip -c > %s.gz" % (username, password, hostname, database, filename))
       
        FROM = "backup@mydomainhere.com.au"
       
        if notify_to == "":
            notify_to = "backup@mydomainhere.com.au"
           
        TO = [notify_to]    # using a list to make it easier to support multiple addresses
        SUBJECT = "Backup Complete Notification"
        TEXT = "The latest database backup has been complete."

        # Prepare actual message
        message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

        # Send the mail
        p = os.popen("%s -t -i" % SENDMAIL, "w")
        p.write(message)
        status = p.close()
        if status:
            print "Sendmail exit status", status