<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Threepress Consulting blog &#187; mysql</title>
	<atom:link href="http://blog.threepress.org/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.threepress.org</link>
	<description>Threepress creates software for publishers, educators and authors.</description>
	<lastBuildDate>Fri, 03 Sep 2010 23:28:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Django/MySQL database backup script</title>
		<link>http://blog.threepress.org/2008/07/17/djangomysql-database-backup-script/</link>
		<comments>http://blog.threepress.org/2008/07/17/djangomysql-database-backup-script/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 23:38:25 +0000</pubDate>
		<dc:creator>Liza Daly</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://blog.threepress.org/?p=20</guid>
		<description><![CDATA[I&#8217;m sure everyone has one of these lying around, but here&#8217;s mine in case you are as lazy a programmer as I usually am.  The only thing Django-specific about it is that it reads your database configuration directly out of your settings file.
If you run this via cron it will automatically create its backup directory [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure everyone has one of these lying around, but here&#8217;s mine in case you are as lazy a programmer as I usually am.  The only thing Django-specific about it is that it reads your database configuration directly out of your settings file.</p>
<p>If you run this via cron it will automatically create its backup directory and a zipped SQL dump with the name of the current day of the week.  If one already exists, it simply overwrites it.  This means you&#8217;ll have a week&#8217;s worth of daily rolling backups.  If you need more granularity, add a timestamp to the datestamp.  If you need more persistent backups, switch to a full date scheme.</p>
<p>The latest version of this can always been found <a href="http://code.google.com/p/threepress/source/browse/trunk/bookworm/database-backup.py">here</a>.<br />
<span id="more-20"></span></p>
<pre class="brush: plain;">
#!/usr/bin/env python

import sys
import os.path
import os
import logging
from datetime import datetime
from settings import DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD

logging.basicConfig(level=logging.WARN)

BACKUP_DIR = &quot;%s/backups&quot; % os.path.dirname(__file__)
MYSQL_CMD = 'mysqldump'
ZIP_CMD = 'zip'

def _setup():
   if not os.path.exists(BACKUP_DIR):
       logging.debug(&quot;Created backup directory %s&quot; % BACKUP_DIR)
       os.mkdir(BACKUP_DIR)
   else:
       logging.debug(&quot;Using backup directory %s&quot; % BACKUP_DIR)

def _backup_name():
   now = datetime.now()
   day_name = now.strftime(&quot;%A&quot;)
   file_name = &quot;%s.sql&quot; % day_name.lower()
   logging.debug(&quot;Setting backup name for day name %s as %s&quot; % (day_name, file_name))
   return file_name

def _run_backup(file_name):
   cmd = &quot;%(mysqldump)s -u %(user)s --password=%(password)s %(database)s &gt; %(log_dir)s/%(file)s&quot; % {
       'mysqldump' : MYSQL_CMD,
       'user' : DATABASE_USER,
       'password' : DATABASE_PASSWORD,
       'database' : DATABASE_NAME,
       'log_dir' : BACKUP_DIR,
       'file': file_name}
   logging.debug(&quot;Backing up with command %s &quot; % cmd)
   return os.system(cmd)

def _zip_backup(file_name):
   backup = &quot;%s/%s&quot; % (BACKUP_DIR, file_name)
   zipfile_name = &quot;%s.zip&quot; % (backup)

   if os.path.exists(zipfile_name):
       logging.debug(&quot;Removing previous zip archive %s&quot; % zipfile_name)
       os.remove(zipfile_name)
   zip_cmds = {'zip' : ZIP_CMD, 'zipfile' : zipfile_name, 'file' : backup }

   # Create the backup
   logging.debug(&quot;Making backup as %s &quot; % zipfile_name)
   os.system(&quot;%(zip)s -q -9 %(zipfile)s %(file)s&quot; % zip_cmds)

   # Test our archive
   logging.debug(&quot;Testing zip archive&quot;)
   if not os.system(&quot;%(zip)s -T -D -q %(zipfile)s&quot; % zip_cmds):
       # If there was no problem, then delete the unzipped version
       os.remove(backup)
       return True
   else:
       return False

def main(*args):
   _setup()
   file_name = _backup_name()
   _run_backup(file_name)
   return(_zip_backup(file_name))

if __name__ == '__main__':
   sys.exit(main(*sys.argv))    
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.threepress.org/2008/07/17/djangomysql-database-backup-script/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
