Backup and restore mysql database using php script

myphp-backup

Perform simple and fast MySQL backup/restore using PHP. You can use it to dump a full database or only some tables.

Show

It requires PHP 5.0.5 or later.

More information: Using PHP to backup MySQL databases

Usage

Backup:

Simply upload myphp-backup.php script to the DocumentRoot directory of your web application via FTP or other method and run it accessing http://www.example.com/myphp-backup.php. You can also run it from command line.

Don't forget to set your database access credentials before performing any backup editing these lines from myphp-backup.php script:

/**
 * Define database parameters here
 */
define("DB_USER", 'your_username');
define("DB_PASSWORD", 'your_password');
define("DB_NAME", 'your_db_name');
define("DB_HOST", 'localhost');

define("BACKUP_DIR", 'myphp-backup-files'); // Comment this line to use same script's directory ('.')
define("TABLES", '*'); // Full backup
//define("TABLES", 'table1, table2, table3'); // Partial backup
define("CHARSET", 'utf8');
define("GZIP_BACKUP_FILE", true); // Set to false if you want plain SQL backup files (not gzipped)
define("DISABLE_FOREIGN_KEY_CHECKS", true); // Set to true if you are having foreign key constraint fails

By default backup files will be called myphp-backup-{DB_NAME}-YYYYmmdd-HHMMSS.sql.gz and stored in myphp-backup-files subdirectory. Example output backup file:

myphp-backup-files/myphp-backup-daniloaz-20170828-131745.sql.gz

Restore:

Upload myphp-restore.php script to your DocumentRoot directory and your backup file to a subdirectory called myphp-backup-files. Then simply run the script accessing http://www.example.com/myphp-restore.php or from command line.

You can change the backup filename and subdirectory editing these lines. Don't forget to set your user credentials too!

/**
 * Define database parameters here
 */
define("DB_USER", 'your_username');
define("DB_PASSWORD", 'your_password');
define("DB_NAME", 'your_db_name');
define("DB_HOST", 'localhost');
define("BACKUP_DIR", 'myphp-backup-files'); // Comment this line to use same script's directory ('.')
define("BACKUP_FILE", 'your-backup-file.sql.gz'); // Script will autodetect if backup file is gzipped or not based on .gz extension
define("CHARSET", 'utf8');
define("DISABLE_FOREIGN_KEY_CHECKS", true); // Set to true if you are having foreign key constraint fails


Project at GitHub: https://github.com/daniloaz/myphp-backup

(c) Daniel López Azaña, 2012-2017 (http://www.daniloaz.com)

Backup and restore mysql database using php script
Back up And Restore With PHP

Today I’m going to share with you a very useful script that can create backup of your mysql database and restore it any time you wanted, just as one click recovery of mysql using a php script .

I need to make it because one of my client need this feature in his admin panel So here are the steps to use it.

it take just 5 second for backup and 5 second to restore

Here are some steps you need to take

1. Create an object of class backup_restore

[php]$obj = new backup_restore(‘hostname’,’db_name’,’user’,’paswd’,’*’);[/php]

2. If you want to create backup of your mysql database ,call backup function

[php]$message = $obj–>backup();[/php]

3. If you want to restore your database,call restore function.

[php]$message = $obj–>restore();[/php]

4. That’s it rest of thing ,code will tell you.

Don’t forget to change argument

‘hostname‘ change to ‘your hostname‘.

‘db_name’ change to your database name.

‘username’ change to’ ‘ MySQL username‘.

‘password‘ change to ‘MySQL password‘.

‘path’ is an option argument if you want to store sql dump in a secure location otherwise sql dump will be generated in current directory [NEW ADDED]

‘*’ Change to table name whose backup you want. [ARGUMENT OMITTED]

New Added Feature

  1. You can store the sql dump at you secret location in the server.
  2. Added new formatting function

    private function remove_accents
    private function sql_backquote
    private function sql_addslashes

  3. generate pure sql dump. you can download dump file and import it by phpmyadmin.

Instruction and feature:

1. The file this script generate is pure sql file .so you can import it by phpmyadmin.

2. Can handle large db back and restore.

3. Support cms backup. wordpress, joomla, drupal and all others.

4. It does not use mysqldump command ,As many server do not enable shell_exec.

5. It gives 100% sql file.

6. 5-sec for backup and 5-sec for restoration

7.Open Source : Download –> Use –> Improve –> Share . No Warranty.:)

Download script below

zip contain class and example file

Download it free from github

Download

Thank you