destinations.s3.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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', 'remote 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']['#type'] = 'value';
  78. $form['host']['#value'] = 's3.amazonaws.com';
  79. $form['path']['#title'] = 'S3 Bucket';
  80. $form['path']['#default_value'] = $this->get_bucket();
  81. $form['path']['#description'] = 'This bucket must already exist. It will not be created for you.';
  82. $form['user']['#title'] = 'Access Key ID';
  83. $form['pass']['#title'] = 'Secret Access Key';
  84. $form['subdir'] = array(
  85. '#type' => 'textfield',
  86. '#title' => t('Subdirectory'),
  87. '#default_value' => $this->get_subdir(),
  88. '#weight' => 25
  89. );
  90. $form['settings']['#weight'] = 50;
  91. return $form;
  92. }
  93. /**
  94. * Submit the form for the settings for the s3 destination.
  95. */
  96. function edit_form_submit($form, &$form_state) {
  97. // Append the subdir onto the path.
  98. if (!empty($form_state['values']['subdir'])) {
  99. $form_state['values']['path'] .= '/'. trim($form_state['values']['subdir'], '/');
  100. }
  101. parent::edit_form_submit($form, $form_state);
  102. }
  103. /**
  104. * Generate a filepath with the correct prefix.
  105. */
  106. function remote_path($path) {
  107. if ($subdir = $this->get_subdir()) {
  108. $path = $subdir .'/'. $path;
  109. }
  110. return $path;
  111. }
  112. /**
  113. * Generate a filepath with the correct prefix.
  114. */
  115. function local_path($path) {
  116. if ($subdir = $this->get_subdir()) {
  117. $path = str_replace($subdir .'/', '', $path);
  118. }
  119. return $path;
  120. }
  121. /**
  122. * Get the bucket which is the first part of the path.
  123. */
  124. function get_bucket() {
  125. $parts = explode('/', @$this->dest_url['path']);
  126. return $parts[0];
  127. }
  128. /**
  129. * Get the bucket which is the first part of the path.
  130. */
  131. function get_subdir() {
  132. // Support the older style of subdir saving.
  133. if ($subdir = $this->settings('subdir')) {
  134. return $subdir;
  135. }
  136. $parts = explode('/', @$this->dest_url['path']);
  137. array_shift($parts);
  138. return implode('/', array_filter($parts));
  139. }
  140. function s3_object() {
  141. // Try to use libraries module if available to find the path.
  142. if (function_exists('libraries_get_path')) {
  143. $library_paths[] = libraries_get_path('s3-php5-curl');
  144. }
  145. else {
  146. $library_paths[] = 'sites/all/libraries/s3-php5-curl';
  147. }
  148. $library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes/s3-php5-curl';
  149. $library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes';
  150. foreach($library_paths as $path) {
  151. if (file_exists($path . '/S3.php')) {
  152. require_once $path . '/S3.php';
  153. if (!$this->s3 && !empty($this->dest_url['user'])) {
  154. $this->s3 = new S3($this->dest_url['user'], $this->dest_url['pass']);
  155. }
  156. return $this->s3;
  157. }
  158. }
  159. 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);
  160. return NULL;
  161. }
  162. }