🎨 check-backup : improve code readability

move exit conditions on top to avoid if/else nightmare
This commit is contained in:
Pierre Goiffon
2018-11-23 17:57:39 +01:00
parent 8530db347b
commit 44d7abac6e

View File

@@ -214,54 +214,54 @@ catch(Exception $e)
$sZipArchiveFile = MakeArchiveFileName().'.tar.gz'; $sZipArchiveFile = MakeArchiveFileName().'.tar.gz';
echo date('Y-m-d H:i:s')." - Checking file: $sZipArchiveFile\n"; echo date('Y-m-d H:i:s')." - Checking file: $sZipArchiveFile\n";
if (file_exists($sZipArchiveFile)) if (!file_exists($sZipArchiveFile))
{ {
if ($aStat = stat($sZipArchiveFile)) RaiseAlarm("Missing backup file '$sZipArchiveFile'");
{
$iSize = (int) $aStat['size'];
$iMIN = utils::ReadParam('check_size_min', 0);
if ($iSize > $iMIN)
{
echo "Found the archive\n";
$sOldArchiveFile = MakeArchiveFileName(time() - 86400).'.tar.gz'; // yesterday's archive
if (file_exists($sOldArchiveFile))
{
if ($aOldStat = stat($sOldArchiveFile))
{
echo "Comparing its size with older file: $sOldArchiveFile\n";
$iOldSize = (int) $aOldStat['size'];
$fVariationPercent = 100 * ($iSize - $iOldSize) / $iOldSize;
$sVariation = round($fVariationPercent, 2)." percent(s)";
$iREDUCTIONMAX = utils::ReadParam('check_size_reduction_max'); return;
if ($fVariationPercent < -$iREDUCTIONMAX) }
{
RaiseAlarm("Backup file '$sZipArchiveFile' changed by $sVariation, expecting a reduction limited to $iREDUCTIONMAX percents of the original size"); $aStat = stat($sZipArchiveFile);
} if (!$aStat)
elseif ($fVariationPercent < 0) {
{ RaiseAlarm("Failed to stat backup file '$sZipArchiveFile'");
echo "Size variation: $sVariation (the maximum allowed reduction is $iREDUCTIONMAX) \n";
} return;
else }
{
echo "The archive grew by: $sVariation\n"; $iSize = (int)$aStat['size'];
} $iMIN = utils::ReadParam('check_size_min', 0);
} if ($iSize <= $iMIN)
} {
RaiseAlarm("Backup file '$sZipArchiveFile' too small (Found: $iSize, while expecting $iMIN bytes)");
return;
}
echo "Found the archive\n";
$sOldArchiveFile = MakeArchiveFileName(time() - 86400).'.tar.gz'; // yesterday's archive
if (file_exists($sOldArchiveFile))
{
if ($aOldStat = stat($sOldArchiveFile))
{
echo "Comparing its size with older file: $sOldArchiveFile\n";
$iOldSize = (int)$aOldStat['size'];
$fVariationPercent = 100 * ($iSize - $iOldSize) / $iOldSize;
$sVariation = round($fVariationPercent, 2)." percent(s)";
$iREDUCTIONMAX = utils::ReadParam('check_size_reduction_max');
if ($fVariationPercent < -$iREDUCTIONMAX)
{
RaiseAlarm("Backup file '$sZipArchiveFile' changed by $sVariation, expecting a reduction limited to $iREDUCTIONMAX percents of the original size");
}
elseif ($fVariationPercent < 0)
{
echo "Size variation: $sVariation (the maximum allowed reduction is $iREDUCTIONMAX) \n";
} }
else else
{ {
RaiseAlarm("Backup file '$sZipArchiveFile' too small (Found: $iSize, while expecting $iMIN bytes)"); echo "The archive grew by: $sVariation\n";
} }
} }
else
{
RaiseAlarm("Failed to stat backup file '$sZipArchiveFile'");
}
} }
else
{
RaiseAlarm("Missing backup file '$sZipArchiveFile'");
}
?>