-
Notifications
You must be signed in to change notification settings - Fork 23
/
dump.sh
executable file
·65 lines (52 loc) · 1.36 KB
/
dump.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
#!/bin/sh
while getopts "d:" option
do
case $option in
d)
# remove trailing slash
DUMP_FOLDER=$(echo $OPTARG | sed 's,/*$,,')
echo "INFO: dump in $DUMP_FOLDER"
echo "$DUMP_FOLDER" | grep -q '^/'
if [ $? -ne 0 ];then
echo "ERROR: dump folder should be given full path"
exit 1
fi
;;
esac
done
# Dump timestamp
ts=$(date -u "+%Y%m%dT%H%M%S")
## Backup DB
# Name of the database to dump
DB="kernel-ci"
# Get db container
db=$(docker ps | grep mongo | awk '{print $1}')
if [ "$db" = "" ];then
echo "No mongo container running => exiting"
exit 1
fi
# Dump database
docker exec $db mongodump -d $DB --archive=/tmp/kernelci-$ts.gz --gzip
# Save on host (/var/lib/kernelci/dump)
docker cp $db:/tmp/kernelci-$ts.gz ${DUMP_FOLDER:-/tmp}
## Backup logs
# 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 "Dump $VOLUMENAME"
if [ -z "$VOLUMENAME" ];then
echo "ERROR: no volume name found"
exit 1
fi
# Run alpine container using logs volume
docker run \
--rm \
-v $VOLUMENAME:/tmp/logs \
-v ${DUMP_FOLDER:-/tmp}:/tmp/dump \
alpine tar -zcvf /tmp/dump/kernelci-logs-$ts.tar.gz /tmp/logs
echo
echo "=> database and log files will be saved in folder ${DUMP_FOLDER:-/tmp}"
echo