-
Notifications
You must be signed in to change notification settings - Fork 14
233 lines (196 loc) · 8.18 KB
/
backup-restore.yml
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
name: Backup and Restore test
on:
push:
branches:
- main
- noy/feat-restore-database
- test
pull_request:
branches: [ "main" ]
jobs:
backup-restore-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install dependencies
run: |
go get .
pip3 install "sqlglot[rs]" pyarrow pandas
sudo apt-get update
sudo apt-get install --yes --no-install-recommends postgresql-client
# Install DuckDB CLI and extensions
curl -LJO https://github.com/duckdb/duckdb/releases/download/v1.1.3/duckdb_cli-linux-amd64.zip
unzip duckdb_cli-linux-amd64.zip
chmod +x duckdb
sudo mv duckdb /usr/local/bin
duckdb -c 'INSTALL json from core'
wget https://dl.min.io/client/mc/release/linux-amd64/mc
chmod +x mc
sudo mv mc /usr/local/bin/
- name: Build MyDuck
run: go build -v
- name: Start MinIO service
run: |
docker run \
-p 9001:9000 \
-p 9091:9090 \
-e "MINIO_ROOT_USER=minioadmin" \
-e "MINIO_ROOT_PASSWORD=minioadmin" \
-v /Users/neo/minio/data:/data \
--name minio \
--detach=true \
quay.io/minio/minio server /data --console-address ":9090"
# Wait for MinIO to be ready
for i in {1..30}; do
if curl -I http://127.0.0.1:9001/minio/health/live|grep -q '200 OK'; then
break
fi
sleep 1
done
- name: Configure mc Client
run: |
mc alias set myminio http://localhost:9001 minioadmin minioadmin
mc mb myminio/myduck-backup
mc ls myminio
- name: Start Primary PostgreSQL
run: |
docker run -d --name pg-primary \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=testdb \
-p 15432:5432 \
postgres:latest \
-c wal_level=logical \
-c max_wal_senders=30
# Wait for PostgreSQL to be ready
for i in {1..30}; do
if psql "postgres://postgres:password@localhost:15432/testdb" -c "SELECT 1" >/dev/null 2>&1; then
break
fi
sleep 1
done
- name: Configure Primary for Replication
run: |
# Create test data
psql "postgres://postgres:password@localhost:15432/testdb" <<-EOSQL
CREATE TABLE test_table (id int primary key, name text);
INSERT INTO test_table VALUES (1, 'initial data 1'), (2, 'initial data 2');
CREATE PUBLICATION testpub FOR TABLE test_table;
EOSQL
- name: Start MyDuck and Test Initial Replication
run: |
# Start MyDuck
./myduckserver &
sleep 5
# Create subscription
psql -h 127.0.0.1 -p 5432 -U postgres <<-EOSQL
CREATE SUBSCRIPTION testsub
CONNECTION 'dbname=testdb host=localhost port=15432 user=postgres password=password'
PUBLICATION testpub;
EOSQL
sleep 5
# Verify initial data
psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT 1 FROM test_table WHERE id = 1 AND name = 'initial data 1';" | grep -q 1
psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT 1 FROM test_table WHERE id = 2 AND name = 'initial data 2';" | grep -q 1
- name: Test Ongoing Replication
run: |
# Insert new data in primary
psql "postgres://postgres:password@localhost:15432/testdb" \
-c "INSERT INTO test_table VALUES (3, 'new data 3');"
sleep 2
# Verify replication of new data
psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT 1 FROM test_table WHERE id = 3 AND name = 'new data 3';" | grep -q 1
- name: Backup MyDuck and Insert more data into source PG
run: |
psql "postgres://postgres:@127.0.0.1:5432" <<-EOSQL
BACKUP DATABASE myduck TO 's3c://myduck-backup/myduck/myduck.bak'
ENDPOINT = '127.0.0.1:9001'
ACCESS_KEY_ID = 'minioadmin'
SECRET_ACCESS_KEY = 'minioadmin';
EOSQL
sleep 5
mc stat myminio/myduck-backup/myduck/myduck.bak
pkill myduckserver
rm -f ./myduck.db
# Insert more data in primary while MyDuck is down
psql "postgres://postgres:password@localhost:15432/testdb" \
-c "INSERT INTO test_table VALUES (4, 'offline data 4');"
- name: Restore MyDuck at Startup
run: |
# Restart MyDuck
./myduckserver \
--restore-file=s3c://myduck-backup/myduck/myduck.bak \
--restore-endpoint=127.0.0.1:9001 \
--restore-access-key-id=minioadmin \
--restore-secret-access-key=minioadmin &
sleep 10
- name: Test Replication
run: |
# Verify replication catches up
psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT 1 FROM test_table WHERE id = 4 AND name = 'offline data 4';" | grep -q 1
- name: Cleanup
if: always()
run: |
pkill myduckserver || true
rm -f ./myduck.db
- name: Restore MyDuck at Runtime
run: |
# Start MyDuck
./myduckserver &
sleep 5
psql "postgres://postgres:@127.0.0.1:5432" <<-EOSQL
RESTORE DATABASE testdb2 FROM 's3c://myduck-backup/myduck/myduck.bak'
ENDPOINT = '127.0.0.1:9001'
ACCESS_KEY_ID = 'minioadmin'
SECRET_ACCESS_KEY = 'minioadmin';
EOSQL
sleep 10
- name: Test Replication
run: |
# Verify replication catches up
psql -h 127.0.0.1 -p 5432 -U postgres -d testdb2 -c "SELECT 1 FROM test_table WHERE id = 4 AND name = 'offline data 4';" | grep -q 1
- name: Test Multiple Databases
run: |
psql "postgres://postgres:@127.0.0.1:5432" <<-EOSQL
CREATE DATABASE testdb3;
CREATE SCHEMA testdb3.sch3;
USE testdb3.sch3;
CREATE TABLE test_table3 (id int primary key, name text);
INSERT INTO test_table3 VALUES (3, 'initial data 3'), (33, 'initial data 33');
CREATE DATABASE testdb4;
CREATE SCHEMA testdb4.sch4;
USE testdb4.sch4;
CREATE TABLE test_table4 (id int primary key, name text);
INSERT INTO test_table4 VALUES (4, 'initial data 4'), (44, 'initial data 44');
BACKUP DATABASE testdb3 TO 's3c://myduck-backup/myduck/myduck3.bak'
ENDPOINT = '127.0.0.1:9001'
ACCESS_KEY_ID = 'minioadmin'
SECRET_ACCESS_KEY = 'minioadmin';
BACKUP DATABASE testdb4 TO 's3c://myduck-backup/myduck/myduck4.bak'
ENDPOINT = '127.0.0.1:9001'
ACCESS_KEY_ID = 'minioadmin'
SECRET_ACCESS_KEY = 'minioadmin';
RESTORE DATABASE testdb5 FROM 's3c://myduck-backup/myduck/myduck3.bak'
ENDPOINT = '127.0.0.1:9001'
ACCESS_KEY_ID = 'minioadmin'
SECRET_ACCESS_KEY = 'minioadmin';
RESTORE DATABASE testdb6 FROM 's3c://myduck-backup/myduck/myduck4.bak'
ENDPOINT = '127.0.0.1:9001'
ACCESS_KEY_ID = 'minioadmin'
SECRET_ACCESS_KEY = 'minioadmin';
EOSQL
psql -h 127.0.0.1 -p 5432 -U postgres -d testdb5 -c "SELECT 1 FROM sch3.test_table3 WHERE id = 3 AND name = 'initial data 3';" | grep -q 1
psql -h 127.0.0.1 -p 5432 -U postgres -d testdb6 -c "SELECT 1 FROM sch4.test_table4 WHERE id = 44 AND name = 'initial data 44';" | grep -q 1
- name: Cleanup
if: always()
run: |
pkill myduckserver || true
docker rm -f pg-primary minio || true