-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-backup.sh
executable file
·97 lines (85 loc) · 2.58 KB
/
test-backup.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
#!/bin/bash
#
# Simple test script for backup and restore
#
# Before running it, ensure there is a file test-env.txt
# with configuration options as in test-env.txt.sample
set -e
# build dockup image
docker build -t wetransform/dockup-mongo:local .
MONGODB_PASS="supersecret"
MONGODB_USER="admin"
MONGODB_AUTH_DB="admin"
# run MongoDB
docker stop dockup-mongo-test
docker rm dockup-mongo-test
docker run -d --name dockup-mongo-test \
-e MONGODB_PASS=$MONGODB_PASS \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_DATABASE=$MONGODB_AUTH_DB \
stempler/mongodb:3.2
# wait for MongoDB to be ready (TODO better way to wait?)
sleep 10
# create dummy content to backup
file_time=`date +%Y-%m-%d\\ %H:%M:%S\\ %Z`
docker exec dockup-mongo-test mongo -u $MONGODB_USER \
-p $MONGODB_PASS --authenticationDatabase $MONGODB_AUTH_DB \
test \
--eval "db.createCollection('test'); db.test.insert({created: \"$file_time\"});"
rc=$?; if [ $rc -ne 0 ]; then
echo "ERROR: Error creating dummy content for database"
docker stop dockup-mongo-test
docker rm dockup-mongo-test
exit $rc
fi
# backup
docker run --rm \
--env-file test-env.txt \
--link dockup-mongo-test:mongodb \
-e BACKUP_NAME=dockup-mongo-test \
-e MONGODB_HOST=mongodb \
-e MONGODB_PORT=27017 \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_PASS=$MONGODB_PASS \
--name dockup-run-test wetransform/dockup-mongo:local
rc=$?; if [ $rc -ne 0 ]; then
echo "ERROR: Error running backup"
exit $rc
fi
# recreate MongoDB container
docker stop dockup-mongo-test
docker rm dockup-mongo-test
docker run -d --name dockup-mongo-test \
-e MONGODB_PASS=$MONGODB_PASS \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_DATABASE=$MONGODB_AUTH_DB \
stempler/mongodb:3.2
# wait for MongoDB to be ready (TODO better way to wait?)
sleep 10
# restore
docker run --rm \
--env-file test-env.txt \
--link dockup-mongo-test:mongodb \
-e BACKUP_NAME=dockup-mongo-test \
-e MONGODB_HOST=mongodb \
-e MONGODB_PORT=27017 \
-e MONGODB_USER=$MONGODB_USER \
-e MONGODB_PASS=$MONGODB_PASS \
-e RESTORE=true \
--name dockup-run-test wetransform/dockup-mongo:local
rc=$?; if [ $rc -ne 0 ]; then
echo "ERROR: Error running restore"
docker stop dockup-mongo-test
docker rm dockup-mongo-test
exit $rc
fi
RESULT=$(docker exec dockup-mongo-test mongo -u $MONGODB_USER \
-p $MONGODB_PASS --authenticationDatabase $MONGODB_AUTH_DB \
test \
--eval "db.test.find({}).pretty()" | grep created)
if [ "$RESULT" == "\"created\" : \"$file_time\"" ]; then
echo "ERROR: Backup did not restore entry"
exit $rc
else
echo "Restored database/entry successfully"
fi