def remote_db_dump(directory): """ Uses Drush and the Backup and Migrate module to make a SQL dump of the database for the spcified site. Returns the full filepath to the backup Usage: fab -H [email protected] remote_db_dump:'/site/path/' """ with cd(directory): o = run("drush bam-backup") r = neo._cleanup_drush_output(o) # Get the backup filename from the response r = re.match( 'Default Database backed up successfully to ([^\s]*) ' 'in destination Manual Backups Directory in ' '[\d]*\.[\d]* ms.', r) bam_filename = r.group(1) + ".mysql.gz" # Get the path to the Drupal files directory, silently with neo._mute(): drupal_files_path = run("drush dd files") bam_path = drupal_files_path + "/backup_migrate/manual/" with cd(bam_path): bam_filepath = env.cwd + bam_filename return bam_filepath
def remote_db_dump(directory): """ Uses Drush and the Backup and Migrate module to make a SQL dump of the database for the spcified site. Returns the full filepath to the backup Usage: fab -H [email protected] remote_db_dump:'/site/path/' """ with cd(directory): o = run("drush bam-backup") r = neo._cleanup_drush_output(o) # Get the backup filename from the response r = re.match('Default Database backed up successfully to ([^\s]*) ' 'in destination Manual Backups Directory in ' '[\d]*\.[\d]* ms.', r) bam_filename = r.group(1) + ".mysql.gz" # Get the path to the Drupal files directory, silently with neo._mute(): drupal_files_path = run("drush dd files") bam_path = drupal_files_path + "/backup_migrate/manual/" with cd(bam_path): bam_filepath = env.cwd + bam_filename return bam_filepath
def mysql_cnf_password_set(): """Check to see if a password is set in my.cnf """ with neo._mute(): if mysql_cnf_exists(): if run("grep 'password.*=' < ~/.my.cnf"): return True else: return False
def get_mysql_pass(): """Find out if we have the right information to use MySQL""" # There is a password file and it has a password in it. with neo._mute(): if mysql_cnf_exists() and mysql_cnf_password_set(): password = run('awk < ~/.my.cnf -F"=" \'{ print $2 }\'') return password
def local_db_import(local_db_name, local_sql_file, local_db_host='localhost', local_db_user=None, local_db_pass=None): """ Import a SQL file into a local database """ with neo._mute(): db_import_cmd = "mysql -h %s -u %s" % (local_db_host, local_db_user) if local_db_pass is not None: db_import_cmd = db_import_cmd + " -p %s" % local_db_pass db_import_cmd = db_import_cmd + " %s < %s" % (local_db_name, local_sql_file) local(db_import_cmd)
def pull_db(directory, local_db_host='localhost', local_db_user=None, local_db_pass=None): """ Get a copy of a remote website's db on your local machine. Usage: fab -H [email protected] pull_db:'/path/to/drupal/site/root' """ txt = "Getting a database backup of your remote site." print print neo._header(txt) print path = remote_db_dump(directory) localpath = get(path, "/tmp") gz_file = localpath[0] local("gunzip %s" % gz_file) sql_file = re.sub('\.gz$', '', gz_file) txt = "Importing the database on your local machine." print print neo._header(txt) if local_db_user is None: with neo._mute(): local_db_user = local('whoami', True) print "Writing to your local database using these credentials:" print " hostname: %s" % local_db_host print " username: %s" % local_db_user if local_db_pass is None: print " password: defined in ~/.my.conf" else: print " password: ***" # Create and import db db_name = interactive_create_db('local', local_db_host, local_db_user, local_db_pass) print print "Importing the database..." local_db_import(db_name, sql_file, local_db_host, local_db_user, local_db_pass) print print "...done. Your database, %s is ready to go." % (db_name) print print "Cleaning up." local("rm -f sql_file")
def db_create(place, db_name, db_host, db_user, db_pass): """ Try to create a database. We'll be conservative here and just let the task fail if a database already exists with the same name. """ create = 'mysqladmin create %s -h %s -u %s' % (db_name, db_host, db_user) if db_pass is not None: create = create + " -p %s" % db_pass if place == 'remote': pass #o = run(create) if place == 'local': with neo._mute(): db_response = local(create, True) return db_response
def mysql_cnf_exists(): with neo._mute(): if exists('~/.my.cnf'): return True else: return False