Fix backups cleanup, tweak mongo script

This commit is contained in:
root
2023-07-07 00:48:20 +00:00
parent 0822e740da
commit 193f09dcf4
3 changed files with 1690 additions and 1047 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +1,22 @@
#!/bin/bash
# Use script by passing username, password and path: ./mongodb-backup.sh USER PASSWORD PATH
# If no arguments are passed, the script will use the environment variables:
# BACKUP_MONGO_USER
# BACKUP_MONGO_PASSWORD
# BACKUP_MONGO_PATH
mongo_host="localhost"
mongo_port="27017"
mongo_user=$1
mongo_password=$2
mongo_user=${1:-$BACKUP_MONGO_USER}
mongo_password=${2:-$BACKUP_MONGO_PASSWORD}
mongo_auth_db="admin"
backup_folder_root=$3
backup_folder_root=${3:-$BACKUP_MONGO_PATH}
if [[ -z "$backup_folder_root" ]]; then
echo "Backup folder not set. Aborting script."
exit 1
fi
# set the threshold date as two weeks ago
threshold_date=$(date --date="-2 weeks" +%Y%m%d)
@@ -24,17 +33,19 @@ backup_path="${backup_folder_root}/$backup_parent_dir/$backup_child_dir"
# create the backup parent directory if it doesn't exist
mkdir -p "$backup_path"
# loop through all directories in the parent directory
for dir in */; do
# loop through all directories in the backup root directory
for dir in "$backup_folder_root"/*/; do
# check if the directory name is in YYYYMMDD format
if [[ "$dir" =~ ^[0-9]{8}/$ ]]; then
echo "Checking $dir"
if [[ "$dir" =~ ^$backup_folder_root/[0-9]{8}/$ ]]; then
# get the directory name as a date string
dir_date=$(echo "$dir" | sed 's/\///')
dir_date=$(basename "$dir")
echo "Directory date: $dir_date"
# compare the directory date to the threshold date
if [ "$dir_date" -lt "$threshold_date" ]; then
# delete the directory and its contents
echo "Deleting $dir"
# rm -rf "$dir"
rm -rf "$dir"
fi
fi
done

View File

@@ -5,55 +5,87 @@
# limiting the log file to a maximum of 1000 lines
LOG_FILE="/root/scripts/logs/mongodb-status.log"
EMAIL="tessmarka@gmail.com"
status=$(sudo systemctl status mongodb.service)
current_time=$(date +"%Y-%m-%d %T")
if echo "$status" | grep -qE "Active: running|Active: active"; then
echo "[$current_time] Success $(echo "$status" | grep "Main PID")" >> $LOG_FILE
echo "[$current_time] Success $(echo "$status" | grep "Main PID")" >> "$LOG_FILE"
else
echo "[$current_time] Failed" >> $LOG_FILE
echo "[$current_time] Failed" >> "$LOG_FILE"
email_body="MongoDB database service is down.\n"
# Directory path
directory="/var/run/mongodb"
# Check if directory exists, and create it if it doesn't
if [ ! -d "$directory" ]; then
email_body+="Directory $directory does not exist, and will be created.\n"
if ! mkdir -p "$directory"; then
echo "Failed to create directory: $directory" >> "$LOG_FILE"
email_body+="Failed to create directory: $directory\n"
else
echo "Created directory: $directory" >> "$LOG_FILE"
email_body+="Created directory: $directory\n"
fi
fi
# File path
file="$directory/mongod.pid"
# Check if file exists, and create it if it doesn't
if [ ! -f "$file" ]; then
email_body+="File $file does not exist, and will be created.\n"
if ! touch "$file"; then
echo "Failed to create file: $file" >> "$LOG_FILE"
email_body+="Failed to create file: $file\n"
else
echo "Created file: $file" >> "$LOG_FILE"
email_body+="Created file: $file\n"
fi
fi
# Change ownership of the file to mongod:mongod
email_body+="Changing ownership of $file to mongod:mongod\n"
if ! chown mongod:mongod "$file"; then
echo "Failed to change ownership of $file to mongod:mongod" >> "$LOG_FILE"
email_body+="Failed to change ownership of $file to mongod:mongod\n"
else
echo "Changed ownership of $file to mongod:mongod" >> "$LOG_FILE"
email_body+="Changed ownership of $file to mongod:mongod\n"
fi
# Do a final check to see if the file exists and is owned by mongod:mongod
if [ -f "$file" ] && [ "$(stat -c %U:%G "$file")" == "mongod:mongod" ]; then
email_body+="File $file exists and is owned by mongod:mongod\n"
else
email_body+="File $file does not exist or is not owned by mongod:mongod\n"
exit
fi
sleep 5
# Restart service
email_body+="Restarting MongoDB database service\n"
sudo systemctl restart mongodb.service
echo "MongoDB database service has stopped running, and was restarted" | mail -s "MongoDB down" tessmarka@gmail.com
sleep 5
status=$(sudo systemctl status mongodb.service)
if echo "$status" | grep -qE "Active: running|Active: active"; then
echo "[$current_time] Successfully restarted" >> $LOG_FILE
email_body+="MongoDB database service has been restarted successfully.\n"
echo "[$current_time] Successfully restarted" >> "$LOG_FILE"
else
echo "[$current_time] Failed to restart" >> $LOG_FILE
# Directory path
directory="/var/run/mongodb"
# Check if directory exists, and create it if it doesn't
if [ ! -d "$directory" ]; then
mkdir -p "$directory"
echo "Created directory: $directory" >> $LOG_FILE
fi
# File path
file="$directory/mongod.pid"
# Check if file exists, and create it if it doesn't
if [ ! -f "$file" ]; then
touch "$file"
echo "Created file: $file" >> $LOG_FILE
fi
# Change ownership of the file to mongod:mongod
chown mongod:mongod "$file"
echo "Changed ownership of $file to mongod:mongod" >> $LOG_FILE
# Restart service
sudo systemctl restart mongodb.service
status=$(sudo systemctl status mongodb.service)
if echo "$status" | grep -qE "Active: running|Active: active"; then
echo "[$current_time] Successfully restarted" >> $LOG_FILE
else
echo "[$current_time] Failed to restart" >> $LOG_FILE
fi
email_body+="MongoDB database service failed to restart.\n"
echo "[$current_time] Failed to restart" >> "$LOG_FILE"
fi
# Send email
echo -e "$email_body" | mail -s "MongoDB service has stopped" "$EMAIL"
fi
lines=$(wc -l < $LOG_FILE)