skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER

    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);
    

  2. 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";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search