Created
February 9, 2010 01:20
-
-
Save jasonbouffard/298804 to your computer and use it in GitHub Desktop.
backupTask provides a method to dump your database to Amazon's S3 for a symfony application
This file contains hidden or 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
<?php | |
/** | |
* backupTask provides a method to dump your database to Amazon's S3 for a symfony application. | |
* The S3 php class used: http://undesigned.org.za/2007/10/22/amazon-s3-php-class | |
* | |
* @author Jason Bouffard | |
*/ | |
class backupTask extends sfBaseTask | |
{ | |
protected function configure() | |
{ | |
$this->addOptions(array( | |
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'), | |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'prod'), | |
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'), | |
)); | |
$this->namespace = 'backup'; | |
$this->name = 's3'; | |
$this->briefDescription = 'Backs up db to S3'; | |
$this->detailedDescription = <<<EOF | |
The [backup-db|INFO] task does things. | |
Call it with: | |
[php symfony backup-db|INFO] | |
EOF; | |
} | |
protected function execute($arguments = array(), $options = array()) | |
{ | |
// initialize the database connection | |
$databaseManager = new sfDatabaseManager($this->configuration); | |
$connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection(); | |
// add your code here | |
$file = sfConfig::get('sf_cache_dir').'/db_backup.yml'; | |
$gzipFile = sfConfig::get('sf_cache_dir').'/db_backup.yml.gz'; | |
require_once sfConfig::get('sf_lib_dir').'/vendor/amazon/S3.php'; | |
$bucket = sfConfig::get('app_aws_bucket'); | |
$fileName = 'db_backup-'.date('YmdHis').'.yml'; | |
$this->runTask('doctrine:data-dump', array($file)); | |
$this->getFilesystem()->execute('gzip -9c ' . $file . ' > ' . $gzipFile); | |
$this->logSection('S3', 'Putting backup file: '.$fileName.' in bucket: ' . $bucket); | |
$s3 = new S3(sfConfig::get('app_aws_key'), sfConfig::get('app_aws_secret'), false); | |
$s3->putObject($s3->inputFile($gzipFile, false), $bucket, $fileName, S3::ACL_PRIVATE); | |
$this->getFilesystem()->remove(array($file, $gzipFile)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment