Skip to content

Instantly share code, notes, and snippets.

@tonyclemmey
Last active October 16, 2023 12:11
Show Gist options
  • Save tonyclemmey/3acd1f5fd08c290344b91236cead0155 to your computer and use it in GitHub Desktop.
Save tonyclemmey/3acd1f5fd08c290344b91236cead0155 to your computer and use it in GitHub Desktop.
PBM
sudo nano /etc/logrotate.d/mongod.conf
```
/var/log/mongodb/mongod.log {
daily
size 100M
rotate 7
missingok
compress
delaycompress
create 0600 mongodb mongodb
sharedscripts
postrotate
/bin/kill -SIGUSR1 $(pidof mongod)
endscript
}
```
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
logRotate: reopen
#!/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 ############
# ===================================
MONGO_USERNAME="mongo-root"
MONGO_PASSWORD="password"
PBM_USERNAME="mongo-pbm"
PBM_PASSWORD="password"
MONGO_NODE_1="server1"
MONGO_NODE_2="server2"
MONGO_NODE_3="server3"
MONGO_PORT="27017"
MONGO_URI="mongodb://$PBM_USERNAME:$PBM_PASSWORD@localhost:$MONGO_PORT"
BACKUPS_DIR="/data/local_backups/percona-backup-mongodb/"
# ===================================
# ########## Installation ###########
# ===================================
# Install the packages
cd ~ ; mkdir -p percona_backup_mongodb ; cd percona_backup_mongodb ; \
curl -O https://repo.percona.com/apt/percona-release_latest.generic_all.deb ; \
sudo apt-get -q install gnupg2 lsb-release -y ; \
sudo apt-get -q install ./percona-release_latest.generic_all.deb -y ; sudo apt-get -q update -y ; \
sudo percona-release enable pbm release ; sudo apt-get update -y ; \
sudo apt-get -q install percona-backup-mongodb -y ; cd ~
sudo tee /etc/apt/sources.list.d/percona-pbm-release.list << EOF
deb http://repo.percona.com/pbm/apt focal main
deb-src http://repo.percona.com/pbm/apt focal main
EOF
# Start the pbm-agent process
sudo systemctl enable pbm-agent --now
sleep 15s
sudo systemctl status pbm-agent --no-pager
# ===================================
# ######### Configuration ###########
# ===================================
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Creating the PBM User${NC}"
echo -e "${YEL}===================================${NC}"
# Create the role that allows any action on any resource.
sudo tee percona_mongodb_pbm_role.js <<EOF
db = db.getSiblingDB("admin");
db.createRole(
{
"role": "pbmAnyAction",
"privileges": [
{
"resource": { "anyResource": true },
"actions": [ "anyAction" ]
}
],
"roles": []
}
);
EOF
# Create the user and assign the role you created to it.
sudo tee percona_mongodb_pbm_user.js <<EOF
db = db.getSiblingDB("admin");
db.createUser(
{
"user": "$PBM_USERNAME",
"pwd": "$PBM_PASSWORD",
"roles" : [
{ "db" : "admin", "role" : "readWrite", "collection": "" },
{ "db" : "admin", "role" : "backup" },
{ "db" : "admin", "role" : "clusterMonitor" },
{ "db" : "admin", "role" : "restore" },
{ "db" : "admin", "role" : "pbmAnyAction" }
]
}
);
EOF
mongosh "mongodb://$MONGO_NODE_1:27017,$MONGO_NODE_2:27017,$MONGO_NODE_3:27017/?replicaSet=rs0&authSource=admin" \
--username "$MONGO_USERNAME" \
--password "$MONGO_PASSWORD" \
--authenticationDatabase "admin" \
--file percona_mongodb_pbm_role.js \
--file percona_mongodb_pbm_user.js
mongosh "mongodb://$MONGO_NODE_1:27017,$MONGO_NODE_2:27017,$MONGO_NODE_3:27017/?replicaSet=rs0&authSource=admin" \
--username "$MONGO_USERNAME" \
--password "$MONGO_PASSWORD" \
--eval 'use admin' \
--eval 'printjson(db.getUsers());'
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Creating the MongoDB connection URI${NC}"
echo -e "${YEL}===================================${NC}"
# Set the MongoDB connection URI for pbm-agent
sudo tee /etc/default/pbm-agent<<EOF
PBM_MONGODB_URI=$MONGO_URI
EOF
# Set the MongoDB connection URI for CLI
sudo echo "export PBM_MONGODB_URI='$MONGO_URI'" >> /etc/environment
. /etc/environment
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Configuring the PBM options and agent${NC}"
echo -e "${YEL}===================================${NC}"
# Create the backup path
sudo mkdir -p $BACKUPS_DIR
sudo chown -R mongodb:mongodb $BACKUPS_DIR
sudo chmod 0755 $BACKUPS_DIR
# Configure remote backup storage
sudo tee /usr/local/bin/pbm_config.yaml <<EOF
storage:
type: filesystem
filesystem:
path: $BACKUPS_DIR
EOF
# Insert the config file
pbm config --file /usr/local/bin/pbm_config.yaml
# Change user / group permission
sudo sed -i 's/^User=.*/User=mongodb/g' /lib/systemd/system/pbm-agent.service
sudo sed -i 's/^Group=.*/Group=mongodb/g' /lib/systemd/system/pbm-agent.service
# Make systemd aware of the new service:
sudo systemctl daemon-reload
# Restart the pbm-agent
sudo systemctl restart pbm-agent
sleep 15s
sudo systemctl status pbm-agent --no-pager
#!/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 ############
# ===================================
PBM_USERNAME="mongo-pbm"
PBM_PASSWORD="password"
MONGO_NODE_1="server1"
MONGO_NODE_2="server2"
MONGO_NODE_3="server3"
MONGO_PORT="27017"
MONGO_URI="mongodb://$PBM_USERNAME:$PBM_PASSWORD@$MONGO_NODE_1:$MONGO_PORT,$MONGO_NODE_2:$MONGO_PORT,$MONGO_NODE_3:$MONGO_PORT/?replicaSet=rs0&authSource=admin"
BACKUPS_DIR="/data/local_backups/percona-backup-mongodb/"
# ===================================
# ########## Installation ###########
# ===================================
# Install the packages
cd ~ ; mkdir -p percona_backup_mongodb ; cd percona_backup_mongodb ; \
curl -O https://repo.percona.com/apt/percona-release_latest.generic_all.deb ; \
sudo apt-get -q install gnupg2 lsb-release -y ; \
sudo apt-get -q install ./percona-release_latest.generic_all.deb -y ; sudo apt-get -q update -y ; \
sudo percona-release enable pbm release ; sudo apt-get update -y ; \
sudo apt-get -q install percona-backup-mongodb -y ; cd ~
# ===================================
# ######### Configuration ###########
# ===================================
# Set the MongoDB connection URI for CLI
sudo echo "export PBM_MONGODB_URI='$MONGO_URI'" >> /etc/environment
. /etc/environment
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Configuring the PBM options${NC}"
echo -e "${YEL}===================================${NC}"
# Configure remote backup storage
sudo tee /usr/local/bin/pbm_config.yaml <<EOF
storage:
type: filesystem
filesystem:
path: $BACKUPS_DIR
EOF
# Backup options
sudo tee -a /usr/local/bin/pbm_config.yaml <<EOF
backup:
priority:
"$MONGO_NODE_1:$MONGO_PORT": 0.5
"$MONGO_NODE_2:$MONGO_PORT": 1.0
"$MONGO_NODE_3:$MONGO_PORT": 2.0
compression: "gzip"
compressionLevel: 9
EOF
# Insert the config file
pbm config --file /usr/local/bin/pbm_config.yaml
# Perform Backup before PITR
pbm backup
# Enable PITR
pbm config --set pitr.enabled=true
pbm config --set pitr.compression=gzip
#!/bin/bash
# ===================================
# ##### Cron Job Backup (Prod) ######
# ===================================
# Add Cron Job:
# `crontab -e`
# Contents:
# ```
# # Everyday 15 mins (backup)
# # m h dom mon dow command
*/15 * * * * . /etc/environment ; /usr/bin/pbm backup
# ```
# ```
# # Everyday at 22:00 (cleanup)
# # m h dom mon dow command
0 22 * * * * /usr/bin/pbm delete-backup -f --older-than $(date -d '-35 min' +\%Y-\%m-\%d\T%H:\%M:\%S)
0 22 * * * * /usr/bin/pbm delete-backup -f --older-than $(date -d '-1 day' +\%Y-\%m-\%d\T%H:\%M:\%S)
0 22 * * * * /usr/bin/pbm delete-backup -f --older-than $(date -d '-1 month' +\%Y-\%m-\%d\T%H:\%M:\%S)
# ```
# ===================================
# ##### Cron Job Restore (DR) ######
# ===================================
# Add Environment Variables
sudo tee /etc/default/pbm-restore-cron <<EOF
export PBM_USERNAME="mongo-pbm"
export PBM_PASSWORD="password"
export MONGO_DR_NODE_1="server4"
export MONGO_DR_NODE_2="server5"
export MONGO_DR_NODE_3="server6"
export MONGO_DR_PORT="27017"
export MONGO_DR_URI="mongodb://$PBM_USERNAME:$PBM_PASSWORD@$MONGO_DR_NODE_1:$MONGO_DR_PORT,$MONGO_DR_NODE_2:$MONGO_DR_PORT,$MONGO_DR_NODE_3:$MONGO_DR_PORT/?replicaSet=rs0&authSource=admin"
export BACKUPS_DIR="/data/local_backups/percona-backup-mongodb/"
EOF
# Source Environment Variables
. /etc/default/pbm-restore-cron
File:
`/usr/local/bin/pbm_restore_backup.sh`
Contents:
```
/usr/bin/pbm config --force-resync
export BACKUP=$(find $BACKUPS_DIR/* -maxdepth 0 -type d ! -iname '*pbm*' -printf "%f\n" | sort -n | tail -n 1)
/usr/bin/pbm describe-backup $BACKUP
/usr/bin/pbm restore $BACKUP --mongodb-uri $MONGO_DR_URI
```
# Add Cron Job:
# `crontab -e`
# Contents:
# ```
# # Everyday 15 mins (backup)
# # m h dom mon dow command
*/15 * * * * sleep 150 ; . /etc/default/pbm-restore-cron ; bash /usr/local/bin/pbm_restore_backup.sh
# ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment