Skip to content

Commit 4ae3c07

Browse files
authored
Merge pull request #16 from weaponsforge/feature/weaponsforge-15
feat: display the database list, #15
2 parents f16689a + aaa9aa3 commit 4ae3c07

File tree

2 files changed

+101
-23
lines changed

2 files changed

+101
-23
lines changed

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
> - Imports `(mongorestore)` the exported binary database into a new database.
77
> - Drops a specified localhost database.
88
9+
### Table of Contents
10+
11+
- [Dependencies](#dependencies)
12+
- [Content](#content)
13+
- [Usage and Installation](#usage-and-installation)
14+
- [Troubleshooting](#troubleshooting)
15+
916

1017

1118
### Dependencies
@@ -64,7 +71,7 @@ Version is not strict, but for reference, the project used **MongoDB Community S
6471
Windows batch script to automate switching of MongoDB database credentials for a faster database export `(mongodump)`, import `(mongorestore)` and database drop process.
6572

6673

67-
## Usage
74+
## Usage and Installation
6875

6976
1. Clone this repository.
7077
`git clone https://github.com/weaponsforge/mongo-importex.git`
@@ -81,23 +88,29 @@ Windows batch script to automate switching of MongoDB database credentials for a
8188
| 4 | Enter user name | MongoDB user name |
8289
| 5 | Enter password | MongoDB user password |
8390
| 6 | Enter mongo shell | The available and active MongoDB shell. Choose:<br> - `mongo` (for MongoDB v4.2.0 or lower versions) <br>- `mongosh` (for MongoDB v4.4.0 or higher versions)|
84-
| 7 | Save | Saves the encoded database credentials for future use to a `.env` and exit to the main screen. |
85-
| 8 | Save and Export Database | Saves the mongodb settings to a `.env` file and starts the Export Database process. |
86-
| 9 | Save and Import Database | Saves the mongodb settings to a `.env` file and starts to the Import Database process. |
87-
| 10 | Export Database | Exports (`mongodump`) Exports the database defined in the MongoDB Connection Credentials Setup to a database in binary JSON format.<br> - **NOTE:** Choosing this option will NOT save recent updates made to the database credentials. |
88-
| 11 | Import Database | Imports (`mongorestore`) a binary database to the database defined in the MongoDB Connection Credentials settings.<br>It expects to find binary database contents in subdirectories relative to the script, similar to the output of the **[10] Export Database option**.<br>**NOTE:** Choosing this option will NOT save recent updates made to the database credentials. |
91+
| 7 | Save | Saves the encoded database credentials for future use to a `.env` file and exit to the main screen. |
92+
| 8 | Save and Export Database | Saves the mongodb settings to a `.env` file and starts the **Export Database** process. |
93+
| 9 | Save and Import Database | Saves the mongodb settings to a `.env` file and starts to the **Import Database** process. |
94+
| 10 | Export Database | Exports (`mongodump`) the database defined in the MongoDB Connection Credentials Setup to a database in binary JSON format.<br> - **NOTE:** Choosing this option will NOT save recent updates made to the database credentials.<br>- Type `"Y"` and press ENTER if you want to use the `"mongo+srv://"` SRV Connection String.<br>- Type `"n"` and press ENTER if you want to use the `"mongo://"` Standard Connection String. |
95+
| 11 | Import Database | Imports (`mongorestore`) a binary database to the database defined in the MongoDB Connection Credentials settings.<br>It expects to find binary database contents in subdirectories relative to the script, similar to the output of the **[10] Export Database option**.<br>- **NOTE:** Choosing this option will NOT save recent updates made to the database credentials.<br>- Type `"Y"` and press ENTER if you want to use the `"mongo+srv://"` SRV Connection String.<br>- Type `"n"` and press ENTER if you want to use the `"mongo://"` Standard Connection String. |
8996
| x | Exit | Exit the script. |
9097

91-
4. The **VIEWING THE [ACTIVE] MONGODB CONNECTION CREDENTIALS** screen provides quick links for database import, export, drop, and database credentials updating, which will be accessible after saving the initial database credentials required from the **MONGODB CONNECTION CREDENTIALS SETUP**.
98+
4. The **VIEWING THE [ACTIVE] MONGODB CONNECTION CREDENTIALS** screen provides quick links for database import, export, drop, and database credentials updating, which will be accessible after saving the initial database credentials required from the **MONGODB CONNECTION CREDENTIALS SETUP**.<br>
99+
100+
The **Export Database**, **Import Database**, and **List Databases** options will prompt selecting the `"mongo+srv://"` SRV Connection String. Type `"Y"` or `"n"` and press ENTER when prompted.
101+
- Type `"Y"` and press ENTER if you want to use the `"mongo+srv://"` SRV Connection String.
102+
- Type `"n"` and press ENTER if you want to use the `"mongo://"` Standard Connection String.
92103

93104
| NO. | Prompt | Description |
94105
|---|---|---|
95106
| 1 | Export Database | Exports (`mongodump`) the database defined in the MongoDB Connection Credentials Setup to a binary JSON format database. |
96107
| 2 | Import Database | Imports (`mongorestore`) the database defined in the MongoDB Connection Credentials settings to a local binary JSON format database relative to the script's location |
97108
| 3 | Drop Database | It deletes a database defined in the MongoDB connection credentials. Currently available only for localhost MongoDB. |
98-
| 4 | Update Connection Credentials | Displays the **MONGODB CONNECTION CREDENTIALS SETUP** screen for editing the stored database connection details. |
99-
| 5 | Reset | Resets the database conection details |
100-
| 6 | Exit | Exit the script. |
109+
| 4 | List Databases | Lists the available databases from the defined MongoDB connection credentials. |
110+
| 5 | List Local Databases | Lists the available localhost databases. |
111+
| 6 | Update Connection Credentials | Displays the **MONGODB CONNECTION CREDENTIALS SETUP** screen for editing the stored database connection details. |
112+
| 7 | Reset | Resets the database conection details |
113+
| x | Exit | Exit the script. |
101114

102115

103116
## Troubleshooting

main.bat

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ GoTo Main
2121
:: Legacy MongoDB uses "mongo" shell, newer versions use "mongosh"
2222
set "MONGO_SHELL="
2323

24+
:: Flag to use the "mongodb+srv://" SRV Connection String URI
25+
set "USESRV="
26+
27+
:: User authentication database if using the Standard Connection String "mongo://"
28+
set "AUTHSOURCE="
29+
2430
:: Screen ID's
2531
set /A _ViewDatabaseCredentials=1
2632
set /A _SetDatabaseCredentials=2
@@ -58,8 +64,10 @@ EXIT /B 0
5864
echo [1] Export Database
5965
echo [2] Import Database
6066
echo [3] Drop Database
61-
echo [4] Update Connection Credentials
62-
echo [5] Reset
67+
echo [4] List Databases
68+
echo [5] List Local Databases
69+
echo [6] Update Connection Credentials
70+
echo [7] Reset
6371
echo [x] Exit
6472
set "choice=-1"
6573
echo.
@@ -68,8 +76,16 @@ EXIT /B 0
6876
if %choice% EQU 1 GoTo ExportDatabase
6977
if %choice% EQU 2 GoTo SelectDatabaseToImport
7078
if %choice% EQU 3 Goto DeleteDatabase
71-
if %choice% EQU 4 Goto SetDatabaseCredentials
72-
if %choice% EQU 5 Goto ResetData
79+
if %choice% EQU 4 (
80+
set /A NextScreen=_ViewDatabaseCredentials
81+
Goto ShowConnectionDatabases
82+
)
83+
if %choice% EQU 5 (
84+
set /A NextScreen=_ViewDatabaseCredentials
85+
Goto ShowDatabases
86+
)
87+
if %choice% EQU 6 Goto SetDatabaseCredentials
88+
if %choice% EQU 7 Goto ResetData
7389
if %choice% == x EXIT /B 0
7490

7591
Goto ViewDatabaseCredentials
@@ -79,18 +95,17 @@ EXIT /B 0
7995
:: Export (mongodump) the target database using current db credentials
8096
:ExportDatabase
8197
set "continue="
82-
set "usesrv="
8398
set /p continue=Are you ready to start the database export? [Y/n]:
8499

85100
(if "%continue%" == "Y" (
86-
set /p usesrv=Do you want to use the mongodb+srv:// connection string? [Y/n]:
101+
CALL :ConfirmUseSrv
87102
))
88103

89104
:: Export the database given the connection
90105
echo.%continue% | findstr /C:"Y">nul && (
91106
echo Starting database export...
92107

93-
echo.%usesrv% | findstr /C:"Y">nul && (
108+
echo.%USESRV% | findstr /C:"Y">nul && (
94109
mongodump --uri mongodb+srv://%MONGO_USER%:%MONGO_PASSWORD%@%MONGO_HOST%/%MONGO_DB% -o %cd%
95110
) || (
96111
(if %MONGO_PORT% EQU 20717 (
@@ -157,19 +172,17 @@ EXIT /B 0
157172
echo - Password: %MONGO_PASSWORD%
158173

159174
set "con="
160-
set "usesrv="
161-
162175
echo Are you sure you want to import [%db%]
163176
set /p con=to the above database server? [Y/n]:
164177

165178
(if "%con%" == "Y" (
166-
set /p usesrv=Do you want to use the mongodb+srv:// connection string? [Y/n]:
179+
CALL :ConfirmUseSrv
167180
))
168181

169182
echo.%con% | findstr /C:"Y">nul && (
170183
echo Starting database import...
171184

172-
echo.%usesrv% | findstr /C:"Y">nul && (
185+
echo.%USESRV% | findstr /C:"Y">nul && (
173186
mongorestore --uri mongodb+srv://%MONGO_USER%:%MONGO_PASSWORD%@%MONGO_HOST%/%MONGO_DB% %cd%\%db%
174187
) || (
175188
(if %MONGO_PORT% EQU 20717 (
@@ -431,16 +444,53 @@ EXIT /B 0
431444
EXIT /B 0
432445

433446

434-
:: List available databases (on localhost only)
447+
:: Lists available databases (on localhost only)
435448
:ShowDatabases
436449
%MONGO_SHELL% --eval "printjson(db.adminCommand( { listDatabases: 1, nameOnly:true } ))" > .dbs
437450

438451
echo.
439452
echo ----------------------------------------------------------
453+
echo Available LOCAL databases (on localhost only):
454+
findstr /C:name .dbs
455+
456+
set /p go=Press enter to continue...
457+
GoTo RenderNextScreen
458+
EXIT /B 0
459+
460+
461+
:: Lists available databases from the defined active connection credentials
462+
:: - when using the SRV Connection String (mongo+srv://) or,
463+
:: - when using the Standalone Connection String (mongo://) if prompted for an authentication database
464+
:ShowConnectionDatabases
465+
CALL :ConfirmUseSrv
466+
467+
:: Prompt for an authentication database if not using "mongo+srv://"
468+
(if NOT "%USESRV%" == "Y" (
469+
CALL :SetAuthSource
470+
471+
echo.!AUTHSOURCE! | findstr [A-Za-z]>nul && (
472+
echo Selected [!AUTHSOURCE!] for the authentication database.
473+
) || (
474+
echo.
475+
echo Error: Connecting using the standard connection string
476+
set /p go=requires the user's authentication database. Try again.
477+
GoTo RenderNextScreen
478+
)
479+
))
480+
481+
(if "%USESRV%" == "Y" (
482+
%MONGO_SHELL% mongodb+srv://%MONGO_USER%:%MONGO_PASSWORD%@%MONGO_HOST% --eval "printjson(db.adminCommand( { listDatabases: 1, nameOnly:true } ))" > .dbs
483+
) else (
484+
%MONGO_SHELL% mongodb://%MONGO_USER%:%MONGO_PASSWORD%@%MONGO_HOST%:%MONGO_PORT%/?authSource=!AUTHSOURCE! --eval "printjson(db.adminCommand( { listDatabases: 1, nameOnly:true } ))" > .dbs
485+
))
486+
487+
echo.
488+
echo ----------------------------------------------------------
489+
echo Host: %MONGO_HOST%
440490
echo Available databases:
441491
findstr /C:name .dbs
442492

443-
set /p go=Press enter to cotinue...
493+
set /p go=Press enter to continue...
444494
GoTo RenderNextScreen
445495
EXIT /B 0
446496

@@ -478,4 +528,19 @@ EXIT /B 0
478528
) else if %NextScreen% EQU %_ViewDatabaseCredentials% (
479529
GoTo ViewDatabaseCredentials
480530
)
531+
EXIT /B 0
532+
533+
534+
:: Prompt to confirm using the "mongodb+srv://" URI connection string
535+
:ConfirmUseSrv
536+
echo.
537+
set "USESRV="
538+
echo Do you want to use the mongodb+srv:// connection string?
539+
set /p USESRV=Tip: select mongodb+srv:// when interacting with Atlas databases [Y/n]:
540+
EXIT /B 0
541+
542+
:SetAuthSource
543+
echo.
544+
set "AUTHSOURCE="
545+
set /p AUTHSOURCE="Enter the user authentication database:"
481546
EXIT /B 0

0 commit comments

Comments
 (0)