/**
* Backs up database and/or assets to a designated folder,
* and packs up the files into a single sspak.
*
* @param DNDataTransfer $dataTransfer
* @param DeploynautLogFile $log
*/
protected function dataTransferBackup(DNDataTransfer $dataTransfer, DeploynautLogFile $log)
{
$environment = $dataTransfer->Environment();
$name = $environment->getFullName();
// Associate a new archive with the transfer.
// Doesn't retrieve a filepath just yet, need to generate the files first.
$dataArchive = DNDataArchive::create();
$dataArchive->Mode = $dataTransfer->Mode;
$dataArchive->AuthorID = $dataTransfer->AuthorID;
$dataArchive->OriginalEnvironmentID = $environment->ID;
$dataArchive->EnvironmentID = $environment->ID;
$dataArchive->IsBackup = $dataTransfer->IsBackupDataTransfer();
// Generate directory structure with strict permissions (contains very sensitive data)
$filepathBase = $dataArchive->generateFilepath($dataTransfer);
mkdir($filepathBase, 0700, true);
$databasePath = $filepathBase . DIRECTORY_SEPARATOR . 'database.sql';
// Backup database
if (in_array($dataTransfer->Mode, array('all', 'db'))) {
$log->write(sprintf('Backup of database from "%s" started', $name));
$command = $this->getCommand('data:getdb', 'db', $environment, array('data_path' => $databasePath), $log);
$command->run(function ($type, $buffer) use($log) {
$log->write($buffer);
});
if (!$command->isSuccessful()) {
$this->extend('dataTransferFailure', $environment, $log);
throw new RuntimeException($command->getErrorOutput());
}
$log->write(sprintf('Backup of database from "%s" done', $name));
}
// Backup assets
if (in_array($dataTransfer->Mode, array('all', 'assets'))) {
$log->write(sprintf('Backup of assets from "%s" started', $name));
$command = $this->getCommand('data:getassets', 'web', $environment, array('data_path' => $filepathBase), $log);
$command->run(function ($type, $buffer) use($log) {
$log->write($buffer);
});
if (!$command->isSuccessful()) {
$this->extend('dataTransferFailure', $environment, $log);
throw new RuntimeException($command->getErrorOutput());
}
$log->write(sprintf('Backup of assets from "%s" done', $name));
}
$sspakFilename = sprintf('%s.sspak', $dataArchive->generateFilename($dataTransfer));
$sspakFilepath = $filepathBase . DIRECTORY_SEPARATOR . $sspakFilename;
try {
$dataArchive->attachFile($sspakFilepath, $dataTransfer);
$dataArchive->setArchiveFromFiles($filepathBase);
} catch (Exception $e) {
$log->write($e->getMessage());
throw new RuntimeException($e->getMessage());
}
// Remove any assets and db files lying around, they're not longer needed as they're now part
// of the sspak file we just generated. Use --force to avoid errors when files don't exist,
// e.g. when just an assets backup has been requested and no database.sql exists.
$process = new Process(sprintf('rm -rf %s/assets && rm -f %s', $filepathBase, $databasePath));
$process->run();
if (!$process->isSuccessful()) {
$log->write('Could not delete temporary files');
throw new RuntimeException($process->getErrorOutput());
}
$log->write(sprintf('Creating sspak file done: %s', $dataArchive->ArchiveFile()->getAbsoluteURL()));
}