Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to Mail Filesize/Filename Fixer Script #63

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Mail Filesize/Filename Fixer Script
= Mail Filesize/Filename Fixer Script

## Overview
== 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
== Usage

The script can be run with the following command:

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

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

Expand All @@ -26,10 +26,10 @@ If no options are provided, the script will just check for mismatches and displa

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
== Requirements

The script requires Bash 3.x and GNU coreutils.

## Note
== Note

Please ensure that you have the necessary permissions to read and write to the directory and files before running the script.
19 changes: 11 additions & 8 deletions fix-mail-filesize-filename/fix-mail-filesize-filename.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ if [ ! -d "${dir}" ] || [ ! -r "${dir}" ]; then
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)
total_files=0
count=0
mismatch_count=0

Expand All @@ -70,13 +70,16 @@ declare -a mismatches

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

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

# Increment the total_files counter
total_files=$((total_files+1))

# Check if the sizes match
if [ "${expected_size}" != "${actual_size}" ]; then
echo "Mismatch found in file: ${file}"
Expand All @@ -88,16 +91,16 @@ check_filenames() {
fi

# Show the progress
count=$((count+1))
echo -ne "Progress: ${count}/${total_files} files checked\r"
done
echo -ne "Progress: ${total_files} files checked\\r"
done < <(find ${dir} -type f -print0 | grep -zE ',S=[0-9]+')
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"
IFS= read -r -a array <<< "$mismatch"
file="${array[0]}"
echo "${file}" >> "${domain}_${username}_mismatches.txt"
done
Expand All @@ -106,7 +109,7 @@ export_mismatches() {
# Function to fix mismatches
fix_mismatches() {
for mismatch in "${mismatches[@]}"; do
IFS read -r -a array <<< "$mismatch"
IFS= read -r -a array <<< "$mismatch"
file="${array[0]}"
expected_size="${array[1]}"
actual_size="${array[2]}"
Expand Down