Skip to content

Commit de09c94

Browse files
authored
Merge pull request #15 from itnok/master
Support for synchronization over SSH tunnel and optional auth credentials for both local/remote DB
2 parents 55c2667 + 95002cf commit de09c94

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

config.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ local:
44
db: 'local_db_name'
55
host:
66
port: 27017
7+
access:
8+
username: 'local_mongo_user'
9+
password: 'local_mongo_pass'
710

811
remote:
912
db: 'remote_db_name'
@@ -14,8 +17,14 @@ remote:
1417
username: 'remote_mongo_user'
1518
password: 'remote_mongo_pass'
1619

17-
# For now :local only accepts DB name and port
18-
# Assumes DB is at localhost:port
20+
tunnel:
21+
on: false
22+
access:
23+
username: 'remote_ssh_user'
24+
port: 22
25+
26+
# For now :local just
27+
# assumes DB is at localhost:port
1928

2029
# All of the parameters above are required,
2130
# without them, the script will fail

mongo-sync

+66-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function cleanup {
1313
rm -rf $TMPDIR
1414
unset TMPDIR
1515
fi
16+
unset TUNNEL_PORT
17+
unset TUNNEL_CREDENTIALS
18+
unset LOCAL_CREDENTIALS
19+
unset REMOTE_CREDENTIALS
1620
}
1721

1822
function error {
@@ -105,11 +109,29 @@ function load_configs {
105109
# Loads:
106110
# - $local_db
107111
# - $local_host_port
112+
# - $local_access_username
113+
# - $local_access_password
108114
# - $remote_db
109115
# - $remote_host_url
110116
# - $remote_host_port
111117
# - $remote_access_username
112118
# - $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
113135

114136
TMPDIR=/tmp/"$local_db"/dump
115137
}
@@ -129,6 +151,16 @@ function done_msg {
129151
echo
130152
}
131153

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

133165
function pull {
134166
banner
@@ -137,19 +169,33 @@ function pull {
137169
fi
138170
load_configs
139171

172+
if [ "$tunnel_on" == true ] ; then
173+
open_tunnel
174+
remote_host_url="localhost"
175+
remote_host_port=$TUNNEL_PORT
176+
fi
177+
140178
echo "Dumping Remote DB to $TMPDIR... "
141179
mongodump \
142180
-h "$remote_host_url":"$remote_host_port" \
143181
-d "$remote_db" \
144-
-u "$remote_access_username" \
145-
-p "$remote_access_password" \
182+
$REMOTE_CREDENTIALS \
146183
-o "$TMPDIR" > /dev/null
147184
success_msg
148185

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
151193
success_msg
152194

195+
if [ "$tunnel_on" == true ] ; then
196+
close_tunnel
197+
fi
198+
153199
cleanup
154200
done_msg
155201
}
@@ -161,19 +207,32 @@ function push {
161207
fi
162208
load_configs
163209

210+
if [ "$tunnel_on" == true ] ; then
211+
open_tunnel
212+
remote_host_url="localhost"
213+
remote_host_port=$TUNNEL_PORT
214+
fi
215+
164216
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
166222
success_msg
167223

168224
echo "Overwriting Remote DB with Dump... "
169225
mongorestore \
170226
-h "$remote_host_url":"$remote_host_port" \
171227
-d "$remote_db" \
172-
-u "$remote_access_username" \
173-
-p "$remote_access_password" \
228+
$REMOTE_CREDENTIALS \
174229
"$TMPDIR"/"$local_db" --drop > /dev/null
175230
success_msg
176231

232+
if [ "$tunnel_on" == true ] ; then
233+
close_tunnel
234+
fi
235+
177236
cleanup
178237
done_msg
179238
}

0 commit comments

Comments
 (0)