Snapshot.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Exception\NotFoundException;
  4. use Elastica\Exception\ResponseException;
  5. use Elasticsearch\Endpoints\Snapshot\Restore;
  6. /**
  7. * Class Snapshot.
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html
  10. */
  11. class Snapshot
  12. {
  13. /**
  14. * @var Client
  15. */
  16. protected $_client;
  17. /**
  18. * @param Client $client
  19. */
  20. public function __construct(Client $client)
  21. {
  22. $this->_client = $client;
  23. }
  24. /**
  25. * Register a snapshot repository.
  26. *
  27. * @param string $name the name of the repository
  28. * @param string $type the repository type ("fs" for file system)
  29. * @param array $settings Additional repository settings. If type "fs" is used, the "location" setting must be provided.
  30. *
  31. * @return Response
  32. */
  33. public function registerRepository($name, $type, $settings = [])
  34. {
  35. $data = [
  36. 'type' => $type,
  37. 'settings' => $settings,
  38. ];
  39. return $this->request($name, Request::PUT, $data);
  40. }
  41. /**
  42. * Retrieve a repository record by name.
  43. *
  44. * @param string $name the name of the desired repository
  45. *
  46. * @throws Exception\ResponseException
  47. * @throws Exception\NotFoundException
  48. *
  49. * @return array
  50. */
  51. public function getRepository($name)
  52. {
  53. try {
  54. $response = $this->request($name);
  55. } catch (ResponseException $e) {
  56. if (404 == $e->getResponse()->getStatus()) {
  57. throw new NotFoundException("Repository '".$name."' does not exist.");
  58. }
  59. throw $e;
  60. }
  61. $data = $response->getData();
  62. return $data[$name];
  63. }
  64. /**
  65. * Retrieve all repository records.
  66. *
  67. * @return array
  68. */
  69. public function getAllRepositories()
  70. {
  71. return $this->request('_all')->getData();
  72. }
  73. /**
  74. * Create a new snapshot.
  75. *
  76. * @param string $repository the name of the repository in which this snapshot should be stored
  77. * @param string $name the name of this snapshot
  78. * @param array $options optional settings for this snapshot
  79. * @param bool $waitForCompletion if true, the request will not return until the snapshot operation is complete
  80. *
  81. * @return Response
  82. */
  83. public function createSnapshot($repository, $name, $options = [], $waitForCompletion = false)
  84. {
  85. return $this->request($repository.'/'.$name, Request::PUT, $options, ['wait_for_completion' => $waitForCompletion]);
  86. }
  87. /**
  88. * Retrieve data regarding a specific snapshot.
  89. *
  90. * @param string $repository the name of the repository from which to retrieve the snapshot
  91. * @param string $name the name of the desired snapshot
  92. *
  93. * @throws Exception\ResponseException
  94. * @throws Exception\NotFoundException
  95. *
  96. * @return array
  97. */
  98. public function getSnapshot($repository, $name)
  99. {
  100. try {
  101. $response = $this->request($repository.'/'.$name);
  102. } catch (ResponseException $e) {
  103. if (404 == $e->getResponse()->getStatus()) {
  104. throw new NotFoundException("Snapshot '".$name."' does not exist in repository '".$repository."'.");
  105. }
  106. throw $e;
  107. }
  108. $data = $response->getData();
  109. return $data['snapshots'][0];
  110. }
  111. /**
  112. * Retrieve data regarding all snapshots in the given repository.
  113. *
  114. * @param string $repository the repository name
  115. *
  116. * @return array
  117. */
  118. public function getAllSnapshots($repository)
  119. {
  120. return $this->request($repository.'/_all')->getData();
  121. }
  122. /**
  123. * Delete a snapshot.
  124. *
  125. * @param string $repository the repository in which the snapshot resides
  126. * @param string $name the name of the snapshot to be deleted
  127. *
  128. * @return Response
  129. */
  130. public function deleteSnapshot($repository, $name)
  131. {
  132. return $this->request($repository.'/'.$name, Request::DELETE);
  133. }
  134. /**
  135. * Restore a snapshot.
  136. *
  137. * @param string $repository the name of the repository
  138. * @param string $name the name of the snapshot
  139. * @param array $options options for the restore operation
  140. * @param bool $waitForCompletion if true, the request will not return until the restore operation is complete
  141. *
  142. * @return Response
  143. */
  144. public function restoreSnapshot($repository, $name, $options = [], $waitForCompletion = false)
  145. {
  146. $endpoint = new Restore();
  147. $endpoint->setRepository($repository);
  148. $endpoint->setSnapshot($name);
  149. $endpoint->setBody($options);
  150. $endpoint->setParams(['wait_for_completion' => $waitForCompletion]);
  151. return $this->_client->requestEndpoint($endpoint);
  152. }
  153. /**
  154. * Perform a snapshot request.
  155. *
  156. * @param string $path the URL
  157. * @param string $method the HTTP method
  158. * @param array $data request body data
  159. * @param array $query query string parameters
  160. *
  161. * @return Response
  162. */
  163. public function request($path, $method = Request::GET, $data = [], array $query = [])
  164. {
  165. return $this->_client->request('_snapshot/'.$path, $method, $data, $query);
  166. }
  167. }