The command to get just data is:
mysqldump --no-create-info --no-create-db --opt db_name > latest_data_db_name.sql
Otherwise with example 3 you get both create and inserts statements. I use this command line script when I want to backup databases:
<?php
// usage:
// php backup.php [backup type (default 1), [db list (csv list)]]
// example:
// php backup.php 2 project_a,project_b
// the above will backup two databases,
// by omitting the second argument (db list)
// it will backup all databases defined
// in the hardcoded list
$type = array_key_exists(1, $argv) ? $argv[1] : 1;
$list = array_key_exists(2, $argv) ? $argv[2] : FALSE;
$user = '';
$pass = '';
if($list)
$dbs = array_map('trim', explode(',', $list));
// hardcoded databases list
else
$dbs = [
'db_name',
'another_database'
];
switch ($type) {
// full, no routines
case 1:
array_walk($dbs, function($db) use($user, $pass) {
exec("mysqldump -u{$user} -p{$pass} $db > full_{$db}.sql");
});
break;
// only create statements
case 2:
array_walk($dbs, function($db) use($user, $pass) {
exec("mysqldump -u{$user} -p{$pass} --no-data $db > create_{$db}.sql");
});
break;
// only data
case 3:
array_walk($dbs, function($db) use($user, $pass) {
exec("mysqldump -u{$user} -p{$pass} --no-create-info --no-create-db $db > data_{$db}.sql");
});
break;
// only routines
case 4:
array_walk($dbs, function($db) use($user, $pass) {
exec("mysqldump -u{$user} -p{$pass} --routines --no-create-info --no-data --no-create-db --skip-opt $db > routines_{$db}.sql");
});
break;
}