'backup_migrate_drush_backup', 'description' => dt('Backup the site\'s database with Backup and Migrate.'), 'arguments' => array( 'destination' => "Optional. The id of destination to send the backup file to. Use 'drush bam destinations' to get a list of destinations. Defaults to 'manual'", 'profile' => "Optional. The id of a settings profile to use. Use 'drush bam profiles' to get a list of available profiles. Defaults to 'default'", ), ); $items['bam restore'] = array( 'callback' => 'backup_migrate_drush_restore', 'description' => dt('Restore the site\'s database with Backup and Migrate.'), 'arguments' => array( 'destination' => "Optional. The id of destination to send the backup file to. Use 'drush bam destinations' to get a list of destinations. Defaults to 'manual'", 'backup id' => "Required. The id of a backup file restore. Use 'drush bam backups' to get a list of available backup files.", ), ); $items['bam destinations'] = array( 'callback' => 'backup_migrate_drush_destinations', 'description' => dt('Get a list of available destinations.'), ); $items['bam profiles'] = array( 'callback' => 'backup_migrate_drush_profiles', 'description' => dt('Get a list of available settings profiles.'), ); $items['bam backups'] = array( 'callback' => 'backup_migrate_drush_destination_files', 'description' => dt('Get a list of previously created backup files.'), 'arguments' => array( 'destination' => "Required. The id of destination to list backups from. Use 'drush bam destinations' to get a list of destinations.", ), ); return $items; } /** * Implementation of hook_drush_help(). */ function backup_migrate_drush_help($section) { switch ($section) { case 'drush:bam backup': return dt("Backup the site's database using default settings."); case 'drush:bam restore': return dt('Restore the site\'s database with Backup and Migrate.'); case 'drush:bam destinations': return dt('Get a list of available destinations.'); case 'drush:bam profiles': return dt('Get a list of available settings profiles.'); case 'drush:bam backups': return dt('Get a list of previously created backup files.'); } } /** * Backup the default database. */ function backup_migrate_drush_backup($destination_id = 'manual', $profile_id = 'default') { backup_migrate_include('files', 'db', 'profiles', 'destinations'); // Set the message mode to logging. _backup_migrate_message_callback('_backup_migrate_message_drush'); if (!backup_migrate_get_destination($destination_id)) { _backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error'); return; } $settings = backup_migrate_get_profile($profile_id); if(!$settings) { _backup_migrate_message("Could not find the profile '@profile'. Try using 'drush bam profiles' to get a list of available profiles.", array('@profile' => $profile_id), 'error'); return; } _backup_migrate_message('Starting backup...'); $settings->destination_id = $destination_id; backup_migrate_perform_backup($settings); } /** * Restore to the default database. */ function backup_migrate_drush_restore($destination_id = 'manual', $file_id = '') { backup_migrate_include('files', 'db', 'profiles', 'destinations'); // Set the message mode to drush output. _backup_migrate_message_callback('_backup_migrate_message_drush'); if (!$destination = backup_migrate_get_destination($destination_id)) { _backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error'); return; } if (!$file = backup_migrate_destination_get_file($destination_id, $file_id)) { _backup_migrate_message("Could not find the file '@file'. Try using 'drush bam backups @destination' to get a list of available backup files in this destination destinations.", array('@destination' => $destination_id, '@file' => $file_id), 'error'); return; } _backup_migrate_message('Starting restore...'); backup_migrate_perform_restore($destination_id, $file_id); } /** * Get a list of available destinations. */ function backup_migrate_drush_destinations() { backup_migrate_include('destinations'); $rows = array(array(dt('ID'), dt('Name'), dt('Operations'))); foreach (backup_migrate_get_destinations() as $destination) { $rows[] = array( $destination->get_id(), $destination->get_name(), implode (', ', $destination->ops()), ); } drush_print_table($rows, TRUE, array(32, 25)); } /** * Get a list of available profiles. */ function backup_migrate_drush_profiles() { backup_migrate_include('profiles'); $rows = array(array(dt('ID'), dt('Name'))); foreach (backup_migrate_get_profiles() as $profile) { $rows[] = array( $profile->get_id(), $profile->get_name(), ); } drush_print_table($rows, TRUE, array(32, 25)); } /** * Get a list of files in a given destination */ function backup_migrate_drush_destination_files($destination_id = NULL) { backup_migrate_include('destinations'); // Set the message mode to drush output. _backup_migrate_message_callback('_backup_migrate_message_drush'); if (!$destination_id) { _backup_migrate_message("You must specify an existing destination. Try using 'drush bam destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error'); return; } if (!$destination = backup_migrate_get_destination($destination_id)) { _backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error'); return; } $out = array(array( dt('Filename'), dt('Date'), dt('Age'), dt('Size'), )); $files = $destination->list_files(); $i = 0; foreach ((array)$files as $file) { // Show only files that can be restored from. if ($file->is_recognized_type()) { $info = $file->info(); $out[] = array( check_plain($info['filename']), format_date($info['filetime'], 'small'), format_interval(time() - $info['filetime'], 1), format_size($info['filesize']), ); } } if (count($out) > 1) { drush_print_table($out, TRUE); } else { drush_print(dt('There are no backup files to display.')); } } /** * Send a message to the drush log. */ function _backup_migrate_message_drush($message, $replace, $type) { // Use drush_log to display to the user. drush_log(strip_tags(dt($message, $replace)), str_replace('status', 'notice', $type)); // Watchdog log the message as well for admins. _backup_migrate_message_log($message, $replace, $type); }