destinations.s3.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @file
  4. * Functions to handle the s3 backup destination.
  5. */
  6. /**
  7. * A destination for sending database backups to an s3 server.
  8. *
  9. * @ingroup backup_migrate_destinations
  10. */
  11. class backup_migrate_destination_s3 extends backup_migrate_destination_remote {
  12. var $supported_ops = array('scheduled backup', 'manual backup', 'restore', 'list files', 'configure', 'delete');
  13. var $s3 = NULL;
  14. var $cache_files = TRUE;
  15. /**
  16. * Save to to the s3 destination.
  17. */
  18. function _save_file($file, $settings) {
  19. if ($s3 = $this->s3_object()) {
  20. $path = $file->filename();
  21. if ($s3->putObject($s3->inputFile($file->filepath(), FALSE), $this->get_bucket(), $this->remote_path($file->filename()), S3::ACL_PRIVATE)) {
  22. return $file;
  23. }
  24. }
  25. return FALSE;
  26. }
  27. /**
  28. * Load from the s3 destination.
  29. */
  30. function load_file($file_id) {
  31. backup_migrate_include('files');
  32. $file = new backup_file(array('filename' => $file_id));
  33. if ($s3 = $this->s3_object()) {
  34. $data = $s3->getObject($this->get_bucket(), $this->remote_path($file_id), $file->filepath());
  35. if (!$data->error) {
  36. return $file;
  37. }
  38. }
  39. return NULL;
  40. }
  41. /**
  42. * Delete from the s3 destination.
  43. */
  44. function _delete_file($file_id) {
  45. if ($s3 = $this->s3_object()) {
  46. $s3->deleteObject($this->get_bucket(), $this->remote_path($file_id));
  47. }
  48. }
  49. /**
  50. * List all files from the s3 destination.
  51. */
  52. function _list_files() {
  53. backup_migrate_include('files');
  54. $files = array();
  55. if ($s3 = $this->s3_object()) {
  56. $s3_files = $s3->getBucket($this->get_bucket(), $this->get_subdir());
  57. foreach ((array)$s3_files as $id => $file) {
  58. $info = array(
  59. 'filename' => $this->local_path($file['name']),
  60. 'filesize' => $file['size'],
  61. 'filetime' => $file['time'],
  62. );
  63. $files[$info['filename']] = new backup_file($info);
  64. }
  65. }
  66. return $files;
  67. }
  68. /**
  69. * Get the form for the settings for this filter.
  70. */
  71. function edit_form() {
  72. // Check for the library.
  73. $this->s3_object();
  74. $form = parent::edit_form();
  75. $form['scheme']['#type'] = 'value';
  76. $form['scheme']['#value'] = 'https';
  77. $form['host']['#default_value'] = 's3.amazonaws.com';
  78. $form['path']['#title'] = 'S3 Bucket';
  79. $form['path']['#default_value'] = $this->get_bucket();
  80. $form['path']['#description'] = 'This bucket must already exist. It will not be created for you.';
  81. $form['user']['#title'] = 'Access Key ID';
  82. $form['pass']['#title'] = 'Secret Access Key';
  83. $form['subdir'] = array(
  84. '#type' => 'textfield',
  85. '#title' => t('Subdirectory'),
  86. '#default_value' => $this->get_subdir(),
  87. '#weight' => 25
  88. );
  89. $form['settings']['#weight'] = 50;
  90. return $form;
  91. }
  92. /**
  93. * Submit the form for the settings for the s3 destination.
  94. */
  95. function edit_form_submit($form, &$form_state) {
  96. // Append the subdir onto the path.
  97. if (!empty($form_state['values']['subdir'])) {
  98. $form_state['values']['path'] .= '/'. trim($form_state['values']['subdir'], '/');
  99. }
  100. parent::edit_form_submit($form, $form_state);
  101. }
  102. /**
  103. * Generate a filepath with the correct prefix.
  104. */
  105. function remote_path($path) {
  106. if ($subdir = $this->get_subdir()) {
  107. $path = $subdir .'/'. $path;
  108. }
  109. return $path;
  110. }
  111. /**
  112. * Generate a filepath with the correct prefix.
  113. */
  114. function local_path($path) {
  115. if ($subdir = $this->get_subdir()) {
  116. $path = str_replace($subdir .'/', '', $path);
  117. }
  118. return $path;
  119. }
  120. /**
  121. * Get the bucket which is the first part of the path.
  122. */
  123. function get_bucket() {
  124. $parts = explode('/', @$this->dest_url['path']);
  125. return $parts[0];
  126. }
  127. /**
  128. * Get the bucket which is the first part of the path.
  129. */
  130. function get_subdir() {
  131. // Support the older style of subdir saving.
  132. if ($subdir = $this->settings('subdir')) {
  133. return $subdir;
  134. }
  135. $parts = explode('/', @$this->dest_url['path']);
  136. array_shift($parts);
  137. return implode('/', array_filter($parts));
  138. }
  139. function s3_object() {
  140. // Try to use libraries module if available to find the path.
  141. if (function_exists('libraries_get_path')) {
  142. $library_paths[] = libraries_get_path('s3-php5-curl');
  143. }
  144. else {
  145. $library_paths[] = 'sites/all/libraries/s3-php5-curl';
  146. }
  147. $library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes/s3-php5-curl';
  148. $library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes';
  149. foreach($library_paths as $path) {
  150. if (file_exists($path . '/S3.php')) {
  151. require_once $path . '/S3.php';
  152. if (!$this->s3 && !empty($this->dest_url['user'])) {
  153. $this->s3 = new S3($this->dest_url['user'], $this->dest_url['pass'], FALSE, $this->dest_url['host']);
  154. }
  155. return $this->s3;
  156. }
  157. }
  158. drupal_set_message(t('To back up to S3 you must download version 0.4 of the PHP S3 library from !link. Extract the S3.php file and add it the directory %dir.',
  159. array('!link' => l('http://undesigned.org.za/2007/10/22/amazon-s3-php-class', 'http://undesigned.org.za/2007/10/22/amazon-s3-php-class'), '%dir' => 'sites/all/libraries/s3-php5-curl')), 'error', FALSE);
  160. return NULL;
  161. }
  162. }