Delete backups older than 4 weeks ago

The script uses pbm delete-backup and delete-pitr to clean up backups older than 4 weeks. The script then sends an email when complete

Secrets are stored in a .env file

Updated version with more parameters and a comment section.

#!/usr/bin/env python3

"""
purge_mongodb_backups.py

This script does the following:
1. Calculates the date based on the number of weeks to keep (specified by the 'weeks_to_keep' argument).
2. Connects to MongoDB and gets the replica set name.
3. Executes the 'pbm delete-pitr' command with a '--older-than' parameter set to the calculated date, if the 'pitr' argument is set to 1.
4. Executes the 'pbm delete-backup' command with a '--older-than' parameter set to the calculated date, if the 'pitr' argument is set to 0.
5. Sends an email notifying that the chosen command has been executed, the number of weeks specified to keep, and includes the replica set name in the email subject and body.

This script reads the MongoDB username and password from an external .env file.

Before running this script, make sure you have the necessary permissions to execute the commands and send emails.
Also, replace the placeholders in the email details section with your actual SMTP server details, email address, email password, and recipient's email address.

Usage: python3 purge_mongodb_backups.py --pitr {0,1} --weeks_to_keep {1,2,3,4}
"""

import datetime
import subprocess
import shlex
import argparse
from dotenv import load_dotenv
import os
import smtplib
from email.mime.text import MIMEText
from pymongo import MongoClient

# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--pitr', default=0, type=int, choices=[0, 1], help='Set to 1 to delete point-in-time recovery backups, or 0 to delete regular backups.')
parser.add_argument('--weeks_to_keep', default=4, type=int, choices=[1, 2, 3, 4], help='The number of weeks to keep. Valid values are 1, 2, 3, and 4.')
args = parser.parse_args()

# Load values from .env file
load_dotenv()

# Get the values from the .env file
user_name = os.getenv("USERNAME")
pass_word = os.getenv("PASSWORD")

# Calculate the date based on the number of weeks to keep
weeks_to_keep = args.weeks_to_keep
date_to_keep = (datetime.datetime.now() - datetime.timedelta(weeks=weeks_to_keep)).strftime('%Y-%m-%d')

# Prepare the commands
cmd1 = f"pbm delete-pitr -f --older-than {date_to_keep} --mongodb-uri='mongodb://{user_name}:{pass_word}@localhost:27017/admin?authSource=admin'"
cmd2 = f"pbm delete-backup -f --older-than {date_to_keep} --mongodb-uri='mongodb://{user_name}:{pass_word}@localhost:27017/admin?authSource=admin'"
cmd1 = shlex.split(cmd1)
cmd2 = shlex.split(cmd2)

# Execute the chosen command
if args.pitr == 1:
    subprocess.run(cmd1, check=True)
elif args.pitr == 0:
    subprocess.run(cmd2, check=True)

# Connect to MongoDB and get replica set name
client = MongoClient(f'mongodb://{user_name}:{pass_word}@localhost:27017/admin?authSource=admin')
replica_set_name = client.admin.command("isMaster")["setName"]

# Email details
smtp_server = 'smtp.yourserver.com'
smtp_port = 587  # or 465 if your server uses SSL
email_address = 'your_email_address'
email_password = 'your_email_password'
recipient_email = 'recipient_email_address'

# Email content
msg = MIMEText(f'The chosen command has been executed successfully on replica set: {replica_set_name}. The number of weeks to keep is: {weeks_to_keep}.')
msg['Subject'] = f'Command Execution Notification for {replica_set_name}'
msg['From'] = email_address
msg['To'] = recipient_email

# Send the email
s = smtplib.SMTP(smtp_server, smtp_port)
s.starttls()  # comment this line if your server uses SSL
s.login(email_address, email_password)
s.sendmail(email_address, [recipient_email], msg.as_string())
s.quit()

The .env file

The environment file is of the following format

USERNAME=yourusername
PASSWORD=yourpassword

Original version:

import datetime
import subprocess
import shlex
from dotenv import load_dotenv
import os
import smtplib
from email.mime.text import MIMEText
from pymongo import MongoClient

# Load values from .env file
load_dotenv()

# Get the values from the .env file
user_name = os.getenv("USERNAME")
pass_word = os.getenv("PASSWORD")

# Calculate the date four weeks ago
four_weeks_ago = (datetime.datetime.now() - datetime.timedelta(weeks=4)).strftime('%Y-%m-%d')

# Prepare the first command
cmd1 = f"pbm delete-pitr -f --older-than {four_weeks_ago} --mongodb-uri='mongodb://{user_name}:{pass_word}@localhost:27017/admin?authSource=admin'"

# Prepare the second command
cmd2 = f"pbm delete-backup -f --older-than {four_weeks_ago} --mongodb-uri='mongodb://{user_name}:{pass_word}@localhost:27017/admin?authSource=admin'"

# Use shlex to handle spaces in commands properly
cmd1 = shlex.split(cmd1)
cmd2 = shlex.split(cmd2)

# Execute the first command
subprocess.run(cmd1, check=True)

# Execute the second command
subprocess.run(cmd2, check=True)

# Connect to MongoDB and get replica set name
client = MongoClient(f'mongodb://{user_name}:{pass_word}@localhost:27017/admin?authSource=admin')
replica_set_name = client.admin.command("isMaster")["setName"]

# Email details
smtp_server = 'smtp.yourserver.com'
smtp_port = 587  # or 465 if your server uses SSL
email_address = 'your_email_address'
email_password = 'your_email_password'
recipient_email = 'recipient_email_address'

# Email content
msg = MIMEText(f'Both commands have been executed successfully on replica set: {replica_set_name}.')
msg['Subject'] = f'Command Execution Notification for {replica_set_name}'
msg['From'] = email_address
msg['To'] = recipient_email

# Send the email
s = smtplib.SMTP(smtp_server, smtp_port)
s.starttls()  # comment this line if your server uses SSL
s.login(email_address, email_password)
s.sendmail(email_address, [recipient_email], msg.as_string())
s.quit()

By Rudy

Leave a Reply

Your email address will not be published. Required fields are marked *