@@ -13,6 +13,8 @@ function cleanup {
13
13
rm -rf $TMPDIR
14
14
unset TMPDIR
15
15
fi
16
+ unset TUNNEL_PORT
17
+ unset TUNNEL_CREDENTIALS
16
18
unset LOCAL_CREDENTIALS
17
19
unset REMOTE_CREDENTIALS
18
20
}
@@ -114,6 +116,10 @@ function load_configs {
114
116
# - $remote_host_port
115
117
# - $remote_access_username
116
118
# - $remote_access_password
119
+ # - $tunnel_on
120
+ # - $tunnel_access_username
121
+ # - $tunnel_access_port
122
+
117
123
LOCAL_CREDENTIALS=" "
118
124
if [[ ! -z $local_access_username ]] ; then
119
125
LOCAL_CREDENTIALS=" -u $local_access_username -p $local_access_password "
@@ -124,6 +130,9 @@ function load_configs {
124
130
REMOTE_CREDENTIALS=" -u $remote_access_username -p $remote_access_password "
125
131
fi
126
132
133
+ TUNNEL_CREDENTIALS=" $tunnel_access_username @$remote_host_url "
134
+ TUNNEL_PORT=27018
135
+
127
136
TMPDIR=/tmp/" $local_db " /dump
128
137
}
129
138
@@ -142,6 +151,16 @@ function done_msg {
142
151
echo
143
152
}
144
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
+
145
164
146
165
function pull {
147
166
banner
@@ -150,6 +169,12 @@ function pull {
150
169
fi
151
170
load_configs
152
171
172
+ if [ " $tunnel_on " == true ] ; then
173
+ open_tunnel
174
+ remote_host_url=" localhost"
175
+ remote_host_port=$TUNNEL_PORT
176
+ fi
177
+
153
178
echo " Dumping Remote DB to $TMPDIR ... "
154
179
mongodump \
155
180
-h " $remote_host_url " :" $remote_host_port " \
@@ -167,6 +192,10 @@ function pull {
167
192
--drop > /dev/null
168
193
success_msg
169
194
195
+ if [ " $tunnel_on " == true ] ; then
196
+ close_tunnel
197
+ fi
198
+
170
199
cleanup
171
200
done_msg
172
201
}
@@ -178,6 +207,12 @@ function push {
178
207
fi
179
208
load_configs
180
209
210
+ if [ " $tunnel_on " == true ] ; then
211
+ open_tunnel
212
+ remote_host_url=" localhost"
213
+ remote_host_port=$TUNNEL_PORT
214
+ fi
215
+
181
216
echo " Dumping Local DB to $TMPDIR ... "
182
217
mongodump \
183
218
--port " $local_host_port " \
@@ -194,6 +229,10 @@ function push {
194
229
" $TMPDIR " /" $local_db " --drop > /dev/null
195
230
success_msg
196
231
232
+ if [ " $tunnel_on " == true ] ; then
233
+ close_tunnel
234
+ fi
235
+
197
236
cleanup
198
237
done_msg
199
238
}
0 commit comments