-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackup_ansibleforms.sh
55 lines (46 loc) · 2.2 KB
/
backup_ansibleforms.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
(
# Check if both username and password are provided
if [ $# -ne 4 ]; then
echo "Usage: $0 <hostname> <port> <username> <password>"
exit 1
fi
# Input parameters
USERNAME="$3"
PASSWORD="$4"
HOST="$1"
PORT="$2"
# Authentication API Endpoint
BASE_URL="https://$HOST:$PORT/api/v1"
AUTH_URL="$BASE_URL/auth/login"
BACKUP_URL="$BASE_URL/job"
# Step 1: Obtain the authentication token
auth_response=$(curl -s -k -X POST \
-u "$USERNAME:$PASSWORD" \
"$AUTH_URL")
# Extract the token from the response (assuming the token is in JSON format)
token=$(echo "$auth_response" | jq -r '.token')
if [ -z "$token" ]; then
echo "Authentication failed or token not found."
exit 1
fi
# Step 2: Perform the POST request with Bearer token header
JSON_DATA='{"formName":"Backup Ansibleforms","extravars":{"persistent_folder":"/app/dist/persistent","mysql_credential":"__self__"},"credentials":{"mysql_credential":"__self__"}}'
response=$(curl -s -k -X POST \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "$JSON_DATA" \
"$BACKUP_URL")
# Extract status from the response
status=$(echo "$response" | jq -r '.status')
# Check if status is "success"
if [ "$status" == "success" ]; then
# Extract jobid from the response
jobid=$(echo "$response" | jq -r '.data.output.id')
echo "Job ID: $jobid"
else
# Extract error from the response
error=$(echo "$response" | jq -r '.data.error')
echo "Error: $error"
fi
)