0% found this document useful (0 votes)
12 views2 pages

MON NOTEs

Mongo DB script

Uploaded by

aribrabbani2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

MON NOTEs

Mongo DB script

Uploaded by

aribrabbani2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#!

/bin/bash

# MongoDB Maintenance Script

# Set variables
MONGO_HOST="localhost"
MONGO_PORT="27017"
MONGO_USER="admin"
MONGO_PASS="password"
BACKUP_DIR="/path/to/backup"
LOG_FILE="/path/to/log/mongo_maintenance.log"
DATE=$(date +"%Y-%m-%d-%H-%M")

# Functions

# Backup MongoDB
backup_mongo() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Starting MongoDB backup..." | tee -a
$LOG_FILE
mongodump --host $MONGO_HOST --port $MONGO_PORT --username $MONGO_USER --
password $MONGO_PASS --authenticationDatabase "admin" --out
$BACKUP_DIR/mongo_backup_$DATE
if [ $? -eq 0 ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Backup successful:
$BACKUP_DIR/mongo_backup_$DATE" | tee -a $LOG_FILE
else
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Backup failed!" | tee -a $LOG_FILE
fi
}

# Restore MongoDB
restore_mongo() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Starting MongoDB restore..." | tee -a
$LOG_FILE
mongorestore --host $MONGO_HOST --port $MONGO_PORT --username $MONGO_USER --
password $MONGO_PASS --authenticationDatabase "admin" --dir
$BACKUP_DIR/mongo_backup_$DATE
if [ $? -eq 0 ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Restore successful" | tee -a $LOG_FILE
else
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Restore failed!" | tee -a $LOG_FILE
fi
}

# Compact MongoDB collections (optimize storage)


compact_mongo() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Compacting MongoDB collections..." | tee -
a $LOG_FILE
mongo --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASS --
authenticationDatabase "admin" --eval 'db.runCommand({ compact:
"collection_name" })'
if [ $? -eq 0 ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Compacting completed successfully." |
tee -a $LOG_FILE
else
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Compacting failed!" | tee -a $LOG_FILE
fi
}
# Create a new MongoDB user
create_user() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Creating MongoDB user..." | tee -a
$LOG_FILE
mongo --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASS --
authenticationDatabase "admin" --eval '
db.getSiblingDB("admin").createUser({
user: "new_user",
pwd: "new_password",
roles: [{ role: "readWrite", db: "test_db" }]
})'
if [ $? -eq 0 ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] User created successfully." | tee -a
$LOG_FILE
else
echo "[$(date +"%Y-%m-%d %H:%M:%S")] User creation failed!" | tee -a
$LOG_FILE
fi
}

# Drop a MongoDB user


drop_user() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Dropping MongoDB user..." | tee -a
$LOG_FILE
mongo --host $MONGO_HOST --port $MONGO_PORT -u $MONGO_USER -p $MONGO_PASS --
authenticationDatabase "admin" --eval '
db.getSiblingDB("admin").dropUser("new_user")'
if [ $? -eq 0 ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")

You might also like