如何在Windows 10上自动创建备份,下载备份并还原postgres备份?

时间:2019-07-10 09:28:40

标签: windows batch-file cmd

为了自动在本地Windows计算机上获取最新的生产数据库,我想自动执行以下命令的过程:

1-创建备份

curl -H "Username: sian@kurt.com" -H "ApiKey: 2c299965-2458-4708-908c-37519859e139" --data-raw '{"Comment" :  "SK"} https://deploy.x.com/api/1/apps/hdibb/environments/Production/snapshots -X POST > RES_CreateBackup.json

导致以下数组响应,其中我需要捕获头对象。最新的snahshopID的快照ID(将在第二个命令中使用); 20a105e1-d8b3-4b0e-822a-6fcecfa8e67c

[{
  "SnapshotID": "20a105e1-d8b3-4b0e-822a-6fcecfa8e67c",
  "Comment": "Manually created snapshot via API",
  "State": "Completed",
  "ExpiresOn": 1570697660000,
  "CreatedOn": 1562748860000,
  "ModelVersion": "0.0.1.812"
},{
  "SnapshotID": "ecbd5a14-5a4a-43d2-9e59-4d6eae94c568",
  "Comment": "Automatically created nightly snapshot",
  "State": "Completed",
  "ExpiresOn": 1563930109000,
  "CreatedOn": 1562720509000,
  "ModelVersion": "0.0.1.812"
},{
  "SnapshotID": "f6bf15dc-0f7a-483a-b1f1-dc957de921b8",
  "Comment": "SK",
  "State": "Completed",
  "ExpiresOn": 1570599876000,
  "CreatedOn": 1562651076000,
  "ModelVersion": "0.0.1.812"
....

2-获取备用网址

curl -H "Username: si@kurt.nl" -H "ApiKey: 2c299965-2458-4708-908c-37519859e139" https://deploy.x.com/api/1/apps/hdibb/environments/Production/snapshots/20a105e1-d8b3-4b0e-822a-6fcecfa8e67c -X GET > RES_GetBackup.json

产生以下URL,从该URL下载备份文件,其中我只需要第3个命令使用的文件;

{
  "FilesOnly": "https://schnapps-prod-2-eu-central-1.mendix.com/v1/service_instances/a9510a6c-25e3-4f22-ab42-9ffc71e6a352/snapshots/f6bf15dc-0f7a-483a-b1f1-dc957de921b8/download?content=files_only&expire=20190709055628&signature=6AE5E5BC1D870C79C3373F309F6F9EA3E541C337F0FF26D7A591A231713F3DC4D4F0C51FCFCFF88FA4D1263307A7DE7E",
  "DatabaseOnly": "https://schnapps-prod-2-eu-central-1.mendix.com/v1/service_instances/a9510a6c-25e3-4f22-ab42-9ffc71e6a352/snapshots/f6bf15dc-0f7a-483a-b1f1-dc957de921b8/download?content=database_only&expire=20190709055628&signatA7C9D3014D2D2E582FBFA90ED85884F15648241F42",
  "DatabaseAndFiles": "https://schnapps-prod-2-eu-central-1.mendix.com/v1/service_instances/a9510a6c-25e3-4f22-ab42-9ffc71e6a352/snapshots/f6bf15dc-0f7a-483a-b1f1-dc957de921b8/download?content=files_and_database&expire=20190709055628&signature=A5CAF4422A6A3BFE1CB3B0DCF7C930954F1D992D3D756F5EA396752D0F37CDEBF659DD5CEDC4FFA8AB3D86BD89C22BE23F3391A5AAEA21102BB6DDDD6BE89765CF51597F8E7F700E1554170F82ED35ED0BA3AE70A13C3FB84356592AD5B6F53B743"
}

3-从URL下载备份文件 ... ????我还不知道如何在cmd中执行此操作... ??

4-通过创建一个新的数据库来恢复postgres备份,然后在那里恢复它

createdb -h localhost -p 5433 -U postgres OC_P_1007
cd C:\Program Files\PostgreSQL\11
pg_restore -h localhost -p 5433 -U postgres -d testdb2 C:\Users\SK\Downloads\1007.db

我的问题是:

1-如何通过使用上一个命令的输入指定后续命令(例如快照的ID和下载链接)来自动执行命令?

2-如何从命令中的URL下载备份文件?

3-如何设置它,使其每天9点执行,而无需任何手动操作?

4-您建议哪种编程/脚本语言是最简单的设置?

1 个答案:

答案 0 :(得分:0)

您的问题不仅限于一个特定的问题,因此我将仅提供此未经测试的示例来回答问题12

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "NAME=sian@kurt.com"
Set "APIK=2c299965-2458-4708-908c-37519859e139"
Set "BURL=https://deploy.x.com/api/1/apps/hdibb/environments/Production/snapshots"

For /F Tokens^=4Delims^=^" %%A In (
    'Curl -H "Username: %NAME%" -H "ApiKey: %APIC%" --data-raw "{\"Comment\":\"SK\"}" %BURL% -X POST 2^>NUL^|FindStr /I "\"SnapshotID\""'
)Do For /F Tokens^=4Delims^=^" %%B In (
    'Curl -H "Username: %NAME%" -H "ApiKey: %APIC%" %BURL%/%%A -X GET 2^>NUL^|FindStr /I "\"FilesOnly\""'
)Do Echo Downloading latest snapshot from %%B . . .&GoTo Next
:Next
Pause
GoTo :EOF

请注意,我已经调整了“创建备份”中的--data-raw选项,因为您似乎在使用单引号而不是两个,但是我觉得双引号做得更好!如果报价无法正常工作,请随意调整报价,也不要忘记检查/调整行456

就目前而言,我只是Echo将下载URL设置到控制台窗口中,一旦您确定了下载方法,便可以用下载命令替换Echo Downloading latest snapshot from %%B . . .