backup_migrate.drush.inc 12 KB

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