I have a module that allows a user to add to a custom table in bulk via a custom form. But before they do so I'd like to backup that table to the private folder. Does anyone know if it is possible to create a new profile in the Backup and Migrate module and save a table using code in a custom module?

So what I'd like to do is programmatically create a Backup and Migrate profile in my modules .install file. Then in my custom form submit function, trigger the backup using that profile.


I've had a look at the Backup and Migrate code and I've come up with a solution, one that works for me at least.

I've created a function that gets the default profile data, then alters that to save only the tables I want. It seems to work well but if anyone can see any possible problems with this solution please do let me know.

The function:

 * @param $tables
 *  An array of table names that will be backed up.
 * @param $filename
 *  Text prepended to the date when naming the backup file.
function _MYMODULE_backup_custom_tables($tables, $filename = 'custom-backup') {
  module_load_include('module', 'backup_migrate');

  // get default profile settings
  backup_migrate_include('files', 'profiles');
  $settings = _backup_migrate_profile_saved_default_profile();

  // set the destination to our private folder
  $settings->destination_id = 'manual';
  $settings->source_id = 'db';

  // get a list of all tables from the database
  $schema = drupal_get_schema();
  $drupal_tables = array();
  foreach ($schema as $table => $table_schema) {
    $drupal_tables[$table] = $table;

  // remove from the database table list the tables we would like to save
  foreach ($tables as $table) {

  // alter default filters so only the tables we want will be backed up
  $settings->filters['destinations']['db']['nodata_tables'] = array();
  $settings->filters['destinations']['db']['exclude_tables'] = $drupal_tables;

  // change the filename so we can identify the backup in the directory
  $settings->filename = $filename;

  // backup tables

The function can then be used like this:

// the names of the tables we want to save
$tables = array(

// the text to use when naming the backup file
$filename = 'my-custom-backup';

_MYMODULE_backup_custom_tables($tables, $filename);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy