Last active
February 10, 2023 14:11
-
-
Save tonyclemmey/32c8b65b3d3ed550b421521f8021f4d7 to your computer and use it in GitHub Desktop.
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
### MongoDB Backup | |
---- | |
**Shell Script / Cron Job** | |
File : | |
`/usr/local/bin/mongo_backup.sh` | |
Contents: | |
``` | |
#!/bin/bash | |
#-------------------------------------------- | |
# Enviroment Variables | |
#-------------------------------------------- | |
MONGO_USERNAME="" | |
MONGO_PASSWORD="" | |
MONGO_NODE_1="" | |
MONGO_NODE_2="" | |
MONGO_NODE_3="" | |
MONGO_PORT="27017" | |
MONGO_URI="$MONGO_NODE_1:$MONGO_PORT,$MONGO_NODE_2:$MONGO_PORT,$MONGO_NODE_3:$MONGO_PORT" | |
BACKUPS_DIR="/data/backups/mongodb/" | |
mkdir -p $BACKUPS_DIR | |
TIMESTAMP=`date -Iseconds` | |
BACKUP_NAME="mongodb-rs0-fullbackup_$TIMESTAMP" | |
DAYSTORETAINBACKUP="1" | |
echo "--------------------------------------------" | |
echo "Database backup in progress!" | |
echo "--------------------------------------------" | |
# run mongodump | |
mongodump \ | |
--uri="mongodb://$MONGO_URI" \ | |
--username="$MONGO_USERNAME" \ | |
--password="$MONGO_PASSWORD" \ | |
--authenticationDatabase="admin" \ | |
--out="$BACKUPS_DIR" \ | |
--gzip | |
# archive mongodump | |
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUPS_DIR/ | |
# remove backups older than 1 day | |
find $BACKUPS_DIR -type f -mtime +$DAYSTORETAINBACKUP -exec rm {} + | |
echo "--------------------------------------------" | |
echo "Database backup complete!" | |
echo "--------------------------------------------" | |
``` | |
Add Cron Job: | |
`crontab -e` | |
Contents: | |
``` | |
# Everyday at 18:00 | |
# m h dom mon dow command | |
* 18 * * * /bin/bash /usr/local/bin/mongo_backup.sh | |
``` |
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 | |
# =================================== | |
# ########## Bash Colours ########### | |
# =================================== | |
NC='\033[31;0m' # no colors or formatting | |
RED='\033[0;31;1m' # print text in bold Red | |
GRE='\033[0;32;1m' # print text in bold Green | |
YEL='\033[0;33;1m' # print text in bold Yellow | |
BLU='\033[0;34;1m' # print text in bold Blue | |
PUR='\033[0;35;1m' # print text in bold Purple | |
CYA='\033[0;36;1m' # print text in bold Cyan | |
GRA='\033[0;37;1m' # print text in bold Gray | |
# =================================== | |
# ######## ENV Variables ############ | |
# =================================== | |
# Monitoring User Account (SSH & MongoDB) | |
MONITOR_USERNAME="" | |
MONITOR_PASSWORD="" | |
ADMIN_USERNAME="" | |
ADMIN_PASSWORD="" | |
NODE_HOSTNAME="" | |
# =================================== | |
# ####### Create Linux User ######### | |
# =================================== | |
echo -e "${YEL}===================================${NC}" | |
echo -e "${YEL}Creating Linux User${NC}" | |
echo -e "${YEL}===================================${NC}" | |
useradd -m -s /bin/bash $MONITOR_USERNAME | |
echo "$MONITOR_USERNAME:$MONITOR_PASSWORD" | chpasswd | |
# =================================== | |
# ####### Create MongoDB User ####### | |
# =================================== | |
echo -e "${YEL}===================================${NC}" | |
echo -e "${YEL}Creating Monitor User on Master Node${NC}" | |
echo -e "${YEL}===================================${NC}" | |
sudo tee mongo_add_monitor_user.js <<EOF | |
db = db.getSiblingDB("admin"); | |
db.createUser( | |
{ | |
user: "$MONITOR_USERNAME", | |
pwd: "$MONITOR_PASSWORD", | |
roles: [{ role: "clusterMonitor", db: "admin" }] | |
} | |
); | |
EOF | |
mongosh "mongodb://$NODE_HOSTNAME:27017" \ | |
--username "$ADMIN_USERNAME" \ | |
--password "$ADMIN_PASSWORD" \ | |
--authenticationDatabase "admin" \ | |
--file mongo_add_monitor_user.js | |
mongosh "mongodb://$NODE_HOSTNAME:27017" \ | |
--username "$ADMIN_USERNAME" \ | |
--password "$ADMIN_PASSWORD" \ | |
--authenticationDatabase "admin" \ | |
--eval 'use admin' \ | |
--eval 'printjson(db.getUsers());' | |
mongosh "mongodb://$NODE_HOSTNAME:27017" \ | |
--username "$MONITOR_USERNAME" \ | |
--password "$MONITOR_PASSWORD" \ | |
--authenticationDatabase "admin" \ | |
--eval 'use admin' \ | |
--eval 'printjson(rs.status());' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment