Fix backups cleanup, tweak mongo script
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,22 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Use script by passing username, password and path: ./mongodb-backup.sh USER PASSWORD PATH
|
# 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_host="localhost"
|
||||||
mongo_port="27017"
|
mongo_port="27017"
|
||||||
mongo_user=$1
|
mongo_user=${1:-$BACKUP_MONGO_USER}
|
||||||
mongo_password=$2
|
mongo_password=${2:-$BACKUP_MONGO_PASSWORD}
|
||||||
mongo_auth_db="admin"
|
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
|
# set the threshold date as two weeks ago
|
||||||
threshold_date=$(date --date="-2 weeks" +%Y%m%d)
|
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
|
# create the backup parent directory if it doesn't exist
|
||||||
mkdir -p "$backup_path"
|
mkdir -p "$backup_path"
|
||||||
|
|
||||||
# loop through all directories in the parent directory
|
# loop through all directories in the backup root directory
|
||||||
for dir in */; do
|
for dir in "$backup_folder_root"/*/; do
|
||||||
# check if the directory name is in YYYYMMDD format
|
# 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
|
# 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
|
# compare the directory date to the threshold date
|
||||||
if [ "$dir_date" -lt "$threshold_date" ]; then
|
if [ "$dir_date" -lt "$threshold_date" ]; then
|
||||||
# delete the directory and its contents
|
# delete the directory and its contents
|
||||||
echo "Deleting $dir"
|
echo "Deleting $dir"
|
||||||
# rm -rf "$dir"
|
rm -rf "$dir"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|||||||
@@ -5,55 +5,87 @@
|
|||||||
# limiting the log file to a maximum of 1000 lines
|
# limiting the log file to a maximum of 1000 lines
|
||||||
|
|
||||||
LOG_FILE="/root/scripts/logs/mongodb-status.log"
|
LOG_FILE="/root/scripts/logs/mongodb-status.log"
|
||||||
|
EMAIL="tessmarka@gmail.com"
|
||||||
|
|
||||||
status=$(sudo systemctl status mongodb.service)
|
status=$(sudo systemctl status mongodb.service)
|
||||||
current_time=$(date +"%Y-%m-%d %T")
|
current_time=$(date +"%Y-%m-%d %T")
|
||||||
|
|
||||||
if echo "$status" | grep -qE "Active: running|Active: active"; then
|
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
|
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
|
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)
|
status=$(sudo systemctl status mongodb.service)
|
||||||
if echo "$status" | grep -qE "Active: running|Active: active"; then
|
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
|
else
|
||||||
echo "[$current_time] Failed to restart" >> $LOG_FILE
|
email_body+="MongoDB database service failed to restart.\n"
|
||||||
|
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
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Send email
|
||||||
|
echo -e "$email_body" | mail -s "MongoDB service has stopped" "$EMAIL"
|
||||||
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
lines=$(wc -l < $LOG_FILE)
|
lines=$(wc -l < $LOG_FILE)
|
||||||
|
|||||||
Reference in New Issue
Block a user