forked from Percona-QA/percona-qa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_backup_tests.sh
executable file
·264 lines (216 loc) · 10.5 KB
/
docker_backup_tests.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/bash
##########################################################################
# Created By Manish Chawla, Percona LLC #
# This script tests backup for docker containers #
# Usage: #
# 1. Run the script as: ./docker_backup_tests.sh pxb24/pxb8 main/testing #
# 3. Logs are available in: $PWD/backup_log #
##########################################################################
if [ "$#" -ne 2 ]; then
echo "Please run the script with parameters: <version as pxb24/pxb8> <repo as main/testing>"
echo "Main repo is the percona docker image and testing repo is the perconalab docker image"
exit 1
fi
clean_setup() {
# This function checks and cleans the setup
if [ "$1" = "pxb8" ]; then
if [ "$(sudo docker ps -a | grep mysql-8.0)" ]; then
sudo docker stop mysql-8.0 >/dev/null 2>&1
sudo docker rm mysql-8.0 >/dev/null 2>&1
fi
else
if [ "$(sudo docker ps -a | grep mysql-5.7)" ]; then
sudo docker stop mysql-5.7 >/dev/null 2>&1
sudo docker rm mysql-5.7 >/dev/null 2>&1
fi
fi
if [ -d /tmp/mysql_data ]; then
sudo rm -r /tmp/mysql_data
fi
echo "Removing all images and volumes not being used by any container" >>backup_log
sudo docker image prune -a -f >>backup_log
sudo docker volume prune -f >>backup_log
}
test_pxb8_docker() {
# This function runs tests for pxb 8.0 and ms 8.0 docker image
mkdir /tmp/mysql_data
echo "Run mysql 8.0 docker container"
if ! sudo docker run --name mysql-8.0 -v /tmp/mysql_data:/var/lib/mysql -p 3306:3306 -p 3060:3060 -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='mysql' -d mysql/mysql-server:8.0 >>backup_log 2>&1; then
echo "ERR: The docker command to start mysql 8.0 failed"
exit 1
fi
echo "Waiting for mysql to start..."
for ((i=1; i<=180; i++)); do
if ! sudo docker ps -a|grep mysql-8.0|grep healthy >/dev/null 2>&1; then
sleep 1
else
break
fi
if [[ $i -eq 180 ]]; then
echo "ERR: The mysql server failed to start in docker container"
exit 1
fi
done
echo -n "Mysql started with version: "
sudo docker exec -it mysql-8.0 mysql -uroot -pmysql -Bse "SELECT @@version;" |grep -v "Using a password"
echo "Add data in the database"
sudo docker exec -it mysql-8.0 mysql -uroot -pmysql -e "CREATE DATABASE IF NOT EXISTS test;" >/dev/null 2>&1
sudo docker exec -it mysql-8.0 mysql -uroot -pmysql -e "CREATE TABLE test.t1(i INT);" >/dev/null 2>&1
sudo docker exec -it mysql-8.0 mysql -uroot -pmysql -e "INSERT INTO test.t1 VALUES (1), (2), (3), (4), (5);" >/dev/null 2>&1
echo "Run pxb 8.0 docker container, take backup and prepare it"
if [[ "$1" = "main" ]]; then
echo "Using main repo docker image"
sudo docker run --volumes-from mysql-8.0 -v pxb_backup_data:/backup -it --rm --user root percona/percona-xtrabackup /bin/bash -c "xtrabackup --backup --datadir=/var/lib/mysql/ --target-dir=/backup --user=root --password=mysql ; xtrabackup --prepare --target-dir=/backup" >>backup_log 2>&1
else
echo "Using testing repo docker image"
sudo docker run --volumes-from mysql-8.0 -v pxb_backup_data:/backup -it --rm --user root perconalab/percona-xtrabackup:8.0 /bin/bash -c "xtrabackup --backup --datadir=/var/lib/mysql/ --target-dir=/backup --user=root --password=mysql ; xtrabackup --prepare --target-dir=/backup" >>backup_log 2>&1
fi
if [ "$?" -ne 0 ]; then
echo "ERR: The docker command to run pxb 8.0 failed"
exit 1
else
echo "The backup and prepare was successful. Log available at: ${PWD}/backup_log"
fi
echo "Stop the mysql-8.0 docker container"
sudo docker stop mysql-8.0 >>backup_log 2>&1
sudo rm -r /tmp/mysql_data
mkdir /tmp/mysql_data
echo "Run pxb 8.0 docker container to restore the backup"
if [[ "$1" = "main" ]]; then
echo "Using main repo docker image"
sudo docker run --volumes-from mysql-8.0 -v pxb_backup_data:/backup -it --rm --user root percona/percona-xtrabackup /bin/bash -c "xtrabackup --copy-back --datadir=/var/lib/mysql/ --target-dir=/backup" >>backup_log 2>&1
else
echo "Using testing repo docker image"
sudo docker run --volumes-from mysql-8.0 -v pxb_backup_data:/backup -it --rm --user root perconalab/percona-xtrabackup:8.0 /bin/bash -c "xtrabackup --copy-back --datadir=/var/lib/mysql/ --target-dir=/backup" >>backup_log 2>&1
fi
if [ "$?" -ne 0 ]; then
echo "ERR: The docker command to restore the data failed"
exit 1
else
echo "The restore command was successful"
fi
sudo chmod -R 777 /tmp/mysql_data
echo "Start the mysql 8.0 container with the restored data"
if ! sudo docker start mysql-8.0 >>backup_log 2>&1; then
echo "ERR: The docker command to start mysql 8.0 with the restored data failed"
exit 1
fi
echo "Waiting for mysql to start..."
for ((i=1; i<=180; i++)); do
if ! sudo docker ps -a | grep mysql-8.0 | grep healthy >/dev/null 2>&1; then
sleep 1
else
break
fi
if [[ $i -eq 180 ]]; then
echo "ERR: The mysql server failed to start with the restored data in the docker container"
exit 1
fi
done
if [ "$(sudo docker exec -it mysql-8.0 mysql -uroot -pmysql -Bse 'SELECT * FROM test.t1;' | grep -v password | wc -l)" != "5" ]; then
echo "ERR: Data could not be checked in the mysql container"
else
echo "Data was restored successfully"
fi
# Cleanup
echo "Stopping and removing mysql-8.0 docker container"
sudo docker stop mysql-8.0 >>backup_log 2>&1
sudo docker rm mysql-8.0 >>backup_log 2>&1
}
test_pxb24_docker() {
# This function runs tests for pxb 2.4 and ms 5.7 docker image
mkdir /tmp/mysql_data
echo "Run mysql 5.7 docker container"
if ! sudo docker run --name mysql-5.7 -v /tmp/mysql_data:/var/lib/mysql -p 3306:3306 -p 3060:3060 -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='mysql' -d mysql/mysql-server:5.7 >>backup_log 2>&1; then
echo "ERR: The docker command to start mysql 5.7 failed"
exit 1
fi
echo "Waiting for mysql to start..."
for ((i=1; i<=180; i++)); do
if ! sudo docker ps -a|grep mysql-5.7|grep healthy >/dev/null 2>&1; then
sleep 1
else
break
fi
if [[ $i -eq 180 ]]; then
echo "ERR: The mysql server failed to start in docker container"
exit 1
fi
done
echo -n "Mysql started with version: "
sudo docker exec -it mysql-5.7 mysql -uroot -pmysql -Bse "SELECT @@version;" |grep -v "Using a password"
echo "Add data in the database"
sudo docker exec -it mysql-5.7 mysql -uroot -pmysql -e "CREATE DATABASE IF NOT EXISTS test;" >/dev/null 2>&1
sudo docker exec -it mysql-5.7 mysql -uroot -pmysql -e "CREATE TABLE test.t1(i INT);" >/dev/null 2>&1
sudo docker exec -it mysql-5.7 mysql -uroot -pmysql -e "INSERT INTO test.t1 VALUES (1), (2), (3), (4), (5);" >/dev/null 2>&1
echo "Run pxb 2.4 docker container, take backup and prepare it"
if [[ "$1" = "main" ]]; then
echo "Using main repo docker image"
sudo docker run --volumes-from mysql-5.7 -v pxb_backup_data:/backup -it --rm percona/percona-xtrabackup:2.4 /bin/bash -c "xtrabackup --backup --datadir=/var/lib/mysql/ --target-dir=/backup --user=root --password=mysql ; xtrabackup --prepare --target-dir=/backup" >>backup_log 2>&1
else
echo "Using testing repo docker image"
sudo docker run --volumes-from mysql-5.7 -v pxb_backup_data:/backup -it --rm perconalab/percona-xtrabackup:2.4 /bin/bash -c "xtrabackup --backup --datadir=/var/lib/mysql/ --target-dir=/backup --user=root --password=mysql ; xtrabackup --prepare --target-dir=/backup" >>backup_log 2>&1
fi
if [ "$?" -ne 0 ]; then
echo "ERR: The docker command to run pxb 5.7 failed"
exit 1
else
echo "The backup and prepare was successful. Log available at: $HOME/backup_log"
fi
echo "Stop the mysql-5.7 docker container"
sudo docker stop mysql-5.7 >>backup_log 2>&1
sudo rm -r /tmp/mysql_data
mkdir /tmp/mysql_data
echo "Run pxb 5.7 docker container to restore the backup"
if [[ "$1" = "main" ]]; then
echo "Using main repo docker image"
sudo docker run --volumes-from mysql-5.7 -v pxb_backup_data:/backup -it --rm percona/percona-xtrabackup:2.4 /bin/bash -c "xtrabackup --copy-back --datadir=/var/lib/mysql/ --target-dir=/backup" >>backup_log 2>&1
else
echo "Using testing repo docker image"
sudo docker run --volumes-from mysql-5.7 -v pxb_backup_data:/backup -it --rm perconalab/percona-xtrabackup:2.4 /bin/bash -c "xtrabackup --copy-back --datadir=/var/lib/mysql/ --target-dir=/backup" >>backup_log 2>&1
fi
if [ "$?" -ne 0 ]; then
echo "ERR: The docker command to restore the data failed"
exit 1
else
echo "The restore command was successful"
fi
sudo chmod -R 777 /tmp/mysql_data
echo "Start the mysql 5.7 container with the restored data"
if ! sudo docker start mysql-5.7 >>backup_log 2>&1; then
echo "ERR: The docker command to start mysql 5.7 with the restored data failed"
exit 1
fi
echo "Waiting for mysql to start..."
for ((i=1; i<=180; i++)); do
if ! sudo docker ps -a | grep mysql-5.7 | grep healthy >/dev/null 2>&1; then
sleep 1
else
break
fi
if [[ $i -eq 180 ]]; then
echo "ERR: The mysql server failed to start with the restored data in the docker container"
exit 1
fi
done
if [ "$(sudo docker exec -it mysql-5.7 mysql -uroot -pmysql -Bse 'SELECT * FROM test.t1;' | grep -v password | wc -l)" != "5" ]; then
echo "ERR: Data could not be checked in the mysql container"
else
echo "Data was restored successfully"
fi
# Cleanup
echo "Stopping and removing mysql-5.7 docker container"
sudo docker stop mysql-5.7 >>backup_log 2>&1
sudo docker rm mysql-5.7 >>backup_log 2>&1
}
>backup_log
# Check and clean existing installation
clean_setup "$1"
if [ "$1" = "pxb8" ]; then
test_pxb8_docker "$2" | tee -a backup_log
else
test_pxb24_docker "$2" | tee -a backup_log
fi
# Clean up
clean_setup "$1"
echo "Logs for the tests are available at: $PWD/backup_log"