-
Notifications
You must be signed in to change notification settings - Fork 1
112 lines (97 loc) · 3.76 KB
/
Copy pathdb-backup-manual.yml
File metadata and controls
112 lines (97 loc) · 3.76 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
name: Database Backup
permissions:
contents: read
on:
workflow_dispatch:
inputs:
cluster_name:
description: 'PostgreSQL Cluster Name'
required: true
default: 'api-postgres-clone'
type: string
environment:
description: 'Environment to backup'
required: true
default: 'development'
type: choice
options:
- dev
- test
- prod
backup_annotation:
description: 'Backup annotation'
required: true
type: string
default: 'manual_backup'
jobs:
backup-database:
runs-on: ubuntu-latest
permissions:
contents: read
environment:
name: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.environment || (github.ref_name == 'main' && 'prod' || github.ref_name == 'stage' && 'test' || 'dev') }}
outputs:
backup-id: ${{ steps.set-backup-id.outputs.id }}
test-start-time: ${{ steps.set-time.outputs.time }}
steps:
- name: Set environment variables
run: |
echo "NAMESPACE=f3c07a-${{ github.event_name == 'workflow_dispatch' && github.event.inputs.environment || (github.ref_name == 'main' && 'prod' || github.ref_name == 'stage' && 'test' || 'dev') }}" >> $GITHUB_ENV
- name: Set Cluster Name
id: set-cluster
run: |
CLUSTER_NAME='${{ github.event.inputs.cluster_name || 'api-postgres-clone' }}'
echo "cluster_name=$CLUSTER_NAME" >> $GITHUB_OUTPUT
- name: Set Test Start Time
id: set-time
run: |
START_TIME=$(date -u -d '5 minutes ago' "+%Y-%m-%d %H:%M:%S+00")
echo "time=$START_TIME" >> $GITHUB_OUTPUT
- name: Set Backup ID
id: set-backup-id
run: |
BACKUP_ID=$(date '+%Y%m%d-%H%M%S')
echo "id=$BACKUP_ID" >> $GITHUB_OUTPUT
- name: Display Backup ID
run: echo "Generated Backup ID is ${{ steps.set-backup-id.outputs.id }}"
- name: Login to OpenShift
run: |
oc login ${{ secrets.OPENSHIFT_SERVER }} \
--token=${{ secrets.OPENSHIFT_API_TOKEN }} \
--insecure-skip-tls-verify=true
oc project ${{ env.NAMESPACE }}
- name: Configure Manual Backup
run: |
oc patch postgrescluster ${{ steps.set-cluster.outputs.cluster_name }} --type merge \
-p '{"spec":{"backups":{"pgbackrest":{"manual":{"repoName":"repo1","options":["--type=full","--annotation=source=${{ github.event.inputs.backup_annotation }}"]}}}}}'
- name: Trigger Backup
run: |
oc annotate postgrescluster ${{ steps.set-cluster.outputs.cluster_name }} \
--overwrite \
postgres-operator.crunchydata.com/pgbackrest-backup="${{ steps.set-backup-id.outputs.id }}"
- name: Wait for backup completion
run: |
timeout=1200 # 20 minutes timeout
while [ $timeout -gt 0 ]; do
status=$(oc get postgrescluster ${{ steps.set-cluster.outputs.cluster_name }} \
-o jsonpath='{.status.pgbackrest.manualBackup}')
if [ -n "$status" ]; then
echo "Current status: $status"
if echo "$status" | grep -q "${{ steps.set-backup-id.outputs.id }}"; then
if echo "$status" | grep -q '"finished":true'; then
if echo "$status" | grep -q '"succeeded":1'; then
echo "Backup completed successfully"
exit 0
else
echo "Backup failed"
exit 1
fi
fi
fi
fi
echo "Backup in progress..."
sleep 10
timeout=$((timeout-10))
done
echo "Backup timed out"
exit 1