Last active
October 22, 2024 21:13
-
-
Save manjotsk/ece76edc017c162a607e8bf8bf3d5bcb to your computer and use it in GitHub Desktop.
Restoring MongoDB from Atlas server to local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function for password input (hidden) | |
read_password() { | |
echo -n "$1: " | |
read -s password | |
echo "" # Add a newline after input | |
echo $password | |
} | |
# Interactive input for MongoDB Atlas details | |
echo -n "Enter your MongoDB Atlas username: " | |
read atlas_username | |
echo "DEBUG: Username entered: $atlas_username" | |
# Hidden password input | |
echo -n "Enter your MongoDB Atlas username: " | |
read -s atlas_password | |
echo -n "Enter your MongoDB Atlas cluster URL (e.g., cluster0.mongodb.net): " | |
read atlas_cluster_url | |
echo "DEBUG: Cluster URL entered: $atlas_cluster_url" | |
echo -n "Enter the name of the database you want to transfer from Atlas: " | |
read atlas_database_name | |
echo "DEBUG: Database name entered: $atlas_database_name" | |
backup_folder="backup_$(date +"%Y%m%d_%H%M%S")" # Timestamped backup folder | |
# Interactive input for local MongoDB details | |
echo -n "Enter the name for the local database: " | |
read local_database_name | |
echo "DEBUG: Local database name entered: $local_database_name" | |
# Confirm the details | |
echo "------------------------------------" | |
echo "You entered the following details:" | |
echo "Atlas Username: $atlas_username" | |
echo "Atlas Cluster URL: $atlas_cluster_url" | |
echo "Atlas Database Name: $atlas_database_name" | |
echo "Local Database Name: $local_database_name" | |
echo "Backup folder will be created at: ~/$backup_folder" | |
echo "------------------------------------" | |
# Confirm if the user wants to proceed | |
echo -n "Do you want to proceed with the database transfer? (y/n): " | |
read confirm | |
if [[ $confirm != "y" ]]; then | |
echo "Transfer cancelled." | |
exit 1 | |
fi | |
# Step 1: Run mongodump to back up the Atlas database | |
echo "Running mongodump to back up the MongoDB Atlas database..." | |
mongodump --uri="mongodb+srv://$atlas_username:$atlas_password@$atlas_cluster_url/$atlas_database_name" --out ~/$backup_folder | |
if [ $? -ne 0 ]; then | |
echo "Error during mongodump. Exiting." | |
exit 1 | |
fi | |
echo "Backup completed. Data saved to ~/$backup_folder" | |
# Step 2: Restore the database to local MongoDB | |
echo "Restoring the database to your local MongoDB instance..." | |
mongorestore --drop --db $local_database_name ~/$backup_folder/$atlas_database_name | |
if [ $? -ne 0 ]; then | |
echo "Error during mongorestore. Exiting." | |
exit 1 | |
fi | |
echo "Database transfer completed successfully." | |
echo "Local database '$local_database_name' has been restored from MongoDB Atlas database '$atlas_database_name'." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment