How To Back Up The Entire MySql Database with PHP

Have you experienced data loss because the database was never backed up? yes, I just experienced because my hard drive has been damaged. From this experience, I am trying to make a short script to back up the entire MySQL database in bulk. If you have hundreds of databases, and to backup manually definitely exhausting and very boring. For that, you can use a simple script that I created to perform backup the entire MySQL database with PHP. Let’s follow the tutorial below:

My way back up the entire MySQL database with PHP

How To Back Up The Entire MySql Database With PHP

  1. Create a project folder named “backupdb“.
  2. Create 3 pieces PHP file named index.php, dbconn.php, backup.php.
  3. I am using PDO to communicate between PHP and MySql.
  4. Copy the PDO class connection below and save it in dbconn.php.

    the script above is how to use PDO in PHP.

  5. Copy the query class below and save it on file backup.php.

    I keep a collection of database queries into a class backup.

  6. The script below is used to run mysql_dump in PHP and save it in index.php.

A brief description:

The above array is used to hold the database’s name that does not participate to back up

In this example, I create a folder with the format date(‘Y-m-d’) to accommodate the entire back up’s file. Each performs a backup process, the system will check whether the folder already exists or not. If no, the system will create a new folder.

Bulk Mysql Database Backup With Php

Using PHP in_array function, the system will check the database which is not allowed to be backed up.

The following functions are used to check whether a server using windows XAMPP  or Linux.

Then the script will be executed using a command exec($command,$output)

Done, if you test it will be more or less like the following picture:

Back Up Database Mysql With Php

Download full source project how to backup the entire mysql database with php below

To facilitate the process of database backups on a regular basis, you can use crontabs on ubuntu or windows. I have not tried to do, I will update the next time.

Thus my article on How To Back Up The Entire MySql Database with PHP, hope useful

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.