I use this script to create a mysqpl dump and then upload the file to dropbox. If i run the script at first nothing happens -> blank page. But when i refresh the page everything works fine
<?php
######## einstellungen #############################################
$db_name = "name";
$db_passwd = "pw";
$sql_file = "dump_" . $db_name . "_" . date('Ymd_Hi') . ".sql";
####################################################################
exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names $db_name > $sql_file");
exec("gzip $sql_file");
$datei = $sql_file . ".gz";
$link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$link = str_replace(basename(__FILE__),$datei,$link);
require 'DropboxUploader.php';
while (!file_exists($datei)) sleep(1);
$uploader = new DropboxUploader('email', 'pw');
$uploader->upload($datei);
?>
Related
There is my Function to download backup is sql format
public function actionBackup()
{
// Set the database access credentials
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'fads';
// Set the backup filename and path
$backup_file = 'backup.sql';
$backup_path = "C:/Users/" . get_current_user() . "/Downloads/" . $backup_file;
// Execute the mysqldump command
$path = "C: \ xampp_7.3\mysql\bin";
$command = "mysqldump -u {$db_username} -p{$db_password} {$db_name} > {$backup_path}";
// echo $path.$command;die;
exec("$path > $command");
// Send the backup file as a download
Yii::$app->response->sendFile($backup_path);
// Delete the backup file
unlink($backup_path);
}
It's download backup.sql but its empty i don't understand that where is my code wrong and why i received blank backup.sql
First look if you use correct path for command and backup file.
Avoid spaces in paths.
Try this code:
public function actionBackup()
{
// Set the database access credentials
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'fads';
// Set the backup filename and path
$backup_file = 'backup.sql';
$backup_path = "C:\Users\" . get_current_user() . "\Downloads\" . $backup_file;
// Execute the mysqldump command
$command = "\"C:\\xampp_7\\mysql\\bin\\mysqldump.exe\""; // path to mysqldump command
// $commandParams = "-u {$db_username} -p{$db_password} {$db_name} > {$backup_path}"; // mysqldump params
$commandParams = "--host={$db_host} --user={$db_username} --password={$db_password} {$db_name} > {$backup_path}"; // mysqldump params
exec("$command $commandParams");
// Send the backup file as a download
Yii::$app->response->sendFile($backup_path);
// Delete the backup file
unlink($backup_path);
}
Updated the answer
I trying to backup my database using mysqldump. The file is being generated but the file is empty. How can I fix this?
PHP Code:
$dbuser = 'demo';
$dbpass = 'demo';
$host = 'localhost';
$dbname = 'demo';
$filename = __DIR__ . '/backup/'. $dbname . '_'.$date.'.sql';
exec("mysqldump --user={$dbuser} --password={$dbpass} --host={$host} $dbname > {$filename}");
I have been seriously searching for an SQL code to backup my database. I use xampp as my local server and also phpmyadmin. I simply want to do something like:
<?php
$conn = mysqli_connect("localhost", "root", " ", "products");
if(!$conn){
die("Unable to connect ".mysql_error());
}else{
$backup = "BACKUP DATABASE products";
$backup_query = mysqli_query($conn, $backup)
}
?>
How do I backup my database and output it in .sql format on the local computer and possibly upload to recover a damaged database?
Thank you so much!
You can use the shell statement "mysqldump" and call it with php like:
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpassword = 'password';
$dbname = 'datenbankname';
$dumpfile = 'backups/' . $dbname . '_' . date("Y-m-d_H-i-s") . '.sql.gz';
passthru("mysqldump --user=$dbuser --password=$dbpassword --host=$dbhost $dbname | gzip -c > $dumpfile");
echo "-- Dump finished -- ";
echo $dumpfile;
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 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.