@@ -13,6 +13,10 @@ function cleanup {
13
13
rm -rf $TMPDIR
14
14
unset TMPDIR
15
15
fi
16
+ unset TUNNEL_PORT
17
+ unset TUNNEL_CREDENTIALS
18
+ unset LOCAL_CREDENTIALS
19
+ unset REMOTE_CREDENTIALS
16
20
}
17
21
18
22
function error {
@@ -105,11 +109,29 @@ function load_configs {
105
109
# Loads:
106
110
# - $local_db
107
111
# - $local_host_port
112
+ # - $local_access_username
113
+ # - $local_access_password
108
114
# - $remote_db
109
115
# - $remote_host_url
110
116
# - $remote_host_port
111
117
# - $remote_access_username
112
118
# - $remote_access_password
119
+ # - $tunnel_on
120
+ # - $tunnel_access_username
121
+ # - $tunnel_access_port
122
+
123
+ LOCAL_CREDENTIALS=" "
124
+ if [[ ! -z $local_access_username ]] ; then
125
+ LOCAL_CREDENTIALS=" -u $local_access_username -p $local_access_password "
126
+ fi
127
+
128
+ REMOTE_CREDENTIALS=" "
129
+ if [[ ! -z $remote_access_username ]] ; then
130
+ REMOTE_CREDENTIALS=" -u $remote_access_username -p $remote_access_password "
131
+ fi
132
+
133
+ TUNNEL_CREDENTIALS=" $tunnel_access_username @$remote_host_url "
134
+ TUNNEL_PORT=27018
113
135
114
136
TMPDIR=/tmp/" $local_db " /dump
115
137
}
@@ -129,6 +151,16 @@ function done_msg {
129
151
echo
130
152
}
131
153
154
+ function open_tunnel {
155
+ echo " Connecting through SSH tunnel..."
156
+ ssh -fqTNM -S db-sync-socket -L $TUNNEL_PORT :127.0.0.1:$remote_host_port $TUNNEL_CREDENTIALS -p $tunnel_access_port
157
+ }
158
+
159
+ function close_tunnel {
160
+ echo " Disconnecting from SSH tunnel..."
161
+ ssh -S db-sync-socket -O exit $TUNNEL_CREDENTIALS 2> /dev/null
162
+ }
163
+
132
164
133
165
function pull {
134
166
banner
@@ -137,19 +169,33 @@ function pull {
137
169
fi
138
170
load_configs
139
171
172
+ if [ " $tunnel_on " == true ] ; then
173
+ open_tunnel
174
+ remote_host_url=" localhost"
175
+ remote_host_port=$TUNNEL_PORT
176
+ fi
177
+
140
178
echo " Dumping Remote DB to $TMPDIR ... "
141
179
mongodump \
142
180
-h " $remote_host_url " :" $remote_host_port " \
143
181
-d " $remote_db " \
144
- -u " $remote_access_username " \
145
- -p " $remote_access_password " \
182
+ $REMOTE_CREDENTIALS \
146
183
-o " $TMPDIR " > /dev/null
147
184
success_msg
148
185
149
- echo " Overwriting Local DB... "
150
- mongorestore -d " $local_db " --port " $local_host_port " " $TMPDIR " /" $remote_db " --drop > /dev/null
186
+ echo " Overwriting Local DB with Dump... "
187
+ mongorestore \
188
+ --port " $local_host_port " \
189
+ -d " $local_db " \
190
+ $LOCAL_CREDENTIALS \
191
+ " $TMPDIR " /" $remote_db " \
192
+ --drop > /dev/null
151
193
success_msg
152
194
195
+ if [ " $tunnel_on " == true ] ; then
196
+ close_tunnel
197
+ fi
198
+
153
199
cleanup
154
200
done_msg
155
201
}
@@ -161,19 +207,32 @@ function push {
161
207
fi
162
208
load_configs
163
209
210
+ if [ " $tunnel_on " == true ] ; then
211
+ open_tunnel
212
+ remote_host_url=" localhost"
213
+ remote_host_port=$TUNNEL_PORT
214
+ fi
215
+
164
216
echo " Dumping Local DB to $TMPDIR ... "
165
- mongodump -d " $local_db " --port " $local_host_port " -o " $TMPDIR " > /dev/null
217
+ mongodump \
218
+ --port " $local_host_port " \
219
+ -d " $local_db " \
220
+ $LOCAL_CREDENTIALS \
221
+ -o " $TMPDIR " > /dev/null
166
222
success_msg
167
223
168
224
echo " Overwriting Remote DB with Dump... "
169
225
mongorestore \
170
226
-h " $remote_host_url " :" $remote_host_port " \
171
227
-d " $remote_db " \
172
- -u " $remote_access_username " \
173
- -p " $remote_access_password " \
228
+ $REMOTE_CREDENTIALS \
174
229
" $TMPDIR " /" $local_db " --drop > /dev/null
175
230
success_msg
176
231
232
+ if [ " $tunnel_on " == true ] ; then
233
+ close_tunnel
234
+ fi
235
+
177
236
cleanup
178
237
done_msg
179
238
}
0 commit comments