Skip to content

Commit

Permalink
Merge pull request #16 from weaponsforge/feature/weaponsforge-15
Browse files Browse the repository at this point in the history
feat: display the database list, #15
  • Loading branch information
weaponsforge authored Nov 26, 2023
2 parents f16689a + aaa9aa3 commit 4ae3c07
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 23 deletions.
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
> - Imports `(mongorestore)` the exported binary database into a new database.
> - Drops a specified localhost database.
### Table of Contents

- [Dependencies](#dependencies)
- [Content](#content)
- [Usage and Installation](#usage-and-installation)
- [Troubleshooting](#troubleshooting)



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


## Usage
## Usage and Installation

1. Clone this repository.
`git clone https://github.com/weaponsforge/mongo-importex.git`
Expand All @@ -81,23 +88,29 @@ Windows batch script to automate switching of MongoDB database credentials for a
| 4 | Enter user name | MongoDB user name |
| 5 | Enter password | MongoDB user password |
| 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)|
| 7 | Save | Saves the encoded database credentials for future use to a `.env` and exit to the main screen. |
| 8 | Save and Export Database | Saves the mongodb settings to a `.env` file and starts the Export Database process. |
| 9 | Save and Import Database | Saves the mongodb settings to a `.env` file and starts to the Import Database process. |
| 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. |
| 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. |
| 7 | Save | Saves the encoded database credentials for future use to a `.env` file and exit to the main screen. |
| 8 | Save and Export Database | Saves the mongodb settings to a `.env` file and starts the **Export Database** process. |
| 9 | Save and Import Database | Saves the mongodb settings to a `.env` file and starts to the **Import Database** process. |
| 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. |
| 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. |
| x | Exit | Exit the script. |

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**.
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>

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.
- Type `"Y"` and press ENTER if you want to use the `"mongo+srv://"` SRV Connection String.
- Type `"n"` and press ENTER if you want to use the `"mongo://"` Standard Connection String.

| NO. | Prompt | Description |
|---|---|---|
| 1 | Export Database | Exports (`mongodump`) the database defined in the MongoDB Connection Credentials Setup to a binary JSON format database. |
| 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 |
| 3 | Drop Database | It deletes a database defined in the MongoDB connection credentials. Currently available only for localhost MongoDB. |
| 4 | Update Connection Credentials | Displays the **MONGODB CONNECTION CREDENTIALS SETUP** screen for editing the stored database connection details. |
| 5 | Reset | Resets the database conection details |
| 6 | Exit | Exit the script. |
| 4 | List Databases | Lists the available databases from the defined MongoDB connection credentials. |
| 5 | List Local Databases | Lists the available localhost databases. |
| 6 | Update Connection Credentials | Displays the **MONGODB CONNECTION CREDENTIALS SETUP** screen for editing the stored database connection details. |
| 7 | Reset | Resets the database conection details |
| x | Exit | Exit the script. |


## Troubleshooting
Expand Down
91 changes: 78 additions & 13 deletions main.bat
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ GoTo Main
:: Legacy MongoDB uses "mongo" shell, newer versions use "mongosh"
set "MONGO_SHELL="

:: Flag to use the "mongodb+srv://" SRV Connection String URI
set "USESRV="

:: User authentication database if using the Standard Connection String "mongo://"
set "AUTHSOURCE="

:: Screen ID's
set /A _ViewDatabaseCredentials=1
set /A _SetDatabaseCredentials=2
Expand Down Expand Up @@ -58,8 +64,10 @@ EXIT /B 0
echo [1] Export Database
echo [2] Import Database
echo [3] Drop Database
echo [4] Update Connection Credentials
echo [5] Reset
echo [4] List Databases
echo [5] List Local Databases
echo [6] Update Connection Credentials
echo [7] Reset
echo [x] Exit
set "choice=-1"
echo.
Expand All @@ -68,8 +76,16 @@ EXIT /B 0
if %choice% EQU 1 GoTo ExportDatabase
if %choice% EQU 2 GoTo SelectDatabaseToImport
if %choice% EQU 3 Goto DeleteDatabase
if %choice% EQU 4 Goto SetDatabaseCredentials
if %choice% EQU 5 Goto ResetData
if %choice% EQU 4 (
set /A NextScreen=_ViewDatabaseCredentials
Goto ShowConnectionDatabases
)
if %choice% EQU 5 (
set /A NextScreen=_ViewDatabaseCredentials
Goto ShowDatabases
)
if %choice% EQU 6 Goto SetDatabaseCredentials
if %choice% EQU 7 Goto ResetData
if %choice% == x EXIT /B 0

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

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

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

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

set "con="
set "usesrv="

echo Are you sure you want to import [%db%]
set /p con=to the above database server? [Y/n]:

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

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

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


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

echo.
echo ----------------------------------------------------------
echo Available LOCAL databases (on localhost only):
findstr /C:name .dbs

set /p go=Press enter to continue...
GoTo RenderNextScreen
EXIT /B 0


:: Lists available databases from the defined active connection credentials
:: - when using the SRV Connection String (mongo+srv://) or,
:: - when using the Standalone Connection String (mongo://) if prompted for an authentication database
:ShowConnectionDatabases
CALL :ConfirmUseSrv

:: Prompt for an authentication database if not using "mongo+srv://"
(if NOT "%USESRV%" == "Y" (
CALL :SetAuthSource

echo.!AUTHSOURCE! | findstr [A-Za-z]>nul && (
echo Selected [!AUTHSOURCE!] for the authentication database.
) || (
echo.
echo Error: Connecting using the standard connection string
set /p go=requires the user's authentication database. Try again.
GoTo RenderNextScreen
)
))

(if "%USESRV%" == "Y" (
%MONGO_SHELL% mongodb+srv://%MONGO_USER%:%MONGO_PASSWORD%@%MONGO_HOST% --eval "printjson(db.adminCommand( { listDatabases: 1, nameOnly:true } ))" > .dbs
) else (
%MONGO_SHELL% mongodb://%MONGO_USER%:%MONGO_PASSWORD%@%MONGO_HOST%:%MONGO_PORT%/?authSource=!AUTHSOURCE! --eval "printjson(db.adminCommand( { listDatabases: 1, nameOnly:true } ))" > .dbs
))

echo.
echo ----------------------------------------------------------
echo Host: %MONGO_HOST%
echo Available databases:
findstr /C:name .dbs

set /p go=Press enter to cotinue...
set /p go=Press enter to continue...
GoTo RenderNextScreen
EXIT /B 0

Expand Down Expand Up @@ -478,4 +528,19 @@ EXIT /B 0
) else if %NextScreen% EQU %_ViewDatabaseCredentials% (
GoTo ViewDatabaseCredentials
)
EXIT /B 0


:: Prompt to confirm using the "mongodb+srv://" URI connection string
:ConfirmUseSrv
echo.
set "USESRV="
echo Do you want to use the mongodb+srv:// connection string?
set /p USESRV=Tip: select mongodb+srv:// when interacting with Atlas databases [Y/n]:
EXIT /B 0

:SetAuthSource
echo.
set "AUTHSOURCE="
set /p AUTHSOURCE="Enter the user authentication database:"
EXIT /B 0

0 comments on commit 4ae3c07

Please sign in to comment.