Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Created July 11, 2013 20:27
Show Gist options
  • Save christopherhill/5978941 to your computer and use it in GitHub Desktop.
Save christopherhill/5978941 to your computer and use it in GitHub Desktop.
Sample PHP Backup Script. Needs lots of improvements, but a place to start.
<?php
/*
* CRH: 3/24/13
* Backup Script
* - This script will back up all files in public_html to a .tar.gz
* - It will also dump the complete set of mysql databases to a .tar.gz
* - It will send those files to an offsite location via FTP, in addition to locally
* storing them
* - It will send an e-mail with the results
*/
$emailaddress = "user@domain.com";
$target = "/home/user/archives." . date(md) . ".tar.gz";
$dbroot = "root";
$dbpass = "password";
$dbtarget = "/home/user/archives." . date(md) . ".sql";
$ftpserver = "host.backups.com";
$ftpusername = "ftpuser";
$ftpuserpass = "ftppass";
$dbFTPdest = "/archive/archive." . date(md) . ".sql";
$fileFTPdest = "/archive/archive." . date(md) . ".tar.gz";
function sendtoFTP($_server, $_ftp_user_name, $_ftp_user_pass, $_ftp_source, $_ftp_dest) {
$connection = ftp_connect($_server);
if ($connection) {
$login = ftp_login($connection, $_ftp_user_name, $_ftp_user_pass);
if ($login) {
$upload = ftp_put($connection, $_ftp_dest, $_ftp_source, FTP_BINARY);
if (!$upload) {
$ftpstatus = 'Upload failed.';
} else {
$ftpstatus = 'Upload success!';
}
} else {
$ftpstatus = 'Login attempt failed.';
}
ftp_close($connection);
} else {
$ftpstatus = 'Connection attempt failed.';
}
return $ftpstatus;
}
function getSize($size)
{
switch ($size) {
case ($size>=1073741824): $size = round($size/1073741824) . " GB"; break;
case ($size>=1048576): $size = round($size/1048576) . " MB"; break;
case ($size>=1024); $size = round($size/1024) . " KB"; break;
default: $size = $size . " bytes"; break;
}
return $size;
}
// begin flow
if (file_exists($target)) {
unlink($target);
}
if (file_exists($dbtarget)) {
unlink($dbtarget);
}
$fileexec = "tar --create --preserve --gzip --file=" . $target . " /home/user/public_html /home/user/staging";
$dbexec = "mysqldump -p" . $dbpass . " --routines --all-databases --events --flush-privileges > " . $dbtarget;
echo "Creating the Tarball...\r\n";
system($fileexec, $fileresult);
echo "Dumping the Database...\r\n";
system($dbexec, $dbresult);
// upload files
$fileftp = sendtoFTP($ftpserver, $ftpusername, $ftpuserpass, $target, $fileFTPdest);
$dbftp = sendtoFTP($ftpserver, $ftpusername, $ftpuserpass, $dbtarget, $dbFTPdest);
$size = getSize(filesize($target));
$dbsize = getSize(filesize($dbtarget));
$message = "The website file backup has been run.\n\n";
$message .= "\tThe return code was: " . $fileresult . "\n\n";
$message .= "\tThe file path is: " . $target . "\n\n";
$message .= "\tSize of the backup: " . $size . "\n\n";
$message .= "\tServer time of the backup: " . date(" F d h:ia") . "\n\n";
$message .= "The database backup has also been run.\n\n";
$message .= "\tThe return code was: " . $dbresult . "\n\n";
$message .= "\tThe file path is " . $dbtarget ."\n\n";
$message .= "\tSize of the backup: " . $dbsize . "\n\n";
$message .= "\tServer time of the backup: " . date(" F d h:ia") . "\n\n";
$message .= "External FTP Status: ". "\n\n";
$message .= "\tFiles: " . $fileftp . "\n\n";
$message .= "\tDB: " . $dbftp . "\n\n";
$subject = "System Backup Completed: " . date("mdy");
mail($emailaddress, $subject, $message, "From: domain.com <>");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment