backup_migrate.drush.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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' => "Optional. 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', 'sources');
  80. // Set the message mode to logging.
  81. _backup_migrate_message_callback('_backup_migrate_message_drush');
  82. if (!backup_migrate_get_source($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. backup_migrate_include('profiles', 'destinations', 'sources');
  105. // Set the message mode to drush output.
  106. _backup_migrate_message_callback('_backup_migrate_message_drush');
  107. if (!backup_migrate_get_source($source_id)) {
  108. _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');
  109. return;
  110. }
  111. if (!$destination = backup_migrate_get_destination($destination_id)) {
  112. _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');
  113. return;
  114. }
  115. else if (!$file_id || !$file = backup_migrate_destination_get_file($destination_id, $file_id)) {
  116. _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');
  117. return;
  118. }
  119. 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!'));
  120. if (!drush_confirm(dt('Are you sure you want to perform the restore?'))) {
  121. return drush_user_abort();
  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_sources('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 destinations with the given op.
  156. */
  157. function _backup_migrate_drush_sources($op = NULL) {
  158. backup_migrate_include('sources');
  159. $rows = array(array(dt('ID'), dt('Name'), dt('Operations')));
  160. foreach (backup_migrate_get_sources($op) as $destination) {
  161. $rows[] = array(
  162. $destination->get_id(),
  163. $destination->get_name(),
  164. implode (', ', $destination->ops()),
  165. );
  166. }
  167. drush_print_table($rows, TRUE, array(32, 32));
  168. }
  169. /**
  170. * Get a list of available profiles.
  171. */
  172. function backup_migrate_drush_profiles() {
  173. backup_migrate_include('profiles');
  174. $rows = array(array(dt('ID'), dt('Name')));
  175. foreach (backup_migrate_get_profiles() as $profile) {
  176. $rows[] = array(
  177. $profile->get_id(),
  178. $profile->get_name(),
  179. );
  180. }
  181. drush_print_table($rows, TRUE, array(32, 32));
  182. }
  183. /**
  184. * Get a list of files in a given destination
  185. */
  186. function backup_migrate_drush_destination_files($destination_id = NULL) {
  187. backup_migrate_include('destinations');
  188. $destinations = array();
  189. // Set the message mode to drush output.
  190. _backup_migrate_message_callback('_backup_migrate_message_drush');
  191. if ($destination_id && !$destination = backup_migrate_get_destination($destination_id)) {
  192. _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');
  193. return;
  194. }
  195. // Single destination required.
  196. if ($destination) {
  197. $destinations = array($destination);
  198. }
  199. // List all destinations
  200. else {
  201. $destinations = backup_migrate_get_destinations('list files');
  202. }
  203. // Load all the files.
  204. $rows = $sort = array();
  205. foreach ($destinations as $destination) {
  206. $destination->file_cache_clear();
  207. $dest_files = $destination->list_files();
  208. foreach ($dest_files as $id => $file) {
  209. $info = $file->info();
  210. $rows[] = array(
  211. check_plain($info['filename']),
  212. $destination->get_id(),
  213. format_date($info['filetime'], 'small'),
  214. format_interval(time() - $info['filetime'], 1),
  215. format_size($info['filesize']),
  216. );
  217. $sort[] = $info['filetime'];
  218. }
  219. }
  220. $headers = array(array(
  221. dt('Filename'),
  222. dt('Destination'),
  223. dt('Date'),
  224. dt('Age'),
  225. dt('Size'),
  226. ));
  227. if (count($rows)) {
  228. array_multisort($sort, SORT_DESC, $rows);
  229. drush_print_table(array_merge($headers, $rows), TRUE);
  230. }
  231. else {
  232. drush_print(dt('There are no backup files to display.'));
  233. }
  234. }
  235. /**
  236. * Send a message to the drush log.
  237. */
  238. function _backup_migrate_message_drush($message, $replace, $type) {
  239. // Use drush_log to display to the user.
  240. drush_log(strip_tags(dt($message, $replace)), str_replace('status', 'notice', $type));
  241. // Watchdog log the message as well for admins.
  242. _backup_migrate_message_log($message, $replace, $type);
  243. }