backup_migrate.drush.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for backup and migrate.
  5. */
  6. /**
  7. * Implements 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 database 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.',
  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. $items['bam-schedule'] = array(
  57. 'callback' => 'backup_migrate_drush_schedule',
  58. 'description' => dt('Backup using a specific schedule.'),
  59. 'arguments' => array(
  60. 'schedule_id' => dt('The ID of the schedule to run.'),
  61. ),
  62. );
  63. $items['bam-schedules'] = array(
  64. 'callback' => 'backup_migrate_drush_schedules',
  65. 'description' => dt('Get a list of available schedules.'),
  66. );
  67. return $items;
  68. }
  69. /**
  70. * Implements hook_drush_help().
  71. */
  72. function backup_migrate_drush_help($section) {
  73. switch ($section) {
  74. case 'drush:bam-backup':
  75. return dt("Backup the site's database using default settings.");
  76. case 'drush:bam-restore':
  77. return dt('Restore the site\'s database with Backup and Migrate.');
  78. case 'drush:bam-destinations':
  79. return dt('Get a list of available destinations.');
  80. case 'drush:bam-profiles':
  81. return dt('Get a list of available settings profiles.');
  82. case 'drush:bam-backups':
  83. return dt('Get a list of previously created backup files.');
  84. }
  85. }
  86. /**
  87. * Backup the default database.
  88. */
  89. function backup_migrate_drush_backup($source_id = 'db', $destination_id = 'manual', $profile_id = 'default') {
  90. require_once dirname(__FILE__) . '/destinations.inc';
  91. require_once dirname(__FILE__) . '/profiles.inc';
  92. require_once dirname(__FILE__) . '/sources.inc';
  93. // Set the message mode to logging.
  94. _backup_migrate_message_callback('_backup_migrate_message_drush');
  95. if (!backup_migrate_get_source($source_id)) {
  96. _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');
  97. return;
  98. }
  99. if (!backup_migrate_get_destination($destination_id)) {
  100. _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');
  101. return;
  102. }
  103. $settings = backup_migrate_get_profile($profile_id);
  104. if (!$settings) {
  105. _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');
  106. return;
  107. }
  108. _backup_migrate_message('Starting backup...');
  109. $settings->destination_id = $destination_id;
  110. $settings->source_id = $source_id;
  111. backup_migrate_perform_backup($settings);
  112. }
  113. /**
  114. * Backup using schedule.
  115. */
  116. function backup_migrate_drush_schedule($schedule_id = '') {
  117. require_once dirname(__FILE__) . '/schedules.inc';
  118. // Set the message mode to drush output.
  119. _backup_migrate_message_callback('_backup_migrate_message_drush');
  120. if (!($schedule = backup_migrate_get_schedule($schedule_id))) {
  121. _backup_migrate_message("Could not find the schedule '@schedule'. Try using 'drush bam-schedules' to get a list of available schedules.", array('@schedule' => $schedule_id), 'error');
  122. return;
  123. }
  124. if (!$schedule->enabled) {
  125. _backup_migrate_message("Nothing to do, the schedule '@schedule' is disabled.", array('@schedule' => $schedule_id), 'warning');
  126. return;
  127. }
  128. _backup_migrate_message("Starting schedule '$schedule_id'...");
  129. backup_migrate_schedule_run($schedule_id);
  130. }
  131. /**
  132. * Get a list of available destinations.
  133. */
  134. function backup_migrate_drush_schedules() {
  135. require_once dirname(__FILE__) . '/schedules.inc';
  136. $rows = array(array(dt('ID'), dt('Name')));
  137. foreach (backup_migrate_get_schedules() as $schedule) {
  138. $rows[] = array(
  139. $schedule->get_id(),
  140. $schedule->get_name(),
  141. );
  142. }
  143. drush_print_table($rows, TRUE, array(32, 32));
  144. }
  145. /**
  146. * Restore to the default database.
  147. */
  148. function backup_migrate_drush_restore($source_id = '', $destination_id = '', $file_id = '') {
  149. require_once dirname(__FILE__) . '/destinations.inc';
  150. require_once dirname(__FILE__) . '/profiles.inc';
  151. require_once dirname(__FILE__) . '/sources.inc';
  152. // Set the message mode to drush output.
  153. _backup_migrate_message_callback('_backup_migrate_message_drush');
  154. if (!backup_migrate_get_source($source_id)) {
  155. _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');
  156. return;
  157. }
  158. if (!$destination = backup_migrate_get_destination($destination_id)) {
  159. _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');
  160. return;
  161. }
  162. elseif (!$file_id || !$file = backup_migrate_destination_get_file($destination_id, $file_id)) {
  163. _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');
  164. return;
  165. }
  166. 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!'));
  167. if (!drush_confirm(dt('Are you sure you want to perform the restore?'))) {
  168. return drush_user_abort();
  169. }
  170. _backup_migrate_message('Starting restore...');
  171. $settings = array('source_id' => $source_id);
  172. backup_migrate_perform_restore($destination_id, $file_id, $settings);
  173. }
  174. /**
  175. * Get a list of available destinations.
  176. */
  177. function backup_migrate_drush_destinations() {
  178. return _backup_migrate_drush_destinations('all');
  179. }
  180. /**
  181. * Get a list of available sources.
  182. */
  183. function backup_migrate_drush_sources() {
  184. return _backup_migrate_drush_sources('source');
  185. }
  186. /**
  187. * Get a list of available destinations with the given op.
  188. */
  189. function _backup_migrate_drush_destinations($op = NULL) {
  190. require_once dirname(__FILE__) . '/destinations.inc';
  191. $rows = array(array(dt('ID'), dt('Name'), dt('Operations')));
  192. foreach (backup_migrate_get_destinations($op) as $destination) {
  193. $rows[] = array(
  194. $destination->get_id(),
  195. $destination->get_name(),
  196. implode(', ', $destination->ops()),
  197. );
  198. }
  199. drush_print_table($rows, TRUE, array(32, 32));
  200. }
  201. /**
  202. * Get a list of available destinations with the given op.
  203. */
  204. function _backup_migrate_drush_sources($op = NULL) {
  205. require_once dirname(__FILE__) . '/sources.inc';
  206. $rows = array(array(dt('ID'), dt('Name'), dt('Operations')));
  207. foreach (backup_migrate_get_sources($op) as $destination) {
  208. $rows[] = array(
  209. $destination->get_id(),
  210. $destination->get_name(),
  211. implode(', ', $destination->ops()),
  212. );
  213. }
  214. drush_print_table($rows, TRUE, array(32, 32));
  215. }
  216. /**
  217. * Get a list of available profiles.
  218. */
  219. function backup_migrate_drush_profiles() {
  220. require_once dirname(__FILE__) . '/profiles.inc';
  221. $rows = array(array(dt('ID'), dt('Name')));
  222. foreach (backup_migrate_get_profiles() as $profile) {
  223. $rows[] = array(
  224. $profile->get_id(),
  225. $profile->get_name(),
  226. );
  227. }
  228. drush_print_table($rows, TRUE, array(32, 32));
  229. }
  230. /**
  231. * Get a list of files in a given destination.
  232. */
  233. function backup_migrate_drush_destination_files($destination_id = NULL) {
  234. require_once dirname(__FILE__) . '/destinations.inc';
  235. $destinations = array();
  236. // Set the message mode to drush output.
  237. _backup_migrate_message_callback('_backup_migrate_message_drush');
  238. if ($destination_id && !$destination = backup_migrate_get_destination($destination_id)) {
  239. _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');
  240. return;
  241. }
  242. // Single destination required.
  243. if ($destination) {
  244. $destinations = array($destination);
  245. }
  246. // List all destinations.
  247. else {
  248. $destinations = backup_migrate_get_destinations('list files');
  249. }
  250. // Load all the files.
  251. $rows = $sort = array();
  252. foreach ($destinations as $destination) {
  253. $destination->file_cache_clear();
  254. $dest_files = $destination->list_files();
  255. foreach ($dest_files as $id => $file) {
  256. $info = $file->info();
  257. $rows[] = array(
  258. check_plain($info['filename']),
  259. $destination->get_id(),
  260. format_date($info['filetime'], 'small'),
  261. format_interval(time() - $info['filetime'], 1),
  262. format_size($info['filesize']),
  263. );
  264. $sort[] = $info['filetime'];
  265. }
  266. }
  267. $headers = array(
  268. array(
  269. dt('Filename'),
  270. dt('Destination'),
  271. dt('Date'),
  272. dt('Age'),
  273. dt('Size'),
  274. ),
  275. );
  276. if (count($rows)) {
  277. array_multisort($sort, SORT_DESC, $rows);
  278. drush_print_table(array_merge($headers, $rows), TRUE);
  279. }
  280. else {
  281. drush_print(dt('There are no backup files to display.'));
  282. }
  283. }
  284. /**
  285. * Send a message to the drush log.
  286. */
  287. function _backup_migrate_message_drush($message, $replace, $type) {
  288. // If this is an error use drush_set_error to notify the end user and set the
  289. // exit status.
  290. if ($type == 'error') {
  291. drush_set_error(strip_tags(dt($message, $replace)));
  292. }
  293. else {
  294. // Use drush_log to display to the user.
  295. drush_log(strip_tags(dt($message, $replace)), str_replace('status', 'notice', $type));
  296. }
  297. // Watchdog log the message as well for admins.
  298. _backup_migrate_message_log($message, $replace, $type);
  299. }