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.
Related
I'm attempting to run a php script during a cron job to dump my MySQL database to a folder for a backup.
$DBHost = 'localhost';
$DBName = 'mydatabase';
$DBUser = 'myuser';
$DBPassword = 'mypassword';
$PATH = "/home/mysite/Backups/";
$FILE_NAME = "backup-" . date( "Y-m-d_H:i:s" ) . ".sql.gz";
exec( '/usr/local/bin/mysqldump -u ' . $DBName . ' -p' . $DBPassword . ' ' . $DBName . ' | gzip --best > ' . $PATH . $FILE_NAME );
But I keep getting the error:
sh: -c: line 0: unexpected EOF while looking for matching `)'
sh: -c: line 1: syntax error: unexpected end of file
I have checked all the ) and can't find any non-matching.
If I comment out the exec command then I do not get the error.
Any one see what I am doing wrong?
Why don't write whole code in sh?
#!/bin/bash
dbname="database"
dbuser="user"
dbpass="password"
path="/home/backup"
now=$(date + "%Y-%m-%d")
/usr/bin/mysqldump -u $dbuser -p$dbpass $dbname | gzip > $path/backup_$now.sql.gz
save it as e.g. backup.sh,
and then use crontab -e to add new cron job (e.g. 2:15 daily)
15 2 * * * /home/backup/backup.sh
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 need one script that when I open the script.php page for example, that export me one database from MySQL to an SQL document, and I don't know how to do it in PHP, can some one help me with that?
Edited Code:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpwd = "";
$dbname = "activmanagement";
$dumpfile = $dbname . "_" . date("Y-m-d_H-i-s") . ".sql";
passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
// report - disable with // if not needed
// must look like "-- Dump completed on ..."
echo "$dumpfile "; passthru("tail -1 $dumpfile");
?>
This line:
passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
Refer PHP - exec() vs system() vs passthru(). You just need something like this
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'activmanagement';
$sql_backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
$dump = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $sql_backup_file";
system($dump);
?>
if($_POST['submit'])
{
$dbhost = 'localhost';
$DBUSER = 'dbuser';
$DBPASSWD = 'dbpass';
$DATABASE = 'db';
$backupPath = '/home/*****/public_html/admin/sql';
$filename = "backup-" . date("d-m-Y");
$cmd = "mysqldump --complete-insert --create-options --add-locks --disable-keys --extended-insert --quick --quote-names -u $user --password=$DBPASSWD $DATABASE|gzip --fast -c >{$backupPath}/$filename.sql.gz";
exec($cmd);
echo $cmd;
}
This is the code i want to use for dumping database. But i never get any backup to specified directory, it always show the error which i have mentioned on my code. Ca anyone tell me whats the problem? Thank you.
N.B
Update this one -u $user with this --user $DBUSER $user but nothing happened.
Here is the sol-
$filename = $backupPath . '/DB_' . time(). '.gz';
if (file_exists($filename))
{
unlink($filename);
}
$cmd = 'mysqldump --opt -h '.$dbhost.' -u ' . $DBUSER . ' -p\'' . $DBPASSWD. '\' ' . $DATABASE . ' | gzip > ' . $filename;
exec($cmd);
echo $cmd;
Thank you all for your help.
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);
?>