February 2003 archive

Backup Script in Bash/Python

It’s been a while since I’ve written anything on ye olde technical weblog, but I thought that a code snippet I wrote a couple days ago might be generally useful. Have you ever had to do simple, incremental backups? I had to write a simple utility to backup a MySQL database server to a network drive. I wanted to do a backup every night, but also keep the daily backups for the past 7 days. Additionally, I thought it’d be nice to keep a copy for every month so I can go way back in time, if necessary.

My mind was in a Python frame today, and so I wrote the shell script partially in Python… in fact, shell variables are substituted into Python code, which is quite interesting:

#!/bin/sh

datestr=`date +%Y-%m-%d`
filename='mysql_database'
netwerk='/netwerk'
directory='${netwerk}/BlahBlah Backup'

mount "${netwerk}"

/usr/bin/mysqldump -u backup_user --databases mysql db1 db2 \
        > "${directory}/${filename}-${datestr}.sql"

python <<END_PYTHON
import os
import re
logfiles = [x for x in os.listdir("${directory}"} \
        if re.search("${filename}-[0-9]{4}-[0-9]{2}-[0-9]{2}", x)]
# don't delete logfiles on the first of the month
deletablelogfiles = [x for x in logfiles \
        if not re.search("${filename}-[0-9]{4}-[0-9]{2}-01", x)]
deletablelogfiles.sort()
deletablelogfiles.reverse()
while len(deletablelogfiles) > 7:
    file = deletablelogfiles.pop()
    print "Deleting old log file %s...\n" % (file,)
    os.remove("${directory}/%s" % (file,))
END_PYTHON

umount "${netwerk}"

There’s always something intrinsically cool about code writing code. This script seamlessly integrates shell scripting and Python. Shell scripting is useful for controlling and accessing systems. Python allows a more clear implementation of relatively complex operations.