In order to get the last production database automatically on my local Windows machine I want to automate this process of the following commands:
1 - Create a backup
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
Results in following array response, where I need to capture the head object.snapshot ID for the most recent snahshopID (which will be used in the 2nd command); 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 - Get backup URL
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
Results in the following URL to download the backup file from, where I need the filesonly ur for the 3th command;
{
"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 - Download backup file from URL ...???? I don't know how to do this yet in cmd...??
4 - Restore postgres backup by creating a new db, and then restorig it there
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
My question are :
1 - How can I automate the commands by specifying follow up commands with input from previous command (like the id of the snapshot and the download link)?
2 - How can I download backup file from URL in command?
3 - How can I set up this so that it is executed daily on 9 o clock, without any manual action?
4 - Which programming/scripting language could you advice as the most easiest set up?
Your questions are not limited to a single specific issue, so I will offer this untested example in response to questions 1
and 2
only:
@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
Please note that I have adjusted the --data-raw
option in 'create backup' because you appeared to be using one single quote, as opposed to two , but I have a feeling that doublequotes work better! Feel free to adjust that quoting should it fail to work as expected, and don't forget to check/adjust lines 4
, 5
and 6
too.
For now I am simply Echo
ing the download URL to the console window, once you've decided upon your download method, feel free to replace Echo Downloading latest snapshot from %%B . . .
Echo Downloading latest snapshot from %%B . . .
with your download command.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.