/tmp/mysql-backup.sql"; $aOutput = ""; //This is the general output for the commands later. $nError = 0; //This is for error handling with the exec stuff later. $sTarCreate = "tar -cvf " . " {$sTemporaryFile}" . " " . join(" ", $aFilesToArchive);; // }}} if(!($oLogFile = fopen($sLogFile, "a+"))) { echo "There has been an error opening the Log File\n"; die; } fwrite($oLogFile, "---------------------------------\n"); fwrite($oLogFile, "New Dump Started: " . date("D d F Y - g:i:s") . "\n"); fwrite($oLogFile, "----------------\n"); fwrite($oLogFile, "Temporary Storage: {$sTemporaryLocation}\n"); fwrite($oLogFile, "Final File: {$sFinalFile}\n"); fwrite($oLogFile, "Compression: {$bCompression}\n"); fwrite($oLogFile, "MySQLDump: {$bMySQLDump}\n"); fwrite($oLogFile, "---------\n\n"); echo "New Dump Started...\n"; // {{{ Step 1: MySQL Dump if($bMySQLDump) { fwrite($oLogFile, "Beginning MySQL5 Dump!\n"); exec("$sMySQLDump 2>&1", $aOutput, $nError); if($nError) { echo "Error in MySQL Dump, unable to continue!\n"; fwrite($oLogFile, "Error in MySQL Dump, error is: \n"); fwrite($oLogFile, join("\n", $aOutput)); fwrite($oLogFile, "\n Error encountered, exiting!"); fclose($oLogFile); die; } fwrite($oLogFile, "Dump Complete, saved at: /tmp/mysql-backup.sql\n"); echo "Dump Complete, saved at: /tmp/mysql-backup.sql\n\n"; } // }}} // {{{ Step 2: Create and populate TAR archive fwrite($oLogFile, "Beginning Output of TAR archive\n"); fwrite($oLogFile, "Outputting: {$sTarCreate}\n"); echo "Beginning Output of the TAR archive...\n"; exec("$sTarCreate 2>&1", $aOutput, $nError); if($nError) { echo "There has been an error creating the tar archive." . PHP_EOL; fwrite($oLogFile, "There has been an error creating the tar archive. The output is: \n"); fwrite($oLogFile, join("\n", $aOutput)); fwrite($oLogFile, "\n The process can't continue, closing and exiting"); fclose($oLogFile); die; } fwrite($oLogFile, "Tar created successfully at: {$sTemporaryFile}\n"); echo "Ending Output of the TAR archive...\n"; fwrite($oLogFile, "Ending Output of the TAR archive\n\n"); // }}} // {{{ Step 3: Move the archive to final location echo "Moving $sTemporaryFile to $sFinalFile\n"; fwrite($oLogFile, "Moving $sTemporaryFile to $sFinalFile\n"); if(!rename("$sTemporaryFile", "$sFinalFile")) { echo "Moving failed...\n"; fwrite($oLogFile, "Move failed..aborting\n"); fclose($oLogFile); die; } fwrite($oLogFile, "Move Successful\n\n"); echo "Move successful\n"; // }}} // {{{ Step 4: Cleans up temporary files echo "Removing Temporary Files\n"; fwrite($oLogFile, "Beginning Cleanup...\n"); fwrite($oLogFile, "Deleting: /tmp/mysql-backup.sql\n"); if(!unlink("/tmp/mysql-backup.sql")) fwrite($oLogFile, "Delete failed\n"); fwrite($oLogFile, "End Cleanup...\n"); echo "Finished Removing Temporary Files\n"; // }}} // {{{ Step 5: Removes old archives echo "Removing Old Archives\n"; fwrite($oLogFile, "Removing Old Archives\n"); $aFileList = glob(dirname($sFinalFile) . "/*.tar"); echo "Current Archive Number: " . sizeof($aFileList) . " Max: " . $nNumBackups; fwrite($oLogFile, "Current Archive Number: " . sizeof($aFileList) . " Max: " . $nNumBackups); if(sizeof($aFileList) > $nNumBackups) { echo "\n"; fwrite($oLogFile, "\n"); rsort($aFileList); if($nNumBackups > 0) $aFileList = array_slice($aFileList, $nNumBackups); foreach($aFileList as $sFile) { echo "Deleting: {$sFile}\n"; fwrite($oLogFile, "Deleting: {$sFile}\n"); unlink($sFile); } } else { echo " skipping\n"; fwrite($oLogFile, " skipping\n"); } fwrite($oLogFile, "Finished Removing Old Archives\n"); echo "Finished Removing Old Archives\n"; // }}} fwrite($oLogFile, "Dump Ended: " . date("D d Y - g:i:s") . "\n"); fwrite($oLogFile, "---------------------------------\n\n"); fclose($oLogFile); echo "Backup Complete - ". date("D d Y - g:i:s") . "\n"; ?>