#!/usr/bin/env python """[options] [] Backs up a Drupal database to bzipped file""" __all__ = ['drupalbackup'] def drupalbackup(database, directory, backupuser, backuppass, hostname, verbosity=1): """verbosity: 0=quiet, 1=normal (default), 2=verbose, 3=debug Backs up a Drupal database to bzipped file""" if verbosity>=2: print 'drupalbackup: ', database, directory, backupuser, backuppass, hostname, verbosity; print import os import datetime import MySQLdb #First get a list of all the tables in the database db=MySQLdb.connect(host=hostname, user=backupuser, passwd=backuppass, db=database) cursor= db.cursor() cursor.execute("SHOW TABLES IN " + database) tablerows=cursor.fetchall() tables=[] for tablerow in tablerows : #convert from a list of rows to a simple list tables.append(tablerow[0]) cursor.close() db.close() if verbosity>=3: print 'all tables: ', tables; print #Next make a list of all the tables that start with "cache" cachetables=[] for table in tables : if table.startswith('cache') : cachetables.append(table) if verbosity>=2: print 'cache tables: ', cachetables; print ndtablist = [ #These are all the other tables whose data does not need to be backed up 'sessions', 'search_dataset', 'search_index', 'search_keywords_log', 'search_total', 'watchdog', 'accesslog', 'devel_queries', 'devel_times' ] ndtables = (set(tables) & set(ndtablist)) | set(cachetables) #Set of "no data" tables is constructed if verbosity>=2: print 'all non-data tables: ', ndtables; print nodatatables = ' '.join(ndtables) #make a string for the first run of mysqldump - backing up the no-data tables ignoreoption = ' --ignore-table=' + database + '.' ignore = ignoreoption + ignoreoption.join(ndtables) #make a string for the second run of mysqldump - backing up all the other tables backupfile=directory + '/' + database + '_' + str(datetime.date.today()) + '.sql' if verbosity>=1: print "Backing up drupal database: " + database + " into file: " + backupfile mysqldump="mysqldump --compress --add-drop-table --extended-insert -u " + backupuser + " -p" + backuppass status=os.system(mysqldump + " --no-data " + database + " " + nodatatables + " > " + backupfile) if verbosity>=2: print 'non-data status=', status status=os.system(mysqldump + ignore + " " + database + " >> " + backupfile) if verbosity>=2: print 'with-data status=', status; print 'bzipping ' + backupfile status=os.system("nice bzip2 -9 -f " + backupfile) if verbosity>=2: print 'bzip status=', status; print 'done creating file: ' + backupfile + '.bz2' def main(): #This function is a friendly command-line wrapper around the drupalbackup() function above from optparse import OptionParser import sys usage = 'usage: %prog ' + __doc__ parser = OptionParser(usage) parser.add_option("-q", "--quiet", action="store_true", dest="quiet", help="zero verbosity") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="print details of progress") parser.add_option("-d", "--debug", action="store_true", dest="debug", help="print detailed status (very verbose)") parser.add_option("-s", "--host", default="localhost", dest="host", help="host Server [default: %default]") (options, args) = parser.parse_args() if len(args) == 3: args.append(raw_input('Please enter the password for the user <' + args[2] + '> :')) elif len(args) != 4: parser.error("incorrect number of arguments") args.append(options.host) if options.quiet: args.append(0) elif options.verbose: args.append(2) elif options.debug: args.append(3) drupalbackup(*args) if __name__ == "__main__": main()