Is it possible to dump a database using doctrine 2? I have read that symfony has a library which extends doctrine to do it but How could I use it in my zendframework project with Bisna Doctrine 2 Integration?
6 Answers
For Symfony2:
Type
php app/console doctrine:schema:create --dump-sql
in the command line

- 3,644
- 9
- 38
- 49
-
13I would like to dump data instated of structure. Do you know? – dextervip Jan 31 '12 at 11:56
-
Great tip! Is there a way to save the output into a file instead of printing it the console? – Aerendir Feb 24 '15 at 19:37
-
5@Aerendir `php app/console doctrine:schema:create --dump-sql > dump.sql` puts the code in a file "dump.sql". which you can then compress with `gzip dump.sql` if you want. – JamesWilson Apr 17 '15 at 16:38
-
what @dextervip is to dump the data. not only structure – GusDeCooL Jul 25 '15 at 04:03
This is an old thread but I was just doing something similar in Symfony and decided to develop an actual command for it. That's more of a Symfony way of doing it and gives you more control on the output as well as allowing you access to the parameters, so you don't have to parse Yaml using bash script :)
namespace Fancy\Command;
use Fancy\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
class DatabaseDumpCommand extends AbstractCommand
{
/** @var OutputInterface */
private $output;
/** @var InputInterface */
private $input;
private $database;
private $username;
private $password;
private $path;
/** filesystem utility */
private $fs;
protected function configure()
{
$this->setName('fancy-pants:database:dump')
->setDescription('Dump database.')
->addArgument('file', InputArgument::REQUIRED, 'Absolute path for the file you need to dump database to.');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->database = $this->getContainer()->getParameter('database_name') ;
$this->username = $this->getContainer()->getParameter('database_user') ;
$this->password = $this->getContainer()->getParameter('database_password') ;
$this->path = $input->getArgument('file') ;
$this->fs = new Filesystem() ;
$this->output->writeln(sprintf('<comment>Dumping <fg=green>%s</fg=green> to <fg=green>%s</fg=green> </comment>', $this->database, $this->path ));
$this->createDirectoryIfRequired();
$this->dumpDatabase();
$output->writeln('<comment>All done.</comment>');
}
private function createDirectoryIfRequired() {
if (! $this->fs->exists($this->path)){
$this->fs->mkdir(dirname($this->path));
}
}
private function dumpDatabase()
{
$cmd = sprintf('mysqldump -B %s -u %s --password=%s' // > %s'
, $this->database
, $this->username
, $this->password
);
$result = $this->runCommand($cmd);
if($result['exit_status'] > 0) {
throw new \Exception('Could not dump database: ' . var_export($result['output'], true));
}
$this->fs->dumpFile($this->path, $result);
}
/**
* Runs a system command, returns the output, what more do you NEED?
*
* @param $command
* @param $streamOutput
* @param $outputInterface mixed
* @return array
*/
protected function runCommand($command)
{
$command .=" >&1";
exec($command, $output, $exit_status);
return array(
"output" => $output
, "exit_status" => $exit_status
);
}
}
and AbstractCommand is just a class that extends symfony's ContainerAwareCommand:
namespace Fancy\Command;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
abstract class AbstractCommand extends ContainerAwareCommand
{
}

- 421
- 5
- 8
-
2
-
The only problem here is that you are writing the whole db content into RAM (`$result`). A streaming solution would be better. – fishbone May 11 '20 at 12:10
Doctrine has no database-dump feature. I agree it would be nice, but it's also not the ORM's goal.
You could dump the database using
- a PHP script
- a system mysqldump
- phpMyAdmin

- 9,312
- 10
- 63
- 86
-
4
-
1@aryansingh here's a backup of the page, but this answer is probably outdated now: https://web.archive.org/web/20170430091324/http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx – Julien Apr 14 '20 at 23:56
I created a small script that read the parameters from app/config/parameters.yml
and output all the data from a MySQL database to a file (with current datetime used as name).
Save this in the root of your Symfony project (e.g. mysqldump.sh
):
#!/bin/bash
# See http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in/23905052#23905052
ROOT=$(readlink -f $(dirname "$0"))
cd $ROOT
# Get database parameters
dbname=$(grep "database_name" ./app/config/parameters.yml | cut -d " " -f 6)
dbuser=$(grep "database_user" ./app/config/parameters.yml | cut -d " " -f 6)
dbpassword=$(grep "database_password" ./app/config/parameters.yml | cut -d " " -f 6)
filename="$(date '+%Y-%m-%d_%H-%M-%S').sql"
echo "Export $dbname database"
mysqldump -B "$dbname" -u "$dbuser" --password="$dbpassword" > "$filename"
echo "Output file :"
ls -lh "$filename"
Result when running the script:
$ bash mysqldump.sh
Export […] database
Warning: Using a password on the command line interface can be insecure.
Output file :
-rw-rw-r-- 1 […] […] 1,8M march 1 14:39 2016-03-01_14-39-08.sql

- 10,259
- 10
- 67
- 98
-
Please put this into a public gist so we can pull it with wget! Makes life so much easier :) – Fuzzyma Aug 08 '16 at 10:30
-
@Fuzzyma here is the [gist](https://gist.github.com/alexislefebvre/0b2d93b2c78a064b49a6c912deff5ab8). How do you use it? By downloading it and executing it directly? Like `wget https://gist.githubusercontent.com/alexislefebvre/0b2d93b2c78a064b49a6c912deff5ab8/raw/3da6188de911c25b1d3e6c3b3146e864c220d595/symfony_mysqldump.sh ; bash ./symfony_mysqldump.sh`? – A.L Aug 08 '16 at 11:08
-
1jep - thats my intention. I want to use wgets feature of directly run a sript after downloding: `wget -O - http://foo.bar/pathToScript.sh | bash` – Fuzzyma Aug 08 '16 at 11:18
-
2Instead of `cut -d " " -f 6` I would suggest you use `awk '{print $2}'`. That way you won't care about spacing – Radu C Aug 24 '16 at 12:39
For a more generic doctrine way:
protected function execute(InputInterface $input, OutputInterface $output)
{
$conn = $this->getDoctrineConnection('default');
$path = $input->getArgument('filepath');
if (! is_dir(dirname($path))) {
$fs = new Filesystem();
$fs->mkdir(dirname($path));
}
$cmd = sprintf('mysqldump -u %s --password=%s %s %s > %s',
$conn->getUsername(),
$conn->getPassword(),
$conn->getDatabase(),
implode(' ', ['variables', 'config']),
$path
);
exec($cmd, $output, $exit_status);
}

- 21
- 1
Depend on your database. if you use mysql, create a php command to utilise mysqldump
like running this
mysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql

- 5,639
- 17
- 68
- 102