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
Related
I'm trying to write a PHP code that will backup my SQL database daily. However, running it using a Cron Job spits out the above error in the title. My server is hosted on hostgator and its a shared server.
The dbuser has all privileges granted.
Here's my code:
$dbhost = 'localhost';
$dbuser = 'XXXX';
$dbpass = 'XXXX';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);
Any help would be much appreciated!
The following code worked for me:
$DBUSER="XXX";
$DBPASSWD="XXXX";
$DATABASE="XXXXX";
$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > $filename";
passthru( $cmd );
exit(0);
your problem is the space between "-p" and $dbpass. The command mysqldump hopes "-ppassword" together (the response is "using password: no" for this).
Try:
$command = "mysqldump --opt -h $dbhost -u $dbuser -p$dbpass $dbname | gzip > $backupFile";
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 have had written the following snippet that generates the mysql database dump and saves it on the server:
public function save_db_backup()
{
$DBUSER=$this->db->username;
$DBPASSWD=$this->db->password;
$DATABASE=$this->db->database;
$filename = $DATABASE . "-" . date("Y-m-d_H-i-s") . ".sql.gz";
$save_path = $_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/' . $filename;
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > " . $save_path;
exec( $cmd );
}
It has been working fine on my other server. But after moving my site to this new server, it has suddenly stopped working i.e. database backup file is not being saved at the path specified. Also, I have checked exec is enabled on the server plus the directory is readable and also writeable:
is_readable($_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/') // true
is_writable($_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/') // true
I have checked and the database credentials are alright. I have tried with the path to mysqldump and that didn't work either:
$cmd = "/usr/bin/mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > " . $save_path;
What problem could there possibly be?
You should check what user launches the command, and then if the dir is readable and also writeable by THAT user.
If you are launching it from a PHP script, it's the probable cause.
I use this code for download a backup of MySql database but it download a empty file.
Please also guide for restore this backup.
<?php
//connect to database
include'connect.php';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";
system($command);
?>
Replace system with echo to show the generated command, and execute it on the command line manually. Errors are usually shown on STDERR, which isn't caught by the system call, and if you get an empty output that means it couldn't output anything. Fix the error and then fix your code. I'd also use passthru instead of system.
To restore the backup afterwards use (from the commandline):
mysql -u<user> -p <database> < myfile.sql
Your variables are sticked to the options. Try to change this:
$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";
To this:
$command = "mysqldump -h $hostname -u $username -p $password $dbname > $backupFile";
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.