This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
177 lines (130 loc) · 3.03 KB
/
backup.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
DEFAULT_DB="schulcloud"
DEFAULT_HOST="localhost:27017"
DEFAULT_PATH="backup/"
usage()
{
cat << EOF
Usage: ./backup.sh [opts] <export|import>
OPTIONS:
-h Show this help.
-p Set Path
-c Set Collection
-U Mongo Username
-P Mongo Password
-D Mongo Database
-H Mongo Host String (ex. localhost:27017)
-a As JSON Array
-b Pretty Print
EOF
}
while getopts "hp:c:U:P:H:D:ab" opt; do
case $opt in
h)
usage
exit
;;
p)
BACKUP_PATH_PREFIX="$OPTARG"
;;
c)
COLLECTION="$OPTARG"
;;
U)
USERNAME="$OPTARG"
;;
P)
PASSWORD="$OPTARG"
;;
H)
HOST="$OPTARG"
;;
D)
DB="$OPTARG"
;;
a)
JSON_ARRAY=1
;;
b)
PRETTY_PRINT=1
;;
\?)
echo "Invalid option $opt"
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "$1" ]; then
echo "Usage: ./backup.sh [opts] <export|import>"
exit 1
fi
# Set action
ACTION="$1"
# Fill in default values
if [ -z "$DB" ]; then
DB="$DEFAULT_DB"
fi
if [ -z "$HOST" ]; then
HOST="$DEFAULT_HOST"
fi
if [ -z "$BACKUP_PATH_PREFIX" ]; then
BACKUP_PATH_PREFIX=$(date +%Y_%m_%d_%H_%M_%S)
fi
BACKUP_PATH="$DEFAULT_PATH""$BACKUP_PATH_PREFIX"
CREDENTIALS=""
if [ -n "$USERNAME" ]; then
CREDENTIALS="-u $USERNAME"
fi
if [ -n "$PASSWORD" ]; then
CREDENTIALS="$CREDENTIALS -p $PASSWORD"
fi
STYLE=""
#if [ -n "$JSON_ARRAY" ]; then
# STYLE="--jsonArray "
#fi
if [ -n "$PRETTY_PRINT" ]; then
STYLE="$STYLE --pretty "
fi
# Make connection
CONN="$HOST/$DB"
if [ "$ACTION" = "export" ]; then
if [ ! -z "$COLLECTION" ]; then
DATABASE_COLLECTIONS=$COLLECTION
else
DATABASE_COLLECTIONS=$(mongo $CONN $CREDENTIALS --quiet --eval 'db.getCollectionNames().join(" ")')
fi
mkdir -p $BACKUP_PATH
pushd $BACKUP_PATH 2>/dev/null
for collection in $DATABASE_COLLECTIONS; do
echo "Exporting $DB/$collection into $collection.json"
mongoexport --host $HOST $CREDENTIALS --db $DB --collection $collection --out $collection.json $STYLE >/dev/null
done
elif [ "$ACTION" = "import" ]; then
pushd $BACKUP_PATH >/dev/null
for path in *.json; do
# auf array pru:fen und Parameter dazu
STR=$( head -n1 $path )
if [[ ${STR:0:1} == "[" ]] ; then
ARRAY="--jsonArray"
else
ARRAY=""
fi
if [[ $path == *".secrets."* ]]
then
collection=${path%.secrets.json}
else
collection=${path%.json}
fi
echo "Importing $DB/$collection from $path"
if [ "$PASSWORD" == "" ];
then
mongoimport --host $HOST --db $DB --collection $collection $path $STYLE $ARRAY --drop
else
mongoimport --host $HOST $CREDENTIALS --db $DB --collection $collection $path $STYLE $ARRAY --drop
fi
done
else
echo "Usage: ./backup.sh [opts] <export|import>"
fi
exit 0