snapshots.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. [[modules-snapshots]]
  2. == Snapshot And Restore
  3. The snapshot and restore module allows to create snapshots of individual indices or an entire cluster into a remote
  4. repository. At the time of the initial release only shared file system repository was supported, but now a range of
  5. backends are available via officially supported repository plugins.
  6. [float]
  7. === Repositories
  8. Before any snapshot or restore operation can be performed a snapshot repository should be registered in
  9. Elasticsearch. The following command registers a shared file system repository with the name `my_backup` that
  10. will use location `/mount/backups/my_backup` to store snapshots.
  11. [source,js]
  12. -----------------------------------
  13. $ curl -XPUT 'http://localhost:9200/_snapshot/my_backup' -d '{
  14. "type": "fs",
  15. "settings": {
  16. "location": "/mount/backups/my_backup",
  17. "compress": true
  18. }
  19. }'
  20. -----------------------------------
  21. Once repository is registered, its information can be obtained using the following command:
  22. [source,js]
  23. -----------------------------------
  24. $ curl -XGET 'http://localhost:9200/_snapshot/my_backup?pretty'
  25. -----------------------------------
  26. [source,js]
  27. -----------------------------------
  28. {
  29. "my_backup" : {
  30. "type" : "fs",
  31. "settings" : {
  32. "compress" : "true",
  33. "location" : "/mount/backups/my_backup"
  34. }
  35. }
  36. }
  37. -----------------------------------
  38. If a repository name is not specified, or `_all` is used as repository name Elasticsearch will return information about
  39. all repositories currently registered in the cluster:
  40. [source,js]
  41. -----------------------------------
  42. $ curl -XGET 'http://localhost:9200/_snapshot'
  43. -----------------------------------
  44. or
  45. [source,js]
  46. -----------------------------------
  47. $ curl -XGET 'http://localhost:9200/_snapshot/_all'
  48. -----------------------------------
  49. [float]
  50. ===== Shared File System Repository
  51. The shared file system repository (`"type": "fs"`) is using shared file system to store snapshot. The path
  52. specified in the `location` parameter should point to the same location in the shared filesystem and be accessible
  53. on all data and master nodes. The following settings are supported:
  54. [horizontal]
  55. `location`:: Location of the snapshots. Mandatory.
  56. `compress`:: Turns on compression of the snapshot files. Compression is applied only to metadata files (index mapping and settings). Data files are not compressed. Defaults to `true`.
  57. `chunk_size`:: Big files can be broken down into chunks during snapshotting if needed. The chunk size can be specified in bytes or by
  58. using size value notation, i.e. 1g, 10m, 5k. Defaults to `null` (unlimited chunk size).
  59. `max_restore_bytes_per_sec`:: Throttles per node restore rate. Defaults to `20mb` per second.
  60. `max_snapshot_bytes_per_sec`:: Throttles per node snapshot rate. Defaults to `20mb` per second.
  61. `verify`:: Verify repository upon creation. Defaults to `true`.
  62. [float]
  63. ===== Read-only URL Repository
  64. The URL repository (`"type": "url"`) can be used as an alternative read-only way to access data created by shared file
  65. system repository is using shared file system to store snapshot. The URL specified in the `url` parameter should
  66. point to the root of the shared filesystem repository. The following settings are supported:
  67. [horizontal]
  68. `url`:: Location of the snapshots. Mandatory.
  69. [float]
  70. ===== Repository plugins
  71. Other repository backends are available in these official plugins:
  72. * https://github.com/elasticsearch/elasticsearch-cloud-aws#s3-repository[AWS Cloud Plugin] for S3 repositories
  73. * https://github.com/elasticsearch/elasticsearch-hadoop/tree/master/repository-hdfs[HDFS Plugin] for Hadoop environments
  74. * https://github.com/elasticsearch/elasticsearch-cloud-azure#azure-repository[Azure Cloud Plugin] for Azure storage repositories
  75. [float]
  76. ===== Repository Verification
  77. When repository is registered, it's immediately verified on all master and data nodes to make sure that it's functional
  78. on all nodes currently present in the cluster. The verification process can also be executed manually by running the
  79. following command:
  80. [source,js]
  81. -----------------------------------
  82. $ curl -XPOST 'http://localhost:9200/_snapshot/my_backup/_verify'
  83. -----------------------------------
  84. It returns a list of nodes where repository was successfully verified or an error message if verification process failed.
  85. [float]
  86. === Snapshot
  87. A repository can contain multiple snapshots of the same cluster. Snapshot are identified by unique names within the
  88. cluster. A snapshot with the name `snapshot_1` in the repository `my_backup` can be created by executing the following
  89. command:
  90. [source,js]
  91. -----------------------------------
  92. $ curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true"
  93. -----------------------------------
  94. The `wait_for_completion` parameter specifies whether or not the request should return immediately after snapshot
  95. initialization (default) or wait for snapshot completion. During snapshot initialization, information about all
  96. previous snapshots is loaded into the memory, which means that in large repositories it may take several seconds (or
  97. even minutes) for this command to return even if the `wait_for_completion` parameter is set to `false`.
  98. By default snapshot of all open and started indices in the cluster is created. This behavior can be changed by
  99. specifying the list of indices in the body of the snapshot request.
  100. [source,js]
  101. -----------------------------------
  102. $ curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_1" -d '{
  103. "indices": "index_1,index_2",
  104. "ignore_unavailable": "true",
  105. "include_global_state": false
  106. }'
  107. -----------------------------------
  108. The list of indices that should be included into the snapshot can be specified using the `indices` parameter that
  109. supports <<search-multi-index-type,multi index syntax>>. The snapshot request also supports the
  110. `ignore_unavailable` option. Setting it to `true` will cause indices that do not exist to be ignored during snapshot
  111. creation. By default, when `ignore_unavailable` option is not set and an index is missing the snapshot request will fail.
  112. By setting `include_global_state` to false it's possible to prevent the cluster global state to be stored as part of
  113. the snapshot. By default, entire snapshot will fail if one or more indices participating in the snapshot don't have
  114. all primary shards available. This behaviour can be changed by setting `partial` to `true`.
  115. The index snapshot process is incremental. In the process of making the index snapshot Elasticsearch analyses
  116. the list of the index files that are already stored in the repository and copies only files that were created or
  117. changed since the last snapshot. That allows multiple snapshots to be preserved in the repository in a compact form.
  118. Snapshotting process is executed in non-blocking fashion. All indexing and searching operation can continue to be
  119. executed against the index that is being snapshotted. However, a snapshot represents the point-in-time view of the index
  120. at the moment when snapshot was created, so no records that were added to the index after snapshot process had started
  121. will be present in the snapshot. The snapshot process starts immediately for the primary shards that has been started
  122. and are not relocating at the moment. Before version 1.2.0, the snapshot operation fails if the cluster has any relocating or
  123. initializing primaries of indices participating in the snapshot. Starting with version 1.2.0, Elasticsearch waits for
  124. relocation or initialization of shards to complete before snapshotting them.
  125. Besides creating a copy of each index the snapshot process can also store global cluster metadata, which includes persistent
  126. cluster settings and templates. The transient settings and registered snapshot repositories are not stored as part of
  127. the snapshot.
  128. Only one snapshot process can be executed in the cluster at any time. While snapshot of a particular shard is being
  129. created this shard cannot be moved to another node, which can interfere with rebalancing process and allocation
  130. filtering. Once snapshot of the shard is finished Elasticsearch will be able to move shard to another node according
  131. to the current allocation filtering settings and rebalancing algorithm.
  132. Once a snapshot is created information about this snapshot can be obtained using the following command:
  133. [source,shell]
  134. -----------------------------------
  135. $ curl -XGET "localhost:9200/_snapshot/my_backup/snapshot_1"
  136. -----------------------------------
  137. All snapshots currently stored in the repository can be listed using the following command:
  138. [source,shell]
  139. -----------------------------------
  140. $ curl -XGET "localhost:9200/_snapshot/my_backup/_all"
  141. -----------------------------------
  142. A snapshot can be deleted from the repository using the following command:
  143. [source,shell]
  144. -----------------------------------
  145. $ curl -XDELETE "localhost:9200/_snapshot/my_backup/snapshot_1"
  146. -----------------------------------
  147. When a snapshot is deleted from a repository, Elasticsearch deletes all files that are associated with the deleted
  148. snapshot and not used by any other snapshots. If the deleted snapshot operation is executed while the snapshot is being
  149. created the snapshotting process will be aborted and all files created as part of the snapshotting process will be
  150. cleaned. Therefore, the delete snapshot operation can be used to cancel long running snapshot operations that were
  151. started by mistake.
  152. A repository can be deleted using the following command:
  153. [source,shell]
  154. -----------------------------------
  155. $ curl -XDELETE "localhost:9200/_snapshot/my_backup"
  156. -----------------------------------
  157. When a repository is deleted, Elasticsearch only removes the reference to the location where the repository is storing
  158. the snapshots. The snapshots themselves are left untouched and in place.
  159. [float]
  160. === Restore
  161. A snapshot can be restored using the following command:
  162. [source,shell]
  163. -----------------------------------
  164. $ curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore"
  165. -----------------------------------
  166. By default, all indices in the snapshot as well as cluster state are restored. It's possible to select indices that
  167. should be restored as well as prevent global cluster state from being restored by using `indices` and
  168. `include_global_state` options in the restore request body. The list of indices supports
  169. <<search-multi-index-type,multi index syntax>>. The `rename_pattern` and `rename_replacement` options can be also used to
  170. rename index on restore using regular expression that supports referencing the original text as explained
  171. http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#appendReplacement(java.lang.StringBuffer,%20java.lang.String)[here].
  172. Set `include_aliases` to `false` to prevent aliases from being restored together with associated indices
  173. [source,js]
  174. -----------------------------------
  175. $ curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore" -d '{
  176. "indices": "index_1,index_2",
  177. "ignore_unavailable": "true",
  178. "include_global_state": false,
  179. "rename_pattern": "index_(.+)",
  180. "rename_replacement": "restored_index_$1"
  181. }'
  182. -----------------------------------
  183. The restore operation can be performed on a functioning cluster. However, an existing index can be only restored if it's
  184. <<indices-open-close,closed>>. The restore operation automatically opens restored indices if they were closed and creates new indices if they
  185. didn't exist in the cluster. If cluster state is restored, the restored templates that don't currently exist in the
  186. cluster are added and existing templates with the same name are replaced by the restored templates. The restored
  187. persistent settings are added to the existing persistent settings.
  188. [float]
  189. === Partial restore
  190. By default, entire restore operation will fail if one or more indices participating in the operation don't have
  191. snapshots of all shards available. It can occur if some shards failed to snapshot for example. It is still possible to
  192. restore such indices by setting `partial` to `true`. Please note, that only successfully snapshotted shards will be
  193. restored in this case and all missing shards will be recreated empty.
  194. [float]
  195. === Snapshot status
  196. A list of currently running snapshots with their detailed status information can be obtained using the following command:
  197. [source,shell]
  198. -----------------------------------
  199. $ curl -XGET "localhost:9200/_snapshot/_status"
  200. -----------------------------------
  201. In this format, the command will return information about all currently running snapshots. By specifying a repository name, it's possible
  202. to limit the results to a particular repository:
  203. [source,shell]
  204. -----------------------------------
  205. $ curl -XGET "localhost:9200/_snapshot/my_backup/_status"
  206. -----------------------------------
  207. If both repository name and snapshot id are specified, this command will return detailed status information for the given snapshot even
  208. if it's not currently running:
  209. [source,shell]
  210. -----------------------------------
  211. $ curl -XGET "localhost:9200/_snapshot/my_backup/snapshot_1/_status"
  212. -----------------------------------
  213. Multiple ids are also supported:
  214. [source,shell]
  215. -----------------------------------
  216. $ curl -XGET "localhost:9200/_snapshot/my_backup/snapshot_1,snapshot_2/_status"
  217. -----------------------------------
  218. [float]
  219. === Monitoring snapshot/restore progress
  220. There are several ways to monitor the progress of the snapshot and restores processes while they are running. Both
  221. operations support `wait_for_completion` parameter that would block client until the operation is completed. This is
  222. the simplest method that can be used to get notified about operation completion.
  223. The snapshot operation can be also monitored by periodic calls to the snapshot info:
  224. [source,shell]
  225. -----------------------------------
  226. $ curl -XGET "localhost:9200/_snapshot/my_backup/snapshot_1"
  227. -----------------------------------
  228. Please note that snapshot info operation is using the same resources and thread pool as the snapshot operation. So,
  229. executing snapshot info operation while large shards are being snapshotted can cause the snapshot info operation to wait
  230. for available resources before returning the result. On very large shards the wait time can be significant.
  231. To get more immediate and complete information about snapshots the snapshot status command can be used instead:
  232. [source,shell]
  233. -----------------------------------
  234. $ curl -XGET "localhost:9200/_snapshot/my_backup/snapshot_1/_status"
  235. -----------------------------------
  236. While snapshot info method returns only basic information about the snapshot in progress, the snapshot status returns
  237. complete breakdown of the current state for each shard participating in the snapshot.
  238. The restore process piggybacks on the standard recovery mechanism of the Elasticsearch. As a result, standard recovery
  239. monitoring services can be used to monitor the state of restore. When restore operation is executed the cluster
  240. typically goes into `red` state. It happens because the restore operation starts with "recovering" primary shards of the
  241. restored indices. During this operation the primary shards become unavailable which manifests itself in the `red` cluster
  242. state. Once recovery of primary shards is completed Elasticsearch is switching to standard replication process that
  243. creates the required number of replicas at this moment cluster switches to the `yellow` state. Once all required replicas
  244. are created, the cluster switches to the `green` states.
  245. The cluster health operation provides only a high level status of the restore process. It’s possible to get more
  246. detailed insight into the current state of the recovery process by using <<indices-recovery, indices recovery>> and
  247. <<cat-recovery, cat recovery>> APIs.
  248. [float]
  249. === Stopping currently running snapshot and restore operations
  250. The snapshot and restore framework allows running only one snapshot or one restore operation at time. If currently
  251. running snapshot was executed by mistake or takes unusually long, it can be terminated using snapshot delete operation.
  252. The snapshot delete operation checks if deleted snapshot is currently running and if it does, the delete operation stops
  253. such snapshot before deleting the snapshot data from the repository.
  254. The restore operation is using standard shard recovery mechanism. Therefore, any currently running restore operation can
  255. be canceled by deleting indices that are being restored. Please note that data for all deleted indices will be removed
  256. from the cluster as a result of this operation.