destinations.s3.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. public $supported_ops = array('scheduled backup', 'manual backup', 'remote backup', 'restore', 'list files', 'configure', 'delete');
  13. public $s3 = NULL;
  14. public $cache_files = TRUE;
  15. /**
  16. * Save to to the s3 destination.
  17. */
  18. public 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. public function load_file($file_id) {
  31. require_once dirname(__FILE__) . '/files.inc';
  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. public 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. public function _list_files() {
  53. require_once dirname(__FILE__) . '/files.inc';
  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. public 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'] = @$this->dest_url['host'] ? $this->dest_url['host'] : '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. public 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. public 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. public 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. public 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. public 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. /**
  140. *
  141. */
  142. public function s3_object() {
  143. // Try to use libraries module if available to find the path.
  144. if (function_exists('libraries_get_path')) {
  145. $library_paths[] = libraries_get_path('s3-php5-curl');
  146. }
  147. else {
  148. $library_paths[] = 'sites/all/libraries/s3-php5-curl';
  149. }
  150. $library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes/s3-php5-curl';
  151. $library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes';
  152. foreach ($library_paths as $path) {
  153. if (file_exists($path . '/S3.php')) {
  154. require_once $path . '/S3.php';
  155. if (!$this->s3 && !empty($this->dest_url['user'])) {
  156. // The hostname can be overridden.
  157. $host = 's3.amazonaws.com';
  158. if (isset($this->dest_url['host'])) {
  159. $host = $this->dest_url['host'];
  160. }
  161. $this->s3 = new S3($this->dest_url['user'], $this->dest_url['pass'], FALSE, $host);
  162. }
  163. return $this->s3;
  164. }
  165. }
  166. drupal_set_message(t('Due to drupal.org code hosting policies, the S3 library needed to use an S3 destination is no longer distributed with this module. You must download the library from !link and place it in one of these locations: %locations.', array('%locations' => implode(', ', $library_paths), '!link' => l('http://undesigned.org.za/2007/10/22/amazon-s3-php-class', 'http://undesigned.org.za/2007/10/22/amazon-s3-php-class'))), 'error', FALSE);
  167. return NULL;
  168. }
  169. }