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