4

I'm trying to create a custom module that will overwrite my testing site with a copy of my live site (I can't use SSH and I can't install my own PEAR modules, so Drush is a no-go).

I've been using PHP execute() commands to manipulate the file system and dump the database, but I'm getting errors on the testing site when importing the SQL dump, and I usually have to resort instead to the Backup and Migrate module, using a profile that copies my main database over to my testing database.

Now I'm wondering, is there any way I can hook into this and execute it as part of the chain of commands in my custom module? The goal is to turn the process of migrating to the testing site into a one-click (followed by a lot of grinding) process. But since Backup and Migrate doesn't apparently expose any API or hooks, or even the machine names of its profiles and destinations, how could I execute a backup command with a specific preset from a different module?

Say, for instance, I want a PHP command that will tell Backup and Migrate to backup database live_db to destination test_db, both of which are already recorded as mysql://[db_name] in the B&M settings.

1 Answer 1

2

Ok, as it happens, I figured this one out. Below is a sample function meant to be used as an admin form submission handler.

function _my_backup_handler() {
  $destination_hash = 'yourhash'; // See below.

  backup_migrate_include('profiles');
  $settings = _backup_migrate_profile_saved_default_profile();
  $settings->destination_id = $destination_hash;
  $settings->source_id = 'db'; // Your primary Drupal database. This is probably redundant.
  backup_migrate_perform_backup($settings);
}

As for what $destination_hash is, it turns out the "machine name" of a custom BM destination is a random hash. It can be found in the URL if you go to http://www.yoursite.com/admin/config/system/backup_migrate/destination and click the "edit" button for your destination; you'll end up at http://www.yoursite.com/admin/config/system/backup_migrate/destination/list/edit/[hash]. Just copy [hash] and plug it in as a string for the value of $settings->destination_id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.