Created
December 18, 2009 14:31
-
-
Save kennethreitz/259515 to your computer and use it in GitHub Desktop.
PHP script to backup all sites on a MediaTemple server to an S3 bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
set_time_limit(0); | |
$nDays = 300; // How many days of backups to keep? | |
// MySQL variables | |
$bBackupMySQL = true; // Do you want to backup MySQL databases? | |
$mysql = '/usr/bin/mysql'; | |
$mysqldump = '/usr/bin/mysqldump'; | |
$mysql_server = 'xxxx.com'; | |
$mysql_username = 'xxxx'; | |
$mysql_password = 'xxxx'; | |
// httpdocs variables | |
$bBackuphttpd = true; // Do you want to backup httpd (web) docs? | |
$cVhostsDir = '/var/www/vhosts/'; // Include trailing slash | |
// Used to compress backup data | |
$tar = '/bin/tar'; | |
// Where you'll be running the script, most likely | |
$workingDir = '/root/backups/'; | |
// Amazon S3 set-up | |
$bAMZNS3 = true; // Backup to S3? | |
if ($bAMZNS3) { | |
$accessKeyId = 'xxxxxx'; | |
$secretAccessKey = 'xxxxxxx'; | |
$bucketName = 'xxxxxxx'; | |
/** | |
* Amazon S3 PHP class | |
* | |
* @link http://undesigned.org.za/2007/10/22/amazon-s3-php-class | |
*/ | |
include('s3-php5-curl/S3.php'); | |
} // ends if ($bAMZNS3) | |
// FTP set-up | |
$bFTP = false; // Backup to FTP? | |
if ($bFTP) { | |
$ftp_server = '127.0.0.1'; | |
$ftp_username = 'ftpuser'; | |
$ftp_password = 'ftppass!'; | |
} // ends if ($bFTP) | |
/************************************** | |
| What suffix to add to filenames | |
| if daily, make it the date | |
| if you're going to run this hourly, or 2x / day, make it unique | |
***************************************/ | |
$suffix = date("Ymd"); | |
// Done editing script | |
chdir($workingDir); | |
$oMysql = mysql_connect($mysql_server, $mysql_username, $mysql_password); | |
if ($bAMZNS3) { | |
$s3 = new S3($accessKeyId, $secretAccessKey); | |
} // ends if ($bAMZNS3) | |
if ($bFTP) { | |
$oFTP = ftp_connect($ftp_server); | |
ftp_login($oFTP, $ftp_username, $ftp_password); | |
} // ends if ($bFTP) | |
/////////////////////////////////////////////////////////// | |
// Mysql Database Backups | |
/////////////////////////////////////////////////////////// | |
if ($bBackupMySQL) { | |
// Get a list of the databases | |
$oDBList = mysql_list_dbs($oMysql); | |
while ($rsData = mysql_fetch_object($oDBList)) { | |
$cDatabase = $rsData->Database; | |
$cSQLFilename = $cDatabase . '_' . $suffix . '.sql'; | |
$cTarGZFilename = $cSQLFilename . '.tar.gz'; | |
// Dump them | |
$cliResult = `$mysqldump -u$mysql_username -p$mysql_password $cDatabase > $cSQLFilename`; | |
// Tar them up | |
$cliResult = `$tar -cpzf $cTarGZFilename $cSQLFilename`; | |
unlink($cSQLFilename); | |
if ($bAMZNS3) { | |
// Send to Amazon | |
$s3->putObjectFile($cTarGZFilename, $bucketName, $cTarGZFilename, S3::ACL_PRIVATE); | |
} // ends if ($bAMZNS3) | |
if ($bFTP) { | |
// FTP to server | |
ftp_put($oFTP, $cTarGZFilename, $cTarGZFilename, FTP_BINARY); | |
} // ends if ($bFTP) | |
echo("Saved $cTarGZFilename\n"); | |
// Cleanup | |
unlink($cTarGZFilename); | |
} // ends while ($rsData = mysql_fetch_object($oDBList)) | |
} // ends if ($bBackupMySQL) | |
/////////////////////////////////////////////////////////// | |
// Ends MySQL Backups | |
/////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////// | |
// Mysql httpdocs Backups | |
/////////////////////////////////////////////////////////// | |
if ($bBackuphttpd) { | |
// Get a list of the sites | |
chdir($cVhostsDir); | |
if ($oDir = opendir($cVhostsDir)) { | |
while (false !== ($cFilename = readdir($oDir))) { | |
if (!in_array($cFilename, array('.', '..', 'chroot', 'default', '.skel'))) { | |
$cTarGZFilename = $cFilename . '_' . $suffix . '.tar.gz'; | |
$cTarGZPath = $workingDir . $cTarGZFilename; | |
$cSourceFiles = $cFilename . '/httpdocs/'; | |
// Tar them up | |
`$tar -cpzf $cTarGZPath $cSourceFiles`; | |
if ($bAMZNS3) { | |
// Send to Amazon | |
$s3->putObjectFile($cTarGZPath, $bucketName, $cTarGZFilename, S3::ACL_PRIVATE); | |
} // ends if ($bAMZNS3) | |
if ($bFTP) { | |
// FTP to server | |
ftp_put($oFTP, $cTarGZFilename, $cTarGZPath, FTP_BINARY); | |
} // ends if ($bFTP) | |
echo("Saved $cTarGZFilename\n"); | |
// Cleanup | |
unlink($cTarGZPath); | |
} // ends if (!in_array($cFilename, array(... | |
} // ends while (false !== ($cFilename = readdir($oDir))) | |
closedir($oDir); | |
} // ends if ($oDir = opendir('/var/www/vhosts/')) | |
chdir($workingDir); | |
} // ends if ($bBackuphttpd) | |
/////////////////////////////////////////////////////////// | |
// Ends httpdocs backups | |
/////////////////////////////////////////////////////////// | |
echo("Done writing backup - starting cleanup.\n"); | |
/////////////////////////////////////////////////////////// | |
// Cleanup old files from the server | |
/////////////////////////////////////////////////////////// | |
if ($bAMZNS3) { | |
$aS3Files = $s3->getBucket($bucketName); | |
while (list($cFilename, $rsFileData) = each($aS3Files)) { | |
if ($rsFileData['time']+($nDays*24*60*60) < time()) { | |
$s3->deleteObject($bucketName, $cFilename); | |
} // ends if ($tsModTime+432000 < time(void)) | |
} // ends while (list($cFilename, $rsFileData) = each($s3->getBucket($bucketName))) | |
} // ends if ($bAMZNS3) | |
if ($bFTP) { | |
$list=@ftp_rawlist($oFTP, '.') ; | |
$items=array() ; | |
foreach($list as $_) | |
preg_replace( | |
'`^(.{10}+)(\s*)(\d{1})(\s*)(\d*|\w*)'. | |
'(\s*)(\d*|\w*)(\s*)(\d*)\s'. | |
'([a-zA-Z]{3}+)(\s*)([0-9]{1,2}+)'. | |
'(\s*)([0-9]{2}+):([0-9]{2}+)(\s*)(.*)$`Ue', | |
'$items[]=array( | |
"rights"=>"$1", | |
"number"=>"$3", | |
"owner"=>"$5", "group"=>"$7", | |
"file_size"=>"$9", | |
"mod_time"=>"$10 $12 $14:$15", | |
"file"=>"$17", | |
"type"=>print_r((preg_match("/^d/","$1"))?"dir":"file",1));', | |
$_) ; # :p | |
// Remove anything older than 5 days | |
foreach ($items as $rsFTPFile) { | |
$cFilename = trim($rsFTPFile['file']); | |
$tsModTime = strtotime($rsFTPFile['mod_time']); | |
if ($tsModTime+($nDays*24*60*60) < time()) { | |
ftp_delete($oFTP, $cFilename); | |
} // ends if ($tsModTime+432000 < time(void)) | |
} // ends foreach ($items as $rsFTPFile) | |
ftp_close($oFTP); | |
} // ends if ($bFTP) | |
/////////////////////////////////////////////////////////// | |
// Ends cleanup | |
/////////////////////////////////////////////////////////// | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't access to ftp