Example – Backup MySQL Database using PHP

6

The database backup is a most important task for every web developer. Regular database backup prevents risk to lose the data and it helps to restore the database if any issue occurred. So, backup the database whenever possible is a good idea.

There many ways available to backup MySQL database in a file and you can backup database in one click from hosting server. But if you want to take MySQL database backup without login to your hosting server or phpMyAdmin, you can do it from the script using PHP. In this tutorial, we will build a PHP script to backup MySQL database and save in a SQL file.

Perform MySQL Database Backup using PHP

All the PHP code will be grouped together in backupDatabaseTables() function. Using backupDatabaseTables()function you can backup specific tables or all tables from a database. The following parameters are required to backup MySQL database using PHP.

  • $dbHost – Required. Specifies the host of the database.
  • $dbUsername – Required. Specifies the database username.
  • $dbPassword – Required. Specifies the database password.
  • $dbName – Required. Specifies the database which you wants to backup.
  • $tables – Optional. Specifies the table names in comma separated string or array. Omit this parameter to take backup all tables of the database.

Usage:
Use backupDatabaseTables() function in PHP to generate MySQL database backup and save in a SQL file.

Leave A Reply

Your email address will not be published.