获取数据库 snapshot 恢复的状态
通过 GET /v2/databases/:database_id/snapshots/restore 跟踪数据库恢复:它是在排队、运行中、已完成还是已失败,当前所处的步骤以及错误代码。
GET
/
v2
/
databases
/
{database_id}
/
snapshots
/
restore
获取数据库 snapshot 恢复的状态
curl --request GET \
--url https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore \
--header 'Authorization: <authorization>'import requests
url = "https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore"
headers = {"Authorization": "<authorization>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: '<authorization>'}};
fetch('https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "<authorization>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore")
.header("Authorization", "<authorization>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = '<authorization>'
response = http.request(request)
puts response.read_body{
"status": "success",
"response": {
"state": "running",
"step": "load",
"started_at": 1760900000000
}
}
{
"status": "success",
"response": {
"state": "error",
"step": "done",
"code": "SNAPSHOT_CORRUPT",
"started_at": 1760900000000,
"finished_at": 1760900042000
}
}
{
"status": "success",
"response": {
"state": "none"
}
}
此路由的速率限制为每 60 秒 30 次请求。
你账户的 API 密钥。你可以在账户设置中找到它。
snapshots:read 权限范围的 API 密钥。
返回数据库最近一次 snapshot 恢复的进度和结果。数据库恢复会先把整个 snapshot 加载到一个新数据库中,然后才替换当前数据库,因此对于大型数据库,它通常比恢复路由的响应持续得更久,并返回 202 SNAPSHOT_RESTORE_PROCESSING。轮询此路由,直到 state 为 success 或 error。
恢复结束后,结果会保留一小时。超过这个时间,或者数据库从未被恢复过时,state 为 none。
参数
响应
{
"status": "success",
"response": {
"state": "running",
"step": "load",
"started_at": 1760900000000
}
}
{
"status": "success",
"response": {
"state": "error",
"step": "done",
"code": "SNAPSHOT_CORRUPT",
"started_at": 1760900000000,
"finished_at": 1760900042000
}
}
{
"status": "success",
"response": {
"state": "none"
}
}
步骤
每次恢复都从queued 开始,然后是 inspect,最后以 done 结束。在这之间,数据库会按以下顺序经过这些步骤:
| 步骤 | 正在发生什么 |
|---|---|
download | 正在获取 snapshot。 |
preflight、verify | 检查归档文件的大小以及是否损坏。到这一步为止,没有任何内容被更改。 |
pull、unzip | 准备引擎和 snapshot 的数据。 |
stop | 当前数据库被停止。从这里开始直到恢复结束,它都处于离线状态。 |
init、load | 创建一个新数据库,并把 snapshot 加载进去。这是耗时最长的步骤。 |
start | 新数据库被启动,并取代旧数据库。 |
cleanup | 清除旧数据库的残留内容。 |
recovery 步骤。被中断的恢复会以步骤 interrupted 结束,代码为 RESTORE_INTERRUPTED。请把你不认识的步骤视为“仍在运行”:今后可能会增加新的步骤。
失败代码
state: "error" 携带的代码,与恢复路由在其响应时间内失败时返回的代码相同:
| 代码 | 说明 |
|---|---|
SNAPSHOT_CORRUPT | 归档文件未通过完整性检查。数据库没有被改动。请恢复另一个 snapshot。 |
RESTORE_INTERRUPTED | 恢复被中断。请重新运行。 |
CONTAINER_NOT_FOUND | 在数据库所在的服务器上找不到它。请稍后重试,如果问题持续,请联系支持团队。 |
SNAPSHOT_RESTORE_FAILED | 恢复因其他原因失败,数据库回到了原有的数据。请重试,或恢复另一个 snapshot。 |
轮询示例
DB="https://api.squarecloud.app/v2/databases/$DATABASE_ID/snapshots/restore"
while true; do
state=$(curl -fsS "$DB" -H "Authorization: $SQUARECLOUD_API_KEY" | jq -r .response.state)
case "$state" in
success) echo "Restored."; break ;;
error|none) echo "Restore ended as: $state"; exit 1 ;;
esac
sleep 5
done
常见错误
| 代码 | HTTP | 说明 |
|---|---|---|
DATABASE_NOT_FOUND | 404 | 数据库不存在,或不属于调用者。 |
相关内容
- 恢复 snapshot:启动此路由所跟踪的恢复。
- 列出 snapshot:可以恢复的 snapshot。
- snapshot 与恢复:数据库 snapshot 包含什么,以及恢复的行为方式。
⌘I
获取数据库 snapshot 恢复的状态
curl --request GET \
--url https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore \
--header 'Authorization: <authorization>'import requests
url = "https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore"
headers = {"Authorization": "<authorization>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: '<authorization>'}};
fetch('https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "<authorization>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore")
.header("Authorization", "<authorization>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.squarecloud.app/v2/databases/{database_id}/snapshots/restore")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = '<authorization>'
response = http.request(request)
puts response.read_body{
"status": "success",
"response": {
"state": "running",
"step": "load",
"started_at": 1760900000000
}
}
{
"status": "success",
"response": {
"state": "error",
"step": "done",
"code": "SNAPSHOT_CORRUPT",
"started_at": 1760900000000,
"finished_at": 1760900042000
}
}
{
"status": "success",
"response": {
"state": "none"
}
}

