-
Notifications
You must be signed in to change notification settings - Fork 23
/
load_mongodump.sh
70 lines (51 loc) · 2 KB
/
load_mongodump.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
#!/bin/bash
# Directory of the script
SCRIPT_DIR="$(dirname "$0")"
# Path to the configuration file (two levels up)
CONFIG_FILE="$SCRIPT_DIR/../../docker-compose.dev.yml"
# Check if the correct number of arguments is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <mongodump-file>"
echo " <mongodump-file> : The path to the MongoDB dump file to be restored."
exit 1
fi
MONGODUMP_FILE=$1
# Print debug information
echo "Script Directory: $SCRIPT_DIR"
echo "Configuration File Path: $CONFIG_FILE"
echo "MongoDump File Path: $MONGODUMP_FILE"
# Check if the provided file exists
if [ ! -f "$MONGODUMP_FILE" ]; then
echo "Error: File '$MONGODUMP_FILE' does not exist."
exit 1
fi
# Check if the configuration file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: Configuration file '$CONFIG_FILE' does not exist."
exit 1
fi
# Print details about the configuration file
echo "Configuration file details:"
ls -l "$CONFIG_FILE"
# Extract the database name from the mongodump file
DB_NAME=$(tar -tf "$MONGODUMP_FILE" | grep '^dump/' | sed 's|^dump/||' | awk -F'/' '{if (NF > 0) {print $1; exit}}')
# Output the database name
echo "$DB_NAME"
if [ -z "$DB_NAME" ]; then
echo "Error: Failed to extract database name from mongodump."
exit 1
fi
echo "Database Name: $DB_NAME"
# Update the docker-compose configuration file with the actual DB_HOST
DB_HOST="mongodb://db/$DB_NAME"
sed -i.bak "s|DB_HOST=.*|DB_HOST=$DB_HOST|" "$CONFIG_FILE"
echo "Updated docker-compose file:"
cat "$CONFIG_FILE"
echo "Copying file to Docker container"
docker cp "$MONGODUMP_FILE" em-public-dashboard-db-1:/tmp
FILE_NAME=$(basename "$MONGODUMP_FILE")
echo "Clearing existing database"
docker exec em-public-dashboard-db-1 bash -c "mongo $DB_NAME --eval 'db.dropDatabase()'"
echo "Restoring the dump from $FILE_NAME to database $DB_NAME"
docker exec -e MONGODUMP_FILE=$FILE_NAME em-public-dashboard-db-1 bash -c "cd /tmp && tar xvf $FILE_NAME && mongorestore -d $DB_NAME dump/$DB_NAME"
echo "Database restore complete."