Updates the ip adress in Network Access List for cloud.mongodb.com
Steps to run:
mongodb-migrate.bat:
- Save it with a
.bat
extension (e.g.,mongodb-migrate.bat
) - Replace the placeholder values:
PROJECT_ID
PUBLIC_API
MONGO_KEY
MONGODB_CONNECTION_STRING
- Ensure
migrate.exe
is in the same directory or update its path - Run the script with administrator privileges
migrate-mongodb.bat:
- Save it with a
.bat
extension - Replace these placeholder values:
PROJECT_ID
with your MongoDB Atlas project IDPUBLIC_API
with your public API keyMONGO_KEY
with your private API keyALLOWED_IP
with the specific IP address you want to whitelistMONGODB_CONNECTION_STRING
with your MongoDB connection string
- Ensure
migrate.exe
is in the same directory or update its path - Run the script
update-ip.yml:
- Create a new repository on GitHub (if you don't already have one)
- Create the workflow file at
.github/workflows/update-ip.yml
with the content above - Add these secrets in your GitHub repository settings:
MONGODB_PROJECT_ID
: Your MongoDB Atlas Project IDMONGODB_PUBLIC_KEY
: Your MongoDB Atlas Public API KeyMONGODB_PRIVATE_KEY
: Your MongoDB Atlas Private API Key
monitor-ip.ps1:
- Save the script as
monitor-ip.ps1
- Replace these values at the top of the script:
your-project-id
with your MongoDB Atlas Project IDyour-public-key
with your MongoDB Public API Keyyour-private-key
with your MongoDB Private API Key
- Run the script as administrator once to set up: powershell.exe -ExecutionPolicy Bypass -File "C:\path\to\monitor-ip.ps1"
- Save it as
monitor-ip.ps1
- Replace these values at the top:
your-project-id
with MongoDB Atlas Project IDyour-public-key
with MongoDB Public API Keyyour-private-key
with MongoDB Private API Key- Optionally change the
$LOCATION
variable to your preferred location identifier
- Run as administrator: powershell.exe -ExecutionPolicy Bypass -File "C:\path\to\monitor-ip.ps1"
setup-migration.ps1:
- Save the script as
setup-migration.ps1
- Run it as administrator: powershell.exe -ExecutionPolicy Bypass -File "setup-migration.ps1"
After installation, create a migrations directory and migration files:
- Create migrations folder: mkdir migrations
Create a new migration: migrate create -ext .sql -dir migrations -seq init_schema
Edit the generated migration files in the migrations folder
mongo-ip-monitor.ps1:
-
Save it as
mongo-ip-monitor.ps1
-
Replace these values at the top: $PROJECT_ID = "your-project-id" $PUBLIC_KEY = "your-public-key" $PRIVATE_KEY = "your-private-key"
-
Run as administrator: powershell.exe -ExecutionPolicy Bypass -File "C:\path\to\mongo-ip-monitor.ps1"
Description of the scripts:
mongodb-migrate.bat: This Windows Batch script accomplishes the same tasks as your original script but with several improvements:
- Uses native Windows commands and PowerShell where needed
- Includes proper error handling
- Uses Base64 encoding for authentication headers
- Maintains the same workflow:
- Gets the current public IP
- Adds IP to MongoDB Atlas whitelist
- Runs migrations
- Removes IP from whitelist
- Includes cleanup even if migration fails
migrate-mongodb.bat: This simplified script:
- Removes all runner-related code
- Allows you to manually specify the IP address you want to add to MongoDB Atlas Network Access
- Doesn't remove the IP after migration (since it's a permanently allowed IP)
- Still maintains the core functionality of:
- Adding IP to MongoDB Atlas whitelist
- Running migrations
update-ip.yml: This solution:
- Creates a GitHub Actions workflow that runs every 30 minutes (configurable)
- Checks your current public IP using api.ipify.org
- Only updates MongoDB Atlas Network Access if the IP has changed
- Stores the last known IP in a file for comparison
- Uses GitHub's secure secrets for storing sensitive MongoDB credentials
The workflow will:
- Run automatically every 30 minutes
- Check if your IP has changed
- Update MongoDB Atlas Network Access only when necessary
- Keep a record of your last IP in the repository
- Can be manually triggered from the GitHub Actions tab if needed
Benefits of this approach:
- No need for a local runner
- Runs on GitHub's infrastructure
- Automatic and reliable monitoring
- Secure credential handling
- Easy to monitor through GitHub Actions dashboard
- Maintains history of IP changes through git commits
monitor-ip.ps1: This solution:
- Runs locally on your Windows machine
- Creates a Windows Scheduled Task that runs every 15 minutes
- Checks your actual home IP address
- Updates MongoDB Atlas only when your IP changes
- Maintains logs in
C:\\MongoIPMonitor\\ip-monitor.log
- Stores the last known IP in
C:\\MongoIPMonitor\\last-ip.txt
- Runs automatically after system restart
- Uses Windows Task Scheduler for reliability
The script will:
- Monitor your home IP address every 15 minutes
- Update MongoDB Atlas Network Access when your IP changes
- Log all activities for troubleshooting
- Run automatically in the background
- Start automatically when Windows starts
ip-monitor.ps1: Key changes made:
- Added more context to the MongoDB Network Access comment including:
- Location identifier (configurable at the top of script)
- Computer name
- Username
- Timestamp
- The comment will now appear in MongoDB like this example:
"Auto-updated: Home | PC: DESKTOP-ABC123 | User: John | Updated: 2024-11-29 14:30:00"
The script will now add detailed comments to your MongoDB Network Access entries, making it easier to:
- Track which computer updated the IP
- Know when the IP was last updated
- Identify who was using the computer
- Distinguish between different locations if you use the script on multiple machines
setup-migration.ps1: This script will:
- Install Go if it's not already installed
- Install the golang-migrate tool
- Set up the necessary environment variables
- Place migrate.exe in an accessible location
mongo-ip-monitor.ps1: Key improvements in this version:
- Removed all migration-related code
- Added automatic cleanup of old IP entries
- Better error handling and logging
- Maintains a single IP entry per computer
- More detailed status logging
The script will:
- Create a monitoring service that runs every 15 minutes
- Check your current IP address
- Update MongoDB Atlas Network Access when your IP changes
- Remove old IP entries for your computer
- Maintain detailed logs in
C:\\MongoIPMonitor\\ip-monitor.log
New features:
- Automatic cleanup of old IP entries
- Better tracking of which computer added which IP
- More robust error handling
- Detailed logging of all operations
- Automatic task scheduling