I wrote this very simple bash script to check the gzip compressed tar files generated by cPanel's backup. It attempts to extract each file individually to /dev/null and emails a list of the ones that were not extracted successfully.

#!/bin/bash

path="./*.gz"

found_errors=0
errors='The following backup files failed the automatic test: \n'

for f in $path
do
    gunzip -c $f | tar t > /dev/null

    #if the exit status was not 0
    if [ $? -ne 0 ]; then
        found_errors=1
        errors="$errors\n$f"
    fi
done

#if an error was found
if [ $found_erros -ne 0 ]; then
    #email the list of files that could not be extracted/tested
    echo -e $errors | mail -s "Backup Error Check" "admin@example.com"
fi

Lines 8 through 17 loops through the files in the current directory with a .gz extension and extract each one using gunzip and then tar to /dev/null. If the exit status ($? in bash) is not 0 then gunzip or tar was not successful so the file name is concatenated to the variable error which will contain the body of the email.

Lastly the if statement on line 20 will be executed if one or more of the files was not extracted successfully and will send an email containing the list of corrupted files.

One last thing, the script checks the compressed files on the current directory. If you want to schedule the script to run after a backup process completes, replace the 3rd line with the following two lines and set the path to the location of your backups:

date=`date +%Y-%m-%d`
path="/backups/whm/$date/accounts/*.gz"

This way the path will be set according to the current date, so make sure it is scheduled to run on the same day as the backup.

If you found this post useful, I'd be very grateful if you'd help it spread by sharing it. Thank you!