Skip to content

Commit

Permalink
Merge pull request #46 from obendev/master
Browse files Browse the repository at this point in the history
Mail Filesize/Filename Fixer Script
  • Loading branch information
sibprogrammer authored Jan 18, 2024
2 parents 6d91039 + e9d5910 commit f2f8c38
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
1 change: 1 addition & 0 deletions fix-mail-filesize-filename/12389159356695.kb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

35 changes: 35 additions & 0 deletions fix-mail-filesize-filename/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Mail Filesize/Filename Fixer Script

## Overview

This script is designed to validate and correct discrepancies between the actual and stated file sizes in mail files. It's intended for use by technically experienced IT engineers.

The script checks mail files in a specified directory, and identifies any files where the size stated in the filename does not match the actual file size. It can optionally fix these discrepancies by renaming the files, and/or export the filenames of the mismatched files to a text file.

## Usage

The script can be run with the following command:

```
./scriptname [--fix] [--export]
```

Replace `scriptname` with the name of the script file.

The script accepts the following optional arguments:

- `--fix`: If this option is provided, the script will rename any files it finds where the size stated in the filename does not match the actual file size. The new filename will correct the stated size to match the actual size.

- `--export`: If this option is provided, the script will export the filenames of any mismatched files it finds to a text file. The text file will be named using the domain and username, in the format `{domain}_{username}_mismatches.txt`.

If no options are provided, the script will just check for mismatches and display the results. Once done, you'll be given the option to fix and/or export any mismatches found.

When you run the script, it will prompt you to enter the domain name and username. The script will then check the mail files in the directory `/var/qmail/mailnames/{domain}/{username}`.

## Requirements

The script requires Bash 3.x and GNU coreutils.

## Note

Please ensure that you have the necessary permissions to read and write to the directory and files before running the script.
163 changes: 163 additions & 0 deletions fix-mail-filesize-filename/fix-mail-filesize-filename.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/bin/bash
### Copyright 1999-2024. Plesk International GmbH.

###############################################################################
# This script validates and corrects discrepancies between actual and stated file sizes in mail files.
# Detailed instructions and usage guidelines can be found in the README.md.
# Requirements : bash 3.x, GNU coreutils
# Version : 2.1
#########

# Initialize flags for fix and export options
fix_flag=0
export_flag=0

# Initialize counter for fixed mismatches
fixed_count=0

# Check the arguments
for arg in "$@"; do
case $arg in
--fix)
fix_flag=1
shift
;;
--export)
export_flag=1
shift
;;
--help)
echo "Usage: $0 [--fix] [--export]"
echo "--fix: Renames files to correct the discrepancy between actual and stated file sizes."
echo "--export: Saves the names of files with discrepancies to a text file."
echo "--help: Displays this help message."
exit 0
;;
*)
echo "Error: Invalid argument: $arg"
echo "Usage: $0 [--fix] [--export]"
exit 1
;;
esac
done

# Start the timer
start_time=$(date +%s)

# Get the domain name and username
echo -n "Enter the domain name: "
read domain
echo -n "Enter the username: "
read username

# Set the directory path
dir="/var/qmail/mailnames/${domain}/${username}"
echo "The directory to be checked is: ${dir}"

# Check if the directory exists and is readable
if [ ! -d "${dir}" ] || [ ! -r "${dir}" ]; then
echo "Error: Directory does not exist or is not readable: ${dir}"
exit 1
fi

# Get the total number of files that match the expected filename format
total_files=$(find ${dir} -type f | grep -E 'S=[0-9]+:' | wc -l)
count=0
mismatch_count=0

# Initialize an array to store the mismatches
declare -a mismatches

# Function to check filenames
check_filenames() {
for file in $(find ${dir} -type f | grep -E 'S=[0-9]+:'); do
# Extract the expected size from the filename
expected_size=$(echo ${file} | grep -oP 'S=\K[0-9]+')

# Get the actual size
actual_size=$(stat -c%s "${file}")

# Check if the sizes match
if [ "${expected_size}" != "${actual_size}" ]; then
echo "Mismatch found in file: ${file}"
echo "Expected size: ${expected_size}, Actual size: ${actual_size}"
mismatch_count=$((mismatch_count+1))

# Store the mismatch information
mismatches+=("${file} ${expected_size} ${actual_size}")
fi

# Show the progress
count=$((count+1))
echo -ne "Progress: ${count}/${total_files} files checked\r"
done
echo "" # Move to a new line after the loop
}

# Function to export mismatches
export_mismatches() {
for mismatch in "${mismatches[@]}"; do
IFS read -r -a array <<< "$mismatch"
file="${array[0]}"
echo "${file}" >> "${domain}_${username}_mismatches.txt"
done
}

# Function to fix mismatches
fix_mismatches() {
for mismatch in "${mismatches[@]}"; do
IFS read -r -a array <<< "$mismatch"
file="${array[0]}"
expected_size="${array[1]}"
actual_size="${array[2]}"
new_file=$(echo ${file} | sed "s/S=${expected_size}/S=${actual_size}/")
if mv "${file}" "${new_file}"; then
echo "File has been renamed to: ${new_file}"
fixed_count=$((fixed_count+1))
else
echo "Error: Failed to rename file: ${file}"
fi
done
}

# Run the appropriate functions based on the provided arguments
check_filenames
if [ $export_flag -eq 1 ]; then
export_mismatches
fi
if [ $fix_flag -eq 1 ]; then
fix_mismatches
fi

# If no options were provided, ask the user if they want to fix or export the mismatches
if [ $fix_flag -eq 0 ] && [ $export_flag -eq 0 ] && [ $mismatch_count -gt 0 ]; then
echo -n "Inconsistencies found. Would you like to fix them or export them to a file? Enter 'fix', 'export', or 'both': "
read action
case $action in
fix)
fix_mismatches
;;
export)
export_mismatches
;;
both)
fix_mismatches
export_mismatches
;;
*)
echo "Invalid option. No action taken."
;;
esac
fi

# Calculate the elapsed time
end_time=$(date +%s)
elapsed_time=$((end_time-start_time))

# Print the statistics
echo "Total files checked: ${total_files}"
echo "Total mismatches found: ${mismatch_count}"
if [ $fix_flag -eq 1 ] || [ "${action:-}" == "fix" ] || [ "${action:-}" == "both" ]; then
echo "Total mismatches fixed: ${fixed_count}"
fi
echo "Elapsed time: ${elapsed_time} seconds"

0 comments on commit f2f8c38

Please sign in to comment.