I aleady have a code here and it works when I use it on the command prompt directly using this cd C:\wamp...... to point out the bin and use the following code below to dump:
<?php
DEFINE("HOST","localhost"); // Host Name
DEFINE("USER","root"); // Database Username
DEFINE("PASSWORD",""); // Database Password
DEFINE("DATABASE","db_osgamdrp"); // Database Name
DEFINE("TIMEZONE","Asia/Manila"); // Server Timezone
DEFINE("BIN","C:\wamp\bin\mysql\mysql5.6.12\bin") // MySQLi BIN
$Status = 1;
$Message = "";
$Command = "mysqldump --opt -h " . HOST . " -u " . USER . " -p " . PASSWORD . "" . DATABASE . " > ../../../../../resources/database_backup/" . DATABASE . date("Y-m-d-H-i-s") . ".gz";
if (!exec($Command)) {
$Status = 0;
$Message = "Something Went Wrong. Please Try Again";
}
?>
Whats wrong with this code? Its not working.
Related
I'm testing this script on the local server
$dump_path = "backups";
$host = "localhost";
$user = "root";
$pass = "";
$command=$dump_path.'mysqldump -h '.$host.' -u '.$user.' dental > dental_b.sql';
if (system($command)) {
echo "YES" ;
} else {
echo "Error" ;
}
But why isn't that script executed and shows an error although all data from the DB is true.
I'm pretty sure you don't need the host option if you're using mysqldump on localhost. The way you are concatenating your command looks messed up to me. Let me show you what works for me. I use this all the time:
<?php
$user = "root";
$pass = "";
$database = "dental";
// File name
$file_name = 'dental_b.' . date('mdY.Hia') . '.sql.gz';
// Storage directory
$storage_dir = __DIR__ . '/backups';
if( ! is_dir( $storage_dir ) )
mkdir( $storage_dir, 0777, TRUE );
// Absolute path to new file
$absolute_path = $storage_dir . '/' . $file_name;
// Create the backup file
exec( 'mysqldump -u ' . $user .
' -p' . $pass .
' ' . $database .
' | gzip > ' . $absolute_path );
Yes, I am gzipping the file. I think you'll find this is a good option for storage, emailing, etc.
EDIT ---
If you are not using a password, you should not use -p. You'd want to alter the command in that case.
I run a mongodump command from php:
$cmd = "mongodump --host=" . $host . " --port=" . $port . " --db=" . $db . " --username=". $username . " --collection=" . $collectionName . " --out=" . $path . " -p " . $password;
$res = shell_exec($cmd);
Sometimes the connection to the mongo server is down and the dump operation fails, or even the authorisation to the server fails, but I have no way of knowing it through my code.
I have found a temporary solution, which is far from being a good one, by checking the presence of both .bson and .metadata.json files, while the metadata.json file is the more important check, as if the dump fails in the middle, the .bson file still exists, but the metadata.json file is not present
Is there any other way to verify the result of the dump operation ?
I am Using Postgresql for my PHP project which is built in Codeigniter, the size of this database is very large there are upto 500 tables in my project i have used the utility class for backup but it failed due to size of database now in google search i found a solution pg_dump , but can't find a suitable example for my problem. Here is my code but it only generate an empty sql file.
$username = $this->db->username;
$password = $this->db->password;
$hostname = $this->db->hostname;
$dbname = $this->db->database;
$dataDir = DB_BACKUP;
$pgDumpDir = "/usr/bin/pg_dump";
$dated = date('d-m-Y');
$fileName = $dataDir . "backup_" . $dated . ".sql";
$command = $pgDumpDir;
$command .=" --username ".$username;
$command .= " ".$dbname." > ".$fileName;
echo "ok " . shell_exec($command);
Here is my code:
<?php
$backupfile = 'bkp_dbcreditors_' . date("Y-m-d-H-i-s") . '.sql';
$command = "C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump -u root -pare048 dbcreditors > $backupfile";
system($command);
echo "Backup taken.";
exit();
?>
Is there a way to confirm that backup is not taken, if such happens?
Read the return value of the system($command). http://php.net/manual/en/function.system.php
It returns false on failure.
i am trying to backup mysql database by using this code
include ("functions_cp/f_connection.php");
Sqlconnection() ;
$dbname = "Reservebox";
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "123";
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.sql';
$command = 'mysqldump -h' . $dbhost . ' -u ' . $dbuser . '-p =' . $dbpass . ' '. $dbname . ' > ' . $backupFile ;
system($command);
the script works fine and it generates a .sql file , however this file is empty , how can i fix this problem ?
thanks
Don't put a space or equals sign between the -p and the password. Also, you are missing a space before the -p.
$command = 'mysqldump -h' . $dbhost . ' -u ' . $dbuser . ' -p' . $dbpass . ' '. $dbname . ' > ' . $backupFile ;
Probably a file permissions error. Check that whatever user PHP and MySQL are running as have the permissions to write the file. FOr troubleshooting purposes, chmod the directory to 0777 and see if that fixes the problem. If so, chown the directory to whatever user MySQL is running as.