backup_migrate.drush.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for backup and migrate.
  5. */
  6. /**
  7. * Implementation of hook_drush_command().
  8. */
  9. function backup_migrate_drush_command() {
  10. $items['bam-backup'] = array(
  11. 'callback' => 'backup_migrate_drush_backup',
  12. 'description' => dt('Backup the site\'s database with Backup and Migrate.'),
  13. 'aliases' => array('bb'),
  14. 'examples' => array(
  15. 'drush bam-backup' => 'Backup the default databse to the manual backup directory using the default settings.',
  16. 'drush bam-backup db scheduled mysettings' => 'Backup the database to the scheduled directory using a settings profile called "mysettings"',
  17. 'drush bam-backup files' => 'Backup the files directory to the manual directory using the default settings. The Backup and Migrate Files module is required for files backups.',
  18. ),
  19. 'arguments' => array(
  20. 'source' => "Optional. The id of the source (usually a database) to backup. Use 'drush bam-sources' to get a list of sources. Defaults to 'db'",
  21. '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'",
  22. 'profile' => "Optional. The id of a settings profile to use. Use 'drush bam-profiles' to get a list of available profiles. Defaults to 'default'",
  23. ),
  24. );
  25. $items['bam-restore'] = array(
  26. 'callback' => 'backup_migrate_drush_restore',
  27. 'description' => dt('Restore the site\'s database with Backup and Migrate.'),
  28. 'arguments' => array(
  29. 'source' => "Required. The id of the source (usually a database) to restore the backup to. Use 'drush bam-sources' to get a list of sources. Defaults to 'db'",
  30. 'destination' => "Required. The id of destination to send the backup file to. Use 'drush bam-destinations' to get a list of destinations. Defaults to 'manual'",
  31. 'backup id' => "Required. The id of a backup file restore. Use 'drush bam-backups' to get a list of available backup files.",
  32. ),
  33. 'options' => array(
  34. 'yes' => 'Skip confirmation',
  35. ),
  36. );
  37. $items['bam-destinations'] = array(
  38. 'callback' => 'backup_migrate_drush_destinations',
  39. 'description' => dt('Get a list of available destinations.'),
  40. );
  41. $items['bam-sources'] = array(
  42. 'callback' => 'backup_migrate_drush_sources',
  43. 'description' => dt('Get a list of available sources.'),
  44. );
  45. $items['bam-profiles'] = array(
  46. 'callback' => 'backup_migrate_drush_profiles',
  47. 'description' => dt('Get a list of available settings profiles.'),
  48. );
  49. $items['bam-backups'] = array(
  50. 'callback' => 'backup_migrate_drush_destination_files',
  51. 'description' => dt('Get a list of previously created backup files.'),
  52. 'arguments' => array(
  53. 'destination' => "Required. The id of destination to list backups from. Use 'drush bam-destinations' to get a list of destinations.",
  54. ),
  55. );
  56. return $items;
  57. }
  58. /**
  59. * Implementation of hook_drush_help().
  60. */
  61. function backup_migrate_drush_help($section) {
  62. switch ($section) {
  63. case 'drush:bam-backup':
  64. return dt("Backup the site's database using default settings.");
  65. case 'drush:bam-restore':
  66. return dt('Restore the site\'s database with Backup and Migrate.');
  67. case 'drush:bam-destinations':
  68. return dt('Get a list of available destinations.');
  69. case 'drush:bam-profiles':
  70. return dt('Get a list of available settings profiles.');
  71. case 'drush:bam-backups':
  72. return dt('Get a list of previously created backup files.');
  73. }
  74. }
  75. /**
  76. * Backup the default database.
  77. */
  78. function backup_migrate_drush_backup($source_id = 'db', $destination_id = 'manual', $profile_id = 'default') {
  79. backup_migrate_include('profiles', 'destinations');
  80. // Set the message mode to logging.
  81. _backup_migrate_message_callback('_backup_migrate_message_drush');
  82. if (!backup_migrate_get_destination($source_id)) {
  83. _backup_migrate_message("Could not find the source '@source'. Try using 'drush bam-sources' to get a list of available sources or use 'db' to backup the Drupal database.", array('@source' => $source_id), 'error');
  84. return;
  85. }
  86. if (!backup_migrate_get_destination($destination_id)) {
  87. _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');
  88. return;
  89. }
  90. $settings = backup_migrate_get_profile($profile_id);
  91. if(!$settings) {
  92. _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');
  93. return;
  94. }
  95. _backup_migrate_message('Starting backup...');
  96. $settings->destination_id = $destination_id;
  97. $settings->source_id = $source_id;
  98. backup_migrate_perform_backup($settings);
  99. }
  100. /**
  101. * Restore to the default database.
  102. */
  103. function backup_migrate_drush_restore($source_id = '', $destination_id = '', $file_id = '') {
  104. drush_print(dt('Restoring will delete some or all of your data and cannot be undone. ALWAYS TEST YOUR BACKUPS ON A NON-PRODUCTION SERVER!'));
  105. if (!drush_confirm(dt('Are you sure you want to restore the database?'))) {
  106. return drush_user_abort();
  107. }
  108. backup_migrate_include('profiles', 'destinations');
  109. // Set the message mode to drush output.
  110. _backup_migrate_message_callback('_backup_migrate_message_drush');
  111. if (!backup_migrate_get_destination($source_id)) {
  112. _backup_migrate_message("Could not find the source '@source'. Try using 'drush bam-sources' to get a list of available sources or use 'db' to backup the Drupal database.", array('@source' => $source_id), 'error');
  113. return;
  114. }
  115. if (!$destination = backup_migrate_get_destination($destination_id)) {
  116. _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');
  117. return;
  118. }
  119. if (!$file_id || !$file = backup_migrate_destination_get_file($destination_id, $file_id)) {
  120. _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');
  121. return;
  122. }
  123. _backup_migrate_message('Starting restore...');
  124. $settings = array('source_id' => $source_id);
  125. backup_migrate_perform_restore($destination_id, $file_id);
  126. }
  127. /**
  128. * Get a list of available destinations.
  129. */
  130. function backup_migrate_drush_destinations() {
  131. return _backup_migrate_drush_destinations('all');
  132. }
  133. /**
  134. * Get a list of available sources.
  135. */
  136. function backup_migrate_drush_sources() {
  137. return _backup_migrate_drush_destinations('source');
  138. }
  139. /**
  140. * Get a list of available destinations with the given op.
  141. */
  142. function _backup_migrate_drush_destinations($op = NULL) {
  143. backup_migrate_include('destinations');
  144. $rows = array(array(dt('ID'), dt('Name'), dt('Operations')));
  145. foreach (backup_migrate_get_destinations($op) as $destination) {
  146. $rows[] = array(
  147. $destination->get_id(),
  148. $destination->get_name(),
  149. implode (', ', $destination->ops()),
  150. );
  151. }
  152. drush_print_table($rows, TRUE, array(32, 32));
  153. }
  154. /**
  155. * Get a list of available profiles.
  156. */
  157. function backup_migrate_drush_profiles() {
  158. backup_migrate_include('profiles');
  159. $rows = array(array(dt('ID'), dt('Name')));
  160. foreach (backup_migrate_get_profiles() as $profile) {
  161. $rows[] = array(
  162. $profile->get_id(),
  163. $profile->get_name(),
  164. );
  165. }
  166. drush_print_table($rows, TRUE, array(32, 32));
  167. }
  168. /**
  169. * Get a list of files in a given destination
  170. */
  171. function backup_migrate_drush_destination_files($destination_id = NULL) {
  172. backup_migrate_include('destinations');
  173. // Set the message mode to drush output.
  174. _backup_migrate_message_callback('_backup_migrate_message_drush');
  175. if (!$destination_id) {
  176. _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');
  177. return;
  178. }
  179. if (!$destination = backup_migrate_get_destination($destination_id)) {
  180. _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');
  181. return;
  182. }
  183. $out = array(array(
  184. dt('Filename'),
  185. dt('Date'),
  186. dt('Age'),
  187. dt('Size'),
  188. ));
  189. $files = $destination->list_files();
  190. $i = 0;
  191. foreach ((array)$files as $file) {
  192. // Show only files that can be restored from.
  193. if ($file->is_recognized_type()) {
  194. $info = $file->info();
  195. $out[] = array(
  196. check_plain($info['filename']),
  197. format_date($info['filetime'], 'small'),
  198. format_interval(time() - $info['filetime'], 1),
  199. format_size($info['filesize']),
  200. );
  201. }
  202. }
  203. if (count($out) > 1) {
  204. drush_print_table($out, TRUE);
  205. }
  206. else {
  207. drush_print(dt('There are no backup files to display.'));
  208. }
  209. }
  210. /**
  211. * Send a message to the drush log.
  212. */
  213. function _backup_migrate_message_drush($message, $replace, $type) {
  214. // Use drush_log to display to the user.
  215. drush_log(strip_tags(dt($message, $replace)), str_replace('status', 'notice', $type));
  216. // Watchdog log the message as well for admins.
  217. _backup_migrate_message_log($message, $replace, $type);
  218. }