#!/usr/bin/env php $db_file_tmp " ; print "Backing up database...\n"; $fp = popen($cmd, "r"); if (empty($fp)) { $error = "Unable to run command '$cmd'"; return($error); } $retval = pclose($fp); if ($retval != 0) { $error = "Command '$cmd' returned value: $retval"; return($error); } print "Done!\n"; // // Move the temp file into the current directory (with a new name) // so that it can be included in the tarball. // if (!rename($db_file_tmp, $db_file)) { $error = "Renaming file '$db_file_tmp' to '$db_file' failed"; return($error); } // // Get a list of all files in the current directory, filter out the // current and parent directories, and any existing backup files. // Trying to make GNU tar's --exclude switch actually work for me // was like trying to herd housecats that were hopped up on crack. // It just wasn't going to happen. :-) Plus, this approach is // more portable. // $file_list = ""; $fp = opendir("."); while ($file = readdir($fp)) { // // Skip the current and parent directory // if ($file == "." || $file == "..") { continue; } // // Skip any backup files // if (strstr($file, "backup-")) { continue; } $file_list .= $file . " "; } if (!$fp) { $error = "Unable to open current directory"; return($error); } closedir($fp); // // Finally, add in our database backup. It's excluded since it's // a backup file just in case there's multiple database dumps lying // around from previous backups that were aborted. But we want to // explicitly add in the dump from *this* run of backup. // $file_list .= $db_file; // // Now tar up the contents of this directory // $backup_tmp = tempnam("/tmp", "backup-htdocs-"); // // This includes a six digit random number to keep attackers from // guessing the filename. // $backup_file = "backup-" . $date_string . "-" . mt_rand(100000, 999999) . ".gz"; $cmd = "tar cfz $backup_tmp $file_list 2>&1"; print "Backing up filesystem...\n"; $fp = popen($cmd, "r"); if (empty($fp)) { $error = "Unable to run command '$cmd'"; return($error); } while ($line = fgets($fp)) { print "tar output: $line"; } $retval = pclose($fp); if ($retval != 0) { $error = "Command '$cmd' returned value: $retval"; return($error); } print "Done!\n"; // // Now remove the database file, we don't need it anymore. // if (!@unlink($db_file)) { $error = "Unable to delete file '$db_file'"; return($error); } // // Finally, move the tarball into this directory so the user can grab it // if (!rename($backup_tmp, $backup_file)) { $error = "Renaming file '$backup_tmp' to '$backup_file' failed"; return($error); } // // Make our backup file world-readable. // if (!chmod($backup_file, 0644)) { $error = "chmod() failed"; return($error); } // Assume success return(null); } // End of main() if ($error = main()) { print $argv[0] . ": Error: $error\n"; exit (1); } ?>