slm-api.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[snapshot-lifecycle-management-api]]
  4. == Snapshot lifecycle management API
  5. The Snapshot Lifecycle Management APIs are used to manage policies for the time
  6. and frequency of automatic snapshots. Snapshot Lifecycle Management is related
  7. to <<index-lifecycle-management,Index Lifecycle Management>>, however, instead
  8. of managing a lifecycle of actions that are performed on a single index, SLM
  9. allows configuring policies spanning multiple indices.
  10. SLM policy management is split into three different CRUD APIs, a way to put or update
  11. policies, a way to retrieve policies, and a way to delete unwanted policies, as
  12. well as a separate API for immediately invoking a snapshot based on a policy.
  13. Since SLM falls under the same category as ILM, it is stopped and started by
  14. using the <<start-stop-ilm,start and stop>> ILM APIs.
  15. [[slm-api-put]]
  16. === Put Snapshot Lifecycle Policy API
  17. Creates or updates a snapshot policy. If the policy already exists, the version
  18. is incremented. Only the latest version of a policy is stored.
  19. When a policy is created it is immediately scheduled based on the schedule of
  20. the policy, when a policy is updated its schedule changes are immediately
  21. applied.
  22. ==== Path Parameters
  23. `policy_id` (required)::
  24. (string) Identifier (id) for the policy.
  25. ==== Request Parameters
  26. include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  27. ==== Authorization
  28. You must have the `manage_slm` cluster privilege to use this API. You must also
  29. have the `manage` index privilege on all indices being managed by `policy`. All
  30. operations executed by {slm} for a policy are executed as the user that put the
  31. latest version of a policy. For more information, see
  32. {stack-ov}/security-privileges.html[Security Privileges].
  33. ==== Example
  34. The following creates a snapshot lifecycle policy with an id of
  35. `daily-snapshots`:
  36. [source,js]
  37. --------------------------------------------------
  38. PUT /_slm/policy/daily-snapshots
  39. {
  40. "schedule": "0 30 1 * * ?", <1>
  41. "name": "<daily-snap-{now/d}>", <2>
  42. "repository": "my_repository", <3>
  43. "config": { <4>
  44. "indices": ["data-*", "important"], <5>
  45. "ignore_unavailable": false,
  46. "include_global_state": false
  47. }
  48. }
  49. --------------------------------------------------
  50. // CONSOLE
  51. // TEST[setup:setup-repository]
  52. <1> When the snapshot should be taken, in this case, 1:30am daily
  53. <2> The name each snapshot should be given
  54. <3> Which repository to take the snapshot in
  55. <4> Any extra snapshot configuration
  56. <5> Which indices the snapshot should contain
  57. The top-level keys that the policy supports are described below:
  58. |==================
  59. | Key | Description
  60. | `schedule` | A periodic or absolute time schedule. Supports all values
  61. supported by the cron scheduler:
  62. {xpack-ref}/trigger-schedule.html#schedule-cron[Cron scheduler configuration]
  63. | `name` | A name automatically given to each snapshot performed by this policy.
  64. Supports the same <<date-math-index-names,date math>> supported in index
  65. names. A UUID is automatically appended to the end of the name to prevent
  66. conflicting snapshot names.
  67. | `repository` | The snapshot repository that will contain snapshots created by
  68. this policy. The repository must exist prior to the policy's creation and can
  69. be created with the <<modules-snapshots,snapshot repository API>>.
  70. | `config` | Configuration for each snapshot that will be created by this
  71. policy. Any configuration is included with <<modules-snapshots,create snapshot
  72. requests>> issued by this policy.
  73. |==================
  74. To update an existing policy, simply use the put snapshot lifecycle policy API
  75. with the same policy id as an existing policy.
  76. [[slm-api-get]]
  77. === Get Snapshot Lifecycle Policy API
  78. Once a policy is in place, you can retrieve one or more of the policies using
  79. the get snapshot lifecycle policy API. This also includes information about the
  80. latest successful and failed invocation that the automatic snapshots have taken.
  81. ==== Path Parameters
  82. `policy_ids` (optional)::
  83. (string) Comma-separated ids of policies to retrieve.
  84. ==== Examples
  85. To retrieve a policy, perform a `GET` with the policy's id
  86. [source,js]
  87. --------------------------------------------------
  88. GET /_slm/policy/daily-snapshots?human
  89. --------------------------------------------------
  90. // CONSOLE
  91. // TEST[continued]
  92. The output looks similar to the following:
  93. [source,js]
  94. --------------------------------------------------
  95. {
  96. "daily-snapshots" : {
  97. "version": 1, <1>
  98. "modified_date": "2019-04-23T01:30:00.000Z", <2>
  99. "modified_date_millis": 1556048137314,
  100. "policy" : {
  101. "schedule": "0 30 1 * * ?",
  102. "name": "<daily-snap-{now/d}>",
  103. "repository": "my_repository",
  104. "config": {
  105. "indices": ["data-*", "important"],
  106. "ignore_unavailable": false,
  107. "include_global_state": false
  108. }
  109. },
  110. "next_execution": "2019-04-24T01:30:00.000Z", <3>
  111. "next_execution_millis": 1556048160000
  112. }
  113. }
  114. --------------------------------------------------
  115. // TESTRESPONSE[s/"modified_date": "2019-04-23T01:30:00.000Z"/"modified_date": $body.daily-snapshots.modified_date/ s/"modified_date_millis": 1556048137314/"modified_date_millis": $body.daily-snapshots.modified_date_millis/ s/"next_execution": "2019-04-24T01:30:00.000Z"/"next_execution": $body.daily-snapshots.next_execution/ s/"next_execution_millis": 1556048160000/"next_execution_millis": $body.daily-snapshots.next_execution_millis/]
  116. <1> The version of the snapshot policy, only the latest verison is stored and incremented when the policy is updated
  117. <2> The last time this policy was modified
  118. <3> The next time this policy will be executed
  119. Or, to retrieve all policies:
  120. [source,js]
  121. --------------------------------------------------
  122. GET /_slm/policy
  123. --------------------------------------------------
  124. // CONSOLE
  125. // TEST[continued]
  126. [[slm-api-execute]]
  127. === Execute Snapshot Lifecycle Policy API
  128. Sometimes it can be useful to immediately execute a snapshot based on policy,
  129. perhaps before an upgrade or before performing other maintenance on indices. The
  130. execute snapshot policy API allows you to perform a snapshot immediately without
  131. waiting for a policy's scheduled invocation.
  132. ==== Path Parameters
  133. `policy_id` (required)::
  134. (string) Id of the policy to execute
  135. ==== Example
  136. To take an immediate snapshot using a policy, use the following
  137. [source,js]
  138. --------------------------------------------------
  139. PUT /_slm/policy/daily-snapshots/_execute
  140. --------------------------------------------------
  141. // CONSOLE
  142. // TEST[skip:we can't easily handle snapshots from docs tests]
  143. This API will immediately return with the generated snapshot name
  144. [source,js]
  145. --------------------------------------------------
  146. {
  147. "snapshot_name": "daily-snap-2019.04.24-gwrqoo2xtea3q57vvg0uea"
  148. }
  149. --------------------------------------------------
  150. // TESTRESPONSE[skip:we can't handle snapshots from docs tests]
  151. The snapshot will be taken in the background, you can use the
  152. <<modules-snapshots,snapshot APIs>> to monitor the status of the snapshot.
  153. Once a snapshot has been kicked off, you can see the latest successful or failed
  154. snapshot using the get snapshot lifecycle policy API:
  155. [source,js]
  156. --------------------------------------------------
  157. GET /_slm/policy/daily-snapshots?human
  158. --------------------------------------------------
  159. // CONSOLE
  160. // TEST[skip:we already tested get policy above, the last_failure may not be present though]
  161. Which, in this case shows an error because the index did not exist:
  162. [source,js]
  163. --------------------------------------------------
  164. {
  165. "daily-snapshots" : {
  166. "version": 1,
  167. "modified_date": "2019-04-23T01:30:00.000Z",
  168. "modified_date_millis": 1556048137314,
  169. "policy" : {
  170. "schedule": "0 30 1 * * ?",
  171. "name": "<daily-snap-{now/d}>",
  172. "repository": "my_repository",
  173. "config": {
  174. "indices": ["data-*", "important"],
  175. "ignore_unavailable": false,
  176. "include_global_state": false
  177. }
  178. },
  179. "last_failure": { <1>
  180. "snapshot_name": "daily-snap-2019.04.02-lohisb5ith2n8hxacaq3mw",
  181. "time_string": "2019-04-02T01:30:00.000Z",
  182. "time": 1556042030000,
  183. "details": "{\"type\":\"index_not_found_exception\",\"reason\":\"no such index [important]\",\"resource.type\":\"index_or_alias\",\"resource.id\":\"important\",\"index_uuid\":\"_na_\",\"index\":\"important\",\"stack_trace\":\"[important] IndexNotFoundException[no such index [important]]\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.indexNotFoundException(IndexNameExpressionResolver.java:762)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.innerResolve(IndexNameExpressionResolver.java:714)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.resolve(IndexNameExpressionResolver.java:670)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndices(IndexNameExpressionResolver.java:163)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndexNames(IndexNameExpressionResolver.java:142)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndexNames(IndexNameExpressionResolver.java:102)\\n\\tat org.elasticsearch.snapshots.SnapshotsService$1.execute(SnapshotsService.java:280)\\n\\tat org.elasticsearch.cluster.ClusterStateUpdateTask.execute(ClusterStateUpdateTask.java:47)\\n\\tat org.elasticsearch.cluster.service.MasterService.executeTasks(MasterService.java:687)\\n\\tat org.elasticsearch.cluster.service.MasterService.calculateTaskOutputs(MasterService.java:310)\\n\\tat org.elasticsearch.cluster.service.MasterService.runTasks(MasterService.java:210)\\n\\tat org.elasticsearch.cluster.service.MasterService$Batcher.run(MasterService.java:142)\\n\\tat org.elasticsearch.cluster.service.TaskBatcher.runIfNotProcessed(TaskBatcher.java:150)\\n\\tat org.elasticsearch.cluster.service.TaskBatcher$BatchedTask.run(TaskBatcher.java:188)\\n\\tat org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:688)\\n\\tat org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:252)\\n\\tat org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:215)\\n\\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\\n\\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\\n\\tat java.base/java.lang.Thread.run(Thread.java:834)\\n\"}"
  184. } ,
  185. "next_execution": "2019-04-24T01:30:00.000Z",
  186. "next_execution_millis": 1556048160000
  187. }
  188. }
  189. --------------------------------------------------
  190. // TESTRESPONSE[skip:the presence of last_failure is asynchronous and will be present for users, but is untestable]
  191. <1> The last unsuccessfully initiated snapshot by this policy, along with the details of its failure
  192. In this case, it failed due to the "important" index not existing and
  193. `ignore_unavailable` setting being set to `false`.
  194. Updating the policy to change the `ignore_unavailable` setting is done using the
  195. same put snapshot lifecycle policy API:
  196. [source,js]
  197. --------------------------------------------------
  198. PUT /_slm/policy/daily-snapshots
  199. {
  200. "schedule": "0 30 1 * * ?",
  201. "name": "<daily-snap-{now/d}>",
  202. "repository": "my_repository",
  203. "config": {
  204. "indices": ["data-*", "important"],
  205. "ignore_unavailable": true,
  206. "include_global_state": false
  207. }
  208. }
  209. --------------------------------------------------
  210. // CONSOLE
  211. // TEST[continued]
  212. Another snapshot can immediately be executed to ensure the new policy works:
  213. [source,js]
  214. --------------------------------------------------
  215. PUT /_slm/policy/daily-snapshots/_execute
  216. --------------------------------------------------
  217. // CONSOLE
  218. // TEST[skip:we can't handle snapshots in docs tests]
  219. [source,js]
  220. --------------------------------------------------
  221. {
  222. "snapshot_name": "daily-snap-2019.04.24-tmtnyjtrsxkhbrrdcgg18a"
  223. }
  224. --------------------------------------------------
  225. // TESTRESPONSE[skip:we can't handle snapshots in docs tests]
  226. Now retriving the policy shows that the policy has successfully been executed:
  227. [source,js]
  228. --------------------------------------------------
  229. GET /_slm/policy/daily-snapshots?human
  230. --------------------------------------------------
  231. // CONSOLE
  232. // TEST[skip:we already tested this above and the output may not be available yet]
  233. Which now includes the successful snapshot information:
  234. [source,js]
  235. --------------------------------------------------
  236. {
  237. "daily-snapshots" : {
  238. "version": 2, <1>
  239. "modified_date": "2019-04-23T01:30:00.000Z",
  240. "modified_date_millis": 1556048137314,
  241. "policy" : {
  242. "schedule": "0 30 1 * * ?",
  243. "name": "<daily-snap-{now/d}>",
  244. "repository": "my_repository",
  245. "config": {
  246. "indices": ["data-*", "important"],
  247. "ignore_unavailable": true,
  248. "include_global_state": false
  249. }
  250. },
  251. "last_success": { <2>
  252. "snapshot_name": "daily-snap-2019.04.24-tmtnyjtrsxkhbrrdcgg18a",
  253. "time_string": "2019-04-24T16:43:49.316Z",
  254. "time": 1556124229316
  255. } ,
  256. "last_failure": {
  257. "snapshot_name": "daily-snap-2019.04.02-lohisb5ith2n8hxacaq3mw",
  258. "time_string": "2019-04-02T01:30:00.000Z",
  259. "time": 1556042030000,
  260. "details": "{\"type\":\"index_not_found_exception\",\"reason\":\"no such index [important]\",\"resource.type\":\"index_or_alias\",\"resource.id\":\"important\",\"index_uuid\":\"_na_\",\"index\":\"important\",\"stack_trace\":\"[important] IndexNotFoundException[no such index [important]]\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.indexNotFoundException(IndexNameExpressionResolver.java:762)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.innerResolve(IndexNameExpressionResolver.java:714)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.resolve(IndexNameExpressionResolver.java:670)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndices(IndexNameExpressionResolver.java:163)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndexNames(IndexNameExpressionResolver.java:142)\\n\\tat org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndexNames(IndexNameExpressionResolver.java:102)\\n\\tat org.elasticsearch.snapshots.SnapshotsService$1.execute(SnapshotsService.java:280)\\n\\tat org.elasticsearch.cluster.ClusterStateUpdateTask.execute(ClusterStateUpdateTask.java:47)\\n\\tat org.elasticsearch.cluster.service.MasterService.executeTasks(MasterService.java:687)\\n\\tat org.elasticsearch.cluster.service.MasterService.calculateTaskOutputs(MasterService.java:310)\\n\\tat org.elasticsearch.cluster.service.MasterService.runTasks(MasterService.java:210)\\n\\tat org.elasticsearch.cluster.service.MasterService$Batcher.run(MasterService.java:142)\\n\\tat org.elasticsearch.cluster.service.TaskBatcher.runIfNotProcessed(TaskBatcher.java:150)\\n\\tat org.elasticsearch.cluster.service.TaskBatcher$BatchedTask.run(TaskBatcher.java:188)\\n\\tat org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:688)\\n\\tat org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:252)\\n\\tat org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:215)\\n\\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\\n\\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\\n\\tat java.base/java.lang.Thread.run(Thread.java:834)\\n\"}"
  261. } ,
  262. "next_execution": "2019-04-24T01:30:00.000Z",
  263. "next_execution_millis": 1556048160000
  264. }
  265. }
  266. --------------------------------------------------
  267. // TESTRESPONSE[skip:the presence of last_failure and last_success is asynchronous and will be present for users, but is untestable]
  268. <1> The policy's version has been incremented because it was updated
  269. <2> The last successfully initiated snapshot information
  270. It is a good idea to test policies using the execute API to ensure they work.
  271. [[slm-api-delete]]
  272. === Delete Snapshot Lifecycle Policy API
  273. A policy can be deleted by issuing a delete request with the policy id. Note
  274. that this prevents any future snapshots from being taken, but does not cancel
  275. any currently ongoing snapshots or remove any previously taken snapshots.
  276. ==== Path Parameters
  277. `policy_id` (optional)::
  278. (string) Id of the policy to remove.
  279. ==== Example
  280. [source,js]
  281. --------------------------------------------------
  282. DELETE /_slm/policy/daily-snapshots
  283. --------------------------------------------------
  284. // CONSOLE
  285. // TEST[continued]