-
Notifications
You must be signed in to change notification settings - Fork 23
/
restore.sh
executable file
·74 lines (65 loc) · 1.92 KB
/
restore.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
#!/bin/bash
while getopts "d:l:" option
do
case $option in
d) DB_DUMP_FILE=$OPTARG;;
l)
LOG_DUMP_FILE=$OPTARG
echo "$LOG_DUMP_FILE" | grep -q '^/'
if [ $? -ne 0 ];then
echo "ERROR: log dump should be given full path"
exit 1
fi
;;
esac
done
if [ ! "$DB_DUMP_FILE" ] && [ ! "$LOG_DUMP_FILE" ];then
echo "Usage: $0 -d DATABASE_BACKUP.gz -l LOG_BACKUP.tar.gz"
fi
if [ ! -z "$DB_DUMP_FILE" ];then
echo "-->Restoring Mongo database from $DB_DUMP_FILE"
## Get db container
ID=$(docker ps | grep mongo | cut -d" " -f1)
if [ -z "$ID" ];then
echo "--Container for service kernelci_mongo not found exiting--"
exit 1
fi
echo $DB_DUMP_FILE |grep -q 'gz$'
if [ $? -eq 0 ];then
gunzip --keep $DB_DUMP_FILE|| exit 1
DB_DUMP_FILE=$(echo $DB_DUMP_FILE | sed 's,.gz$,,')
fi
## Copy dump to container
docker cp $DB_DUMP_FILE $ID:/tmp || exit 1
# handle if DB_DUMP_FILE was in another folder
DB_DUMP_FILE=$(basename $DB_DUMP_FILE)
## Restore dump
echo "Restore $DB_DUMP_FILE"
docker exec $ID /bin/bash -c "mongorestore --archive=/tmp/$DB_DUMP_FILE --drop"
if [ $? -ne 0 ];then
exit 1
fi
fi
if [ ! -z "$LOG_DUMP_FILE" ];then
# sanity check if volume exists
if [ $(docker volume ls | awk '{print $2}' |grep _kci$ | wc -l) -ge 2 ];then
echo "ERROR: too many kci dava volumes"
exit 1
fi
VOLUMENAME=$(docker volume ls | awk '{print $2}' |grep _kci$)
echo "Restore on $VOLUMENAME"
if [ -z "$VOLUMENAME" ];then
echo "ERROR: no volume name found"
exit 1
fi
echo "-->Restoring logs database from $LOG_DUMP_FILE"
mkdir kernelci-logs
cd kernelci-logs
## Restore logs
tar xvf $LOG_DUMP_FILE || exit 1
docker run --rm \
-v $(pwd):/tmp/kernelci_logs/ \
-v $VOLUMENAME:/var/lib/docker/volumes/kernelci_kci/_data \
busybox cp -r /tmp/kernelci_logs/tmp/logs/. /var/lib/docker/volumes/kernelci_kci/_data
fi
exit 0