forked from blueimp/shell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongorestore.sh
More file actions
executable file
·91 lines (75 loc) · 1.96 KB
/
mongorestore.sh
File metadata and controls
executable file
·91 lines (75 loc) · 1.96 KB
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
#!/bin/sh
# shellcheck shell=dash
#
# Run mongorestore in a docker container.
# Usage: MONGODB_CONTAINER=CONTAINER_ID ./mongorestore.sh [ARGS...]
#
# Copyright 2015, Sebastian Tschan
# https://blueimp.net
#
# Licensed under the MIT license:
# https://opensource.org/licenses/MIT
#
# Exit immediately if a command exits with a non-zero status:
set -e
MONGODB_USER="${MONGODB_USER:-mongodb}"
if [ -z "$MONGODB_CONTAINER" ]; then
echo "Usage: MONGODB_CONTAINER=CONTAINER_ID $0" >&2
exit 1
fi
if [ "$1" = "--help" ] || [ "$1" = "--version" ]; then
docker exec -u "$MONGODB_USER" "$MONGODB_CONTAINER" mongorestore "$@"
exit $?
fi
TMP_DIR="$(mktemp -d /tmp/mongodb-dump-XXXXXXXXXX)"
SCRIPTDIR="$(cd "$(dirname "$0")" && pwd)"
replace() {
if [ -f "$SCRIPTDIR"/replace.sh ]; then
"$SCRIPTDIR"/replace.sh "$@"
else
cat
fi
}
clean_exit() {
local status=$?
rm -rf "$TMP_DIR"
exit $status
}
# Clean up on exit:
trap 'clean_exit' INT TERM
HOSTDIR=""
RESET=""
COUNT=$#
INDEX=0
# Loop over the arguments list to rebuild it:
for arg; do
if [ -z "$RESET" ]; then
# Reset the arguments list at the start of the loop:
set --
RESET="true"
fi
INDEX=$((INDEX+1))
if [ "$INDEX" = "$COUNT" ] && [ -d "$arg" ]; then
HOSTDIR="$arg"
# Replace the dump source with the temp dir:
arg="$TMP_DIR"
fi
# Rebuild the arguments list with each iteration:
set -- "$@" "$arg"
done
if [ -z "$HOSTDIR" ]; then
# Use the default dump target as host dir:
HOSTDIR="$PWD/dump"
# Set the temp dir as dump source:
set -- "$@" "$TMP_DIR"
fi
cd "$HOSTDIR"
# Import the dump data into the running mongodb container:
docker exec "$MONGODB_CONTAINER" mkdir -p "$TMP_DIR"
docker cp . "$MONGODB_CONTAINER":"$TMP_DIR"
# Restore the imported dump data
# and replace the temp dir with the host dir in the stderr output:
{ docker exec -u "$MONGODB_USER" "$MONGODB_CONTAINER" mongorestore "$@" 2>&3; \
} 3>&1 1>&2 | replace "$TMP_DIR" "$HOSTDIR" 1>&2
docker exec "$MONGODB_CONTAINER" rm -rf "$TMP_DIR"
clean_exit