slm-api.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. It is, however, managed
  15. by a different enable setting. To disable SLM's functionality, set the cluster
  16. setting `xpack.slm.enabled` to `false` in elasticsearch.yml.
  17. [[slm-api-put]]
  18. === Put Snapshot Lifecycle Policy API
  19. Creates or updates a snapshot policy. If the policy already exists, the version
  20. is incremented. Only the latest version of a policy is stored.
  21. When a policy is created it is immediately scheduled based on the schedule of
  22. the policy, when a policy is updated its schedule changes are immediately
  23. applied.
  24. ==== Path Parameters
  25. `policy_id` (required)::
  26. (string) Identifier (id) for the policy.
  27. ==== Request Parameters
  28. include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  29. ==== Authorization
  30. You must have the `manage_slm` cluster privilege to use this API. You must also
  31. have the `manage` index privilege on all indices being managed by `policy`. All
  32. operations executed by {slm} for a policy are executed as the user that put the
  33. latest version of a policy. For more information, see
  34. {stack-ov}/security-privileges.html[Security Privileges].
  35. ==== Example
  36. The following creates a snapshot lifecycle policy with an id of
  37. `daily-snapshots`:
  38. [source,console]
  39. --------------------------------------------------
  40. PUT /_slm/policy/daily-snapshots
  41. {
  42. "schedule": "0 30 1 * * ?", <1>
  43. "name": "<daily-snap-{now/d}>", <2>
  44. "repository": "my_repository", <3>
  45. "config": { <4>
  46. "indices": ["data-*", "important"], <5>
  47. "ignore_unavailable": false,
  48. "include_global_state": false
  49. },
  50. "retention": {}
  51. }
  52. --------------------------------------------------
  53. // TEST[setup:setup-repository]
  54. <1> When the snapshot should be taken, in this case, 1:30am daily
  55. <2> The name each snapshot should be given
  56. <3> Which repository to take the snapshot in
  57. <4> Any extra snapshot configuration
  58. <5> Which indices the snapshot should contain
  59. The top-level keys that the policy supports are described below:
  60. |==================
  61. | Key | Description
  62. | `schedule` | A periodic or absolute time schedule. Supports all values
  63. supported by the cron scheduler:
  64. <<schedule-cron,Cron scheduler configuration>>
  65. | `name` | A name automatically given to each snapshot performed by this policy.
  66. Supports the same <<date-math-index-names,date math>> supported in index
  67. names. A UUID is automatically appended to the end of the name to prevent
  68. conflicting snapshot names.
  69. | `repository` | The snapshot repository that will contain snapshots created by
  70. this policy. The repository must exist prior to the policy's creation and can
  71. be created with the <<modules-snapshots,snapshot repository API>>.
  72. | `config` | Configuration for each snapshot that will be created by this
  73. policy. Any configuration is included with <<modules-snapshots,create snapshot
  74. requests>> issued by this policy.
  75. |==================
  76. To update an existing policy, simply use the put snapshot lifecycle policy API
  77. with the same policy id as an existing policy.
  78. [[slm-api-get]]
  79. === Get Snapshot Lifecycle Policy API
  80. Once a policy is in place, you can retrieve one or more of the policies using
  81. the get snapshot lifecycle policy API. This also includes information about the
  82. latest successful and failed invocation that the automatic snapshots have taken.
  83. ==== Path Parameters
  84. `policy_ids` (optional)::
  85. (string) Comma-separated ids of policies to retrieve.
  86. ==== Examples
  87. To retrieve a policy, perform a `GET` with the policy's id
  88. [source,console]
  89. --------------------------------------------------
  90. GET /_slm/policy/daily-snapshots?human
  91. --------------------------------------------------
  92. // TEST[continued]
  93. The output looks similar to the following:
  94. [source,console-result]
  95. --------------------------------------------------
  96. {
  97. "daily-snapshots" : {
  98. "version": 1, <1>
  99. "modified_date": "2019-04-23T01:30:00.000Z", <2>
  100. "modified_date_millis": 1556048137314,
  101. "policy" : {
  102. "schedule": "0 30 1 * * ?",
  103. "name": "<daily-snap-{now/d}>",
  104. "repository": "my_repository",
  105. "config": {
  106. "indices": ["data-*", "important"],
  107. "ignore_unavailable": false,
  108. "include_global_state": false
  109. },
  110. "retention": {}
  111. },
  112. "stats": {
  113. "policy": "daily-snapshots",
  114. "snapshots_taken": 0,
  115. "snapshots_failed": 0,
  116. "snapshots_deleted": 0,
  117. "snapshot_deletion_failures": 0
  118. },
  119. "next_execution": "2019-04-24T01:30:00.000Z", <3>
  120. "next_execution_millis": 1556048160000
  121. }
  122. }
  123. --------------------------------------------------
  124. // 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/]
  125. <1> The version of the snapshot policy, only the latest verison is stored and incremented when the policy is updated
  126. <2> The last time this policy was modified
  127. <3> The next time this policy will be executed
  128. Or, to retrieve all policies:
  129. [source,console]
  130. --------------------------------------------------
  131. GET /_slm/policy
  132. --------------------------------------------------
  133. // TEST[continued]
  134. [[slm-api-execute]]
  135. === Execute Snapshot Lifecycle Policy API
  136. Sometimes it can be useful to immediately execute a snapshot based on policy,
  137. perhaps before an upgrade or before performing other maintenance on indices. The
  138. execute snapshot policy API allows you to perform a snapshot immediately without
  139. waiting for a policy's scheduled invocation.
  140. ==== Path Parameters
  141. `policy_id` (required)::
  142. (string) Id of the policy to execute
  143. ==== Example
  144. To take an immediate snapshot using a policy, use the following
  145. [source,console]
  146. --------------------------------------------------
  147. POST /_slm/policy/daily-snapshots/_execute
  148. --------------------------------------------------
  149. // TEST[skip:we can't easily handle snapshots from docs tests]
  150. This API will immediately return with the generated snapshot name
  151. [source,console-result]
  152. --------------------------------------------------
  153. {
  154. "snapshot_name": "daily-snap-2019.04.24-gwrqoo2xtea3q57vvg0uea"
  155. }
  156. --------------------------------------------------
  157. // TESTRESPONSE[skip:we can't handle snapshots from docs tests]
  158. The snapshot will be taken in the background, you can use the
  159. <<modules-snapshots,snapshot APIs>> to monitor the status of the snapshot.
  160. Once a snapshot has been kicked off, you can see the latest successful or failed
  161. snapshot using the get snapshot lifecycle policy API:
  162. [source,console]
  163. --------------------------------------------------
  164. GET /_slm/policy/daily-snapshots?human
  165. --------------------------------------------------
  166. // TEST[skip:we already tested get policy above, the last_failure may not be present though]
  167. Which, in this case shows an error because the index did not exist:
  168. [source,console-result]
  169. --------------------------------------------------
  170. {
  171. "daily-snapshots" : {
  172. "version": 1,
  173. "modified_date": "2019-04-23T01:30:00.000Z",
  174. "modified_date_millis": 1556048137314,
  175. "policy" : {
  176. "schedule": "0 30 1 * * ?",
  177. "name": "<daily-snap-{now/d}>",
  178. "repository": "my_repository",
  179. "config": {
  180. "indices": ["data-*", "important"],
  181. "ignore_unavailable": false,
  182. "include_global_state": false
  183. },
  184. "retention": {}
  185. },
  186. "stats": {
  187. "policy": "daily-snapshots",
  188. "snapshots_taken": 0,
  189. "snapshots_failed": 1,
  190. "snapshots_deleted": 0,
  191. "snapshot_deletion_failures": 0
  192. }
  193. "last_failure": { <1>
  194. "snapshot_name": "daily-snap-2019.04.02-lohisb5ith2n8hxacaq3mw",
  195. "time_string": "2019-04-02T01:30:00.000Z",
  196. "time": 1556042030000,
  197. "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\"}"
  198. } ,
  199. "next_execution": "2019-04-24T01:30:00.000Z",
  200. "next_execution_millis": 1556048160000
  201. }
  202. }
  203. --------------------------------------------------
  204. // TESTRESPONSE[skip:the presence of last_failure is asynchronous and will be present for users, but is untestable]
  205. <1> The last unsuccessfully initiated snapshot by this policy, along with the details of its failure
  206. In this case, it failed due to the "important" index not existing and
  207. `ignore_unavailable` setting being set to `false`.
  208. Updating the policy to change the `ignore_unavailable` setting is done using the
  209. same put snapshot lifecycle policy API:
  210. [source,console]
  211. --------------------------------------------------
  212. PUT /_slm/policy/daily-snapshots
  213. {
  214. "schedule": "0 30 1 * * ?",
  215. "name": "<daily-snap-{now/d}>",
  216. "repository": "my_repository",
  217. "config": {
  218. "indices": ["data-*", "important"],
  219. "ignore_unavailable": true,
  220. "include_global_state": false
  221. }
  222. }
  223. --------------------------------------------------
  224. // TEST[continued]
  225. Another snapshot can immediately be executed to ensure the new policy works:
  226. [source,console]
  227. --------------------------------------------------
  228. POST /_slm/policy/daily-snapshots/_execute
  229. --------------------------------------------------
  230. // TEST[skip:we can't handle snapshots in docs tests]
  231. [source,console-result]
  232. --------------------------------------------------
  233. {
  234. "snapshot_name": "daily-snap-2019.04.24-tmtnyjtrsxkhbrrdcgg18a"
  235. }
  236. --------------------------------------------------
  237. // TESTRESPONSE[skip:we can't handle snapshots in docs tests]
  238. Now retriving the policy shows that the policy has successfully been executed:
  239. [source,console]
  240. --------------------------------------------------
  241. GET /_slm/policy/daily-snapshots?human
  242. --------------------------------------------------
  243. // TEST[skip:we already tested this above and the output may not be available yet]
  244. Which now includes the successful snapshot information:
  245. [source,console-result]
  246. --------------------------------------------------
  247. {
  248. "daily-snapshots" : {
  249. "version": 2, <1>
  250. "modified_date": "2019-04-23T01:30:00.000Z",
  251. "modified_date_millis": 1556048137314,
  252. "policy" : {
  253. "schedule": "0 30 1 * * ?",
  254. "name": "<daily-snap-{now/d}>",
  255. "repository": "my_repository",
  256. "config": {
  257. "indices": ["data-*", "important"],
  258. "ignore_unavailable": true,
  259. "include_global_state": false
  260. },
  261. "retention": {}
  262. },
  263. "stats": {
  264. "policy": "daily-snapshots",
  265. "snapshots_taken": 1,
  266. "snapshots_failed": 1,
  267. "snapshots_deleted": 0,
  268. "snapshot_deletion_failures": 0
  269. },
  270. "last_success": { <2>
  271. "snapshot_name": "daily-snap-2019.04.24-tmtnyjtrsxkhbrrdcgg18a",
  272. "time_string": "2019-04-24T16:43:49.316Z",
  273. "time": 1556124229316
  274. } ,
  275. "last_failure": {
  276. "snapshot_name": "daily-snap-2019.04.02-lohisb5ith2n8hxacaq3mw",
  277. "time_string": "2019-04-02T01:30:00.000Z",
  278. "time": 1556042030000,
  279. "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\"}"
  280. } ,
  281. "next_execution": "2019-04-24T01:30:00.000Z",
  282. "next_execution_millis": 1556048160000
  283. }
  284. }
  285. --------------------------------------------------
  286. // TESTRESPONSE[skip:the presence of last_failure and last_success is asynchronous and will be present for users, but is untestable]
  287. <1> The policy's version has been incremented because it was updated
  288. <2> The last successfully initiated snapshot information
  289. It is a good idea to test policies using the execute API to ensure they work.
  290. [[slm-get-stats]]
  291. === Get Snapshot Lifecycle Stats API
  292. SLM stores statistics on a global and per-policy level about actions taken. These stats can be
  293. retrieved by using the following API:
  294. ==== Example
  295. [source,console]
  296. --------------------------------------------------
  297. GET /_slm/stats
  298. --------------------------------------------------
  299. // TEST[continued]
  300. Which returns a response similar to:
  301. [source,js]
  302. --------------------------------------------------
  303. {
  304. "retention_runs": 13,
  305. "retention_failed": 0,
  306. "retention_timed_out": 0,
  307. "retention_deletion_time": "1.4s",
  308. "retention_deletion_time_millis": 1404,
  309. "policy_metrics": [
  310. {
  311. "policy": "daily-snapshots",
  312. "snapshots_taken": 1,
  313. "snapshots_failed": 1,
  314. "snapshots_deleted": 0,
  315. "snapshot_deletion_failures": 0
  316. }
  317. ],
  318. "total_snapshots_taken": 1,
  319. "total_snapshots_failed": 1,
  320. "total_snapshots_deleted": 0,
  321. "total_snapshot_deletion_failures": 0
  322. }
  323. --------------------------------------------------
  324. // TESTRESPONSE[s/runs": 13/runs": $body.retention_runs/ s/_failed": 0/_failed": $body.retention_failed/ s/_timed_out": 0/_timed_out": $body.retention_timed_out/ s/"1.4s"/$body.retention_deletion_time/ s/1404/$body.retention_deletion_time_millis/]
  325. [[slm-api-delete]]
  326. === Delete Snapshot Lifecycle Policy API
  327. A policy can be deleted by issuing a delete request with the policy id. Note
  328. that this prevents any future snapshots from being taken, but does not cancel
  329. any currently ongoing snapshots or remove any previously taken snapshots.
  330. ==== Path Parameters
  331. `policy_id` (optional)::
  332. (string) Id of the policy to remove.
  333. ==== Example
  334. [source,console]
  335. --------------------------------------------------
  336. DELETE /_slm/policy/daily-snapshots
  337. --------------------------------------------------
  338. // TEST[continued]