I needed to create a web based backup system and the natural choice of language for the task was php (although I am looking into some perl modules as well). After tweaking with the thought of using a shell command to run a tar command I looked into PEAR’s Archive_Tar package. The class is fairly straightforward to use, my code to backup one particular directory was:
error_reporting(E_ALL); ini_set('display_errors', 1); require_once 'Archive/Tar.php'; PEAR::setErrorHandling(PEAR_ERROR_DIE); $tar = new Archive_Tar("data.tar"); $dir = "../"; $handle = opendir("$dir"); while (false !== ($file = readdir($handle))) { echo "$file \n"; echo ""; $tar->add($dir . "/" . $file) or die ("Could not add file!"); } closedir($handle);
Telling PEAR to die on errors, and spit out the error message was especially helpful when debugging the script. In order for the script to run from a browser, the user running the script (apache in my case), must have write permissions on the directory the script is running from.
Tags: Archive_Tar, Backup, PEAR, PHP