Skip to main content

Backup MySql Database?



I have asked this question before but I haven't got an answer, so I'm rephrasing it.





I want to back up a db using either:







system("mysqldump -h DB_HOST -u DB_USER -p DB_PASS logindb > $location/$backup");







or:







sql="BACKUP my_db TO DISK my_backup_folder WITH FORMAT #";



if ($stmt = $this->connect->prepare($sql)) {

$stmt->execute();

$stmt->close();

} else {

$error = true;

$message['error'] = true;

$message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;

return json_encode($message);

}







But the first gives me an empty sql file and the second gives me nothing. Why is that, and if there is a way to find out what error occurred how would I do it ?





Also which method is better ?


Comments

  1. Found some simpler code here

    Method 1:

    $tableName = 'mypet';
    $backupFile = 'backup/mypet.sql';
    $query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
    $result = mysql_query($query);


    Method 2:

    $backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
    $command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
    system($command);

    ReplyDelete
  2. The start post:

    -p DB_PASS


    And the right form:

    -p[password]


    I don't know why but the -p is agains the password, not equal to the other parameters. So be aware that you take over the examples well. I have seen different cases where this was the issue so take note of it.

    ReplyDelete
  3. Use try-catch, something like:

    try{
    $stmt = $this->connect->prepare($sql);
    $stmt->execute();
    $stmt->close();
    }
    catch(PDOException $e)
    {
    echo $e->getMessage();
    }


    Check log files for errors.

    ReplyDelete
  4. The first one is faster if you have exec permissions for php.

    Check the SQL error log file.

    /var/log/mysql.err - MySQL Error log file

    /var/log/mysql.log - MySQL log file

    For php you might put error_reporting(E_ALL); this to throw php errors if any.

    ReplyDelete
  5. Make sure that the user have the permission to this database.

    Below is the command to backup the mysql database

    mysqldump -h [host_name] -u [username] -p[password] [database_name] > dumpfilename.sql

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...