mysqldump
It’s not that simple. It’s better to use the mysqldump
from PHP with shell_exec
or system
, or simply schedule a cronjob, as suggested.
I particularly use cronjob from a remote system, which calls the mysqldump
, generating my daily backup of my customers' small application database out there.
$this->dbutil->backup()
On one occasion, the server of the annoying provider that hosted the project application I was working on did not allow remote access to Mysql at all. I didn’t have SSH access. O mysqldump
wasn’t an option. I didn’t have one phpMyAdmin of life to generate me a dump.
So that the previous developers had even made their PHP script that made the darlings, since SHOW TABLES
up to everything else you can imagine, and mounted the respective SQL file, containing the dump.
The problem was that it didn’t work. But nobody knew it. Apparently it was going well. But, right strings were truncated. I only "figured it out" because using my local version of the application, I suddenly noticed a slightly different content from the remote application. I was intrigued, and investigating got to the fact that the script didn’t work well enough to generate a dump faithful.
The solution I found was the method backup
of Database Utility Class of Code Igniter. I finally got my dump 100% bulletproof, from PHP.
The implementation of this function that generates the dump is a delicate task. I recommend you take a look at function code backup
in the Code Igniter repository (have to take a look here too). The people I worked on tried to reinvent this wheel and failed.
Therefore, if need get a dump of a Mysql database with PHP, I suggest you use this function, or make your version from this, which has proven to work.
Code example:
// Carrega a classe de utilidades de banco de dados
$this->load->dbutil();
// Coloca o dump de todo o seu banco de dados numa variável
$backup =& $this->dbutil->backup();
// Gravando o dump num arquivo
$this->load->helper('file');
write_file('/path/to/mybackup.gz', $backup);
// Enviando o arquivo para o navegador
$this->load->helper('download');
force_download('mybackup.gz', $backup);
Settings for the dump can be made through a array options, to be used as the first parameter for the backup function. For example:
$prefs = array(
'tables' => array('table1', 'table2'), // Tabelas para incluir no dump
'ignore' => array(), // Tabelas para não incluir no dump
'format' => 'txt', // gzip, zip, txt
'filename' => 'mybackup.sql', // Nome do arquivo - APENAS PARA FORMATO ZIP
'add_drop' => TRUE, // Incluir comandos DROP TABLE no dump
'add_insert' => TRUE, // Incluir os dados das tabelas em comandos INSERT no dump
'newline' => "\n" // Caracter para final de linha a ser usado no dump
);
$this->dbutil->backup($prefs);
Calebe, here are some questions : Does the server run on linux or windows? Do you have access to crontab to schedule some script execution? If it is linux and you have access to crontab, you can schedule the execution of a .sh. Shell would be responsible for calling mysqldump.
– J. Junior
Just to remember, I went through a lot to make mysqldump work, because the websites do not explain that cmd should be executed in the bin folder from where mysql is installed (no use copying the files "mysql.exe" and "mysqldump.exe" to c:/system32 that it does not find the database), oooou... make a previous command that takes cmd to the bin folder, thus "cd c: xampp mysql bin" (in my case it is installed inside xampp). I hope I helped in the instructions, thanks I was looking for how to run in php.
– user18150