Skip to content

Commit 95002cf

Browse files
committed
[IMPROVEMENT] Support for DB syncronization over SSH tunnel
Signed-off-by: Simone Conti <[email protected]>
1 parent 6a27620 commit 95002cf

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

mongo-sync

+39
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function cleanup {
1313
rm -rf $TMPDIR
1414
unset TMPDIR
1515
fi
16+
unset TUNNEL_PORT
17+
unset TUNNEL_CREDENTIALS
1618
unset LOCAL_CREDENTIALS
1719
unset REMOTE_CREDENTIALS
1820
}
@@ -114,6 +116,10 @@ function load_configs {
114116
# - $remote_host_port
115117
# - $remote_access_username
116118
# - $remote_access_password
119+
# - $tunnel_on
120+
# - $tunnel_access_username
121+
# - $tunnel_access_port
122+
117123
LOCAL_CREDENTIALS=""
118124
if [[ ! -z $local_access_username ]] ; then
119125
LOCAL_CREDENTIALS="-u $local_access_username -p $local_access_password"
@@ -124,6 +130,9 @@ function load_configs {
124130
REMOTE_CREDENTIALS="-u $remote_access_username -p $remote_access_password"
125131
fi
126132

133+
TUNNEL_CREDENTIALS="$tunnel_access_username@$remote_host_url"
134+
TUNNEL_PORT=27018
135+
127136
TMPDIR=/tmp/"$local_db"/dump
128137
}
129138

@@ -142,6 +151,16 @@ function done_msg {
142151
echo
143152
}
144153

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+
145164

146165
function pull {
147166
banner
@@ -150,6 +169,12 @@ function pull {
150169
fi
151170
load_configs
152171

172+
if [ "$tunnel_on" == true ] ; then
173+
open_tunnel
174+
remote_host_url="localhost"
175+
remote_host_port=$TUNNEL_PORT
176+
fi
177+
153178
echo "Dumping Remote DB to $TMPDIR... "
154179
mongodump \
155180
-h "$remote_host_url":"$remote_host_port" \
@@ -167,6 +192,10 @@ function pull {
167192
--drop > /dev/null
168193
success_msg
169194

195+
if [ "$tunnel_on" == true ] ; then
196+
close_tunnel
197+
fi
198+
170199
cleanup
171200
done_msg
172201
}
@@ -178,6 +207,12 @@ function push {
178207
fi
179208
load_configs
180209

210+
if [ "$tunnel_on" == true ] ; then
211+
open_tunnel
212+
remote_host_url="localhost"
213+
remote_host_port=$TUNNEL_PORT
214+
fi
215+
181216
echo "Dumping Local DB to $TMPDIR... "
182217
mongodump \
183218
--port "$local_host_port" \
@@ -194,6 +229,10 @@ function push {
194229
"$TMPDIR"/"$local_db" --drop > /dev/null
195230
success_msg
196231

232+
if [ "$tunnel_on" == true ] ; then
233+
close_tunnel
234+
fi
235+
197236
cleanup
198237
done_msg
199238
}

0 commit comments

Comments
 (0)