用于检查tar.gz文件的bash脚本

时间:2013-12-21 08:29:11

标签: bash centos

我创建了以下脚本,我想在创建cPanel备份(每个网站1 .tar.gz文件)后运行以检查压缩文件。它并不意味着取代手动测试恢复,而只是一个额外的检查。问题是,不是重新生成未通过检查的文件列表,而是返回所有文件的列表。

#!/bin/bash

date=`date +%Y-%m-%d`
path="/backups/$date/*.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 [ $?=0 ]; then
                found_errors=1
                errors="$errors\n$f"
                #echo  $f ": Exit status code is " $?
        fi
done

#if an error was found
if [ $found_erros!=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

提前致谢。

1 个答案:

答案 0 :(得分:0)

似乎有一个错字。我敢打赌,对状态的检查应该是

#if the exit status was not 0
if [ $? -ne 0 ]; then