-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_prod_db.sh
executable file
·64 lines (47 loc) · 1.65 KB
/
get_prod_db.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/bash
CONTAINER=grapa_db
SERVICE_NAME=db
DB_NAME=postgres
GRAPA_FILE_NAME=grapa.sql.gz
SERVER=svm-116.cs.helsinki.fi
SERVER_PATH=/home/toska_user/most_recent_backup_store/
SERVER_FILE=${SERVER_PATH}${GRAPA_FILE_NAME}
PROJECT_ROOT=$(dirname $(dirname $(realpath "$0")))
BACKUPS=$PROJECT_ROOT/backups/
DOCKER_COMPOSE=$PROJECT_ROOT/docker-compose.yml
USER_DATA_FILE_PATH=$PROJECT_ROOT/scripts/my_username
username=""
retry () {
for i in {1..60}
do
$@ && break || echo "Retry attempt $i failed, waiting..." && sleep 3;
done
}
get_username() {
# Check if username has already been set
[ -z "$username" ]|| return 0
# Check if username is saved to data file and ask it if not
if [ ! -f "$USER_DATA_FILE_PATH" ]; then
echo ""
echo "!! No previous username data found. Will ask it now !!"
echo "Enter your Uni Helsinki username:"
read username
echo $username > $USER_DATA_FILE_PATH
echo "Succesfully saved username"
echo ""
fi
# Set username
username=$(cat $USER_DATA_FILE_PATH | head -n 1)
}
echo "Creating backups folder"
mkdir -p ${BACKUPS}
echo "Fetching a new dump"
get_username
scp -r -o ProxyCommand="ssh -l $username -W %h:%p melkki.cs.helsinki.fi" $username@$SERVER:$SERVER_FILE $BACKUPS
echo "Removing database and related volume"
docker-compose -f $DOCKER_COMPOSE down -v
echo "Starting postgres in the background"
docker-compose -f $DOCKER_COMPOSE up -d $SERVICE_NAME $JAMI_DB
retry docker-compose -f $DOCKER_COMPOSE exec $SERVICE_NAME pg_isready --dbname=$DB_NAME
echo "Populating database"
docker exec -i $CONTAINER /bin/bash -c "gunzip | psql -U postgres" < ${BACKUPS}${GRAPA_FILE_NAME} 2> /dev/null