Skip to content Skip to sidebar Skip to footer

Create / Export MySQL Database backup using PHP

Reading Time: < 1 minute

How can you create a MySQL Database backup using PHP.If you think the database is too large as GB how do you get a fastest backup?In this post will describes how to create a MySQL Database backup/dump quickly instead of using phpmyadmin access using PHP.Also you can save your time to get backup.

Following function will creating/export your database.Only you have to call to this function with passing database parameters.

Now you have to call to above function to create a backup file.

That’s only.

  • How to create backup file as gz format?

  • Put the following script with replacing line 77,78,79.

    	$data = gzencode($data, 9);
    	$handle = fopen($DbName.'-Database-Backup-'.date('Y-m-d @ h-i-s').'.sql.gz','w+');
    	fwrite($handle, $data);
  • How to create backup for specific table?

  • Simply pass the table names to the function as follows.

    $backup_response = backup_Database('localhost','root','','school_db','users');  // backup "users" table
  • How to create backup for more than one tables?

  • Pass the table names as string.

     $tables = 'users,country,states';
     $backup_response = backup_Database('localhost','root','','school_db',$tables);

This method I tested more than ten times using 2GB large MySQL database file.If you want to get database backup daily you can put this file in to the cpanel as CRON job.

Download Create/Export MySQL Database backup using PHP Example(2 KB)