PHP Classes

Simple PHP Backup Database MySQL: Backup, download and restore a MySQL database

Recommend this page to a friend!
  Info   View files Documentation   View files View files (19)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 295 This week: 1All time: 7,449 This week: 560Up
Version License PHP version Categories
simple-backup 1.0.1Custom (specified...5PHP 5, Databases, System information
Description 

Author

This package can backup, download and restore a MySQL database.

It can connect to a given MySQL database server and backup all tables or just a few tables to a file with MySQL statements that can be downloaded by the current user accessing the page that executes this class.

The package can also take previously downloaded backup file and import it to a given database by executing the SQL statements that it contains.

Picture of Josiah Ovye Yahaya
  Performance   Level  
Name: Josiah Ovye Yahaya <contact>
Classes: 6 packages by
Country: Nigeria Nigeria
Age: 33
All time rank: 232213 in Nigeria Nigeria
Week rank: 416 Up8 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 1x

Recommendations

PHP MySQL Database Synchronize
PHP MySQL Database Synchronize1

Documentation

Simple-backup

<img src="/_docs/store_export.PNG"/><br/> A simple mysql database backup library for php. This library helps you backup your mysql database to a directory in your project or download it to your machine.

The library also allows you import or restore a database without stress. Follow the instructions below to get started.

Installation

Open your terminal or command prompt and type the below command:

composer require coderatio/simple-backup

New in version 1.0.1

  1. You can now chain static `::start()` on `SimpleBackup class.
  2. This version includes fluent chaining of database settings.
  3. Added `->includeOnly(array $tables)` which allows you to export data for only tables mentioned in `$tables` variable.
  4. Added `->excludeOnly(array $tables)` which removes the inclussion of tables mentioned in `$tables` variable during export.

(v1.0.1) Exporting specific tables only

  SimpleBackup::start()
    ->setDbName('db_name')
    ->setDbUser('db_username')
    ->setDbPassword('db_password')
    ->includeOnly(['carts', 'houses', 'categories'])
    ->then()->storeAfterExportTo('backups')
    ->then()->getResponse();

(v1.0.1) Excluding specific tables only

  SimpleBackup::start()
    ->setDbName('db_name')
    ->setDbUser('db_username')
    ->setDbPassword('db_password')
    ->excludeOnly(['users', 'posts'])
    ->then()->storeAfterExportTo('backups')
    ->then()->getResponse();

Exporting

The export can be done in two ways. 1. Store in a directory 2. Download to your machine

1-- Store in a directory

To store the export in a directory, do this:

require 'vendor/autoload.php';

use Coderatio\SimpleBackup\SimpleBackup;

// Set the database to backup
$simpleBackup = SimpleBackup::setDatabase(['db_name', 'db_user', 'db_password', 'db_host'])
  ->storeAfterExportTo('pathtostore', 'file_name (optional)');

To get the stored file name, you can echo it out like this:

echo $simpleBackup->getExportedName();

You can also get the reponse by doing this:

/
* @return object
/
var_dump($simpleBackup->getResponse());

2-- Download

To download the export to your machine, do this:

require 'vendor/autoload.php';

use Coderatio\SimpleBackup\SimpleBackup;

// Set the database to backup
$simpleBackup = SimpleBackup::setDatabase(['db_name', 'db_user', 'db_password', 'db_host'])
  ->downloadAfterExport($file_name (optional));

If $file_name isn't provided, a random name will be generated for the download.

Adding where clauses to tables

To add where clauses as you would do on SQL, you can do this before exporting:

<b>Note:</b> $tables variable must be an associative array e.g

$tables = [
  'users' => 'is_active = true'
];

$simpleBackup->setTableConditions(array $tables);

Setting rows limit on tables

To limit how many rows to be included in your backup for a table, do this before exporting:

<b>Note:</b> Just like adding where clauses, the $tables variable here must be an associative array. e.g

$tables = [
  'users' => 50,
  'posts' => 50
]
$simpleBackup->setTableLimitsOn(array $tables);

Importing

This package makes importing or restoring your mysql database easy. To import your database, do this:

require 'vendor/autoload.php';

use Coderatio\SimpleBackup\SimpleBackup;

// Set the database to backup
$simpleBackup = SimpleBackup::setDatabase(['db_name', 'db_user', 'db_password', 'db_host (optional)']])
    ->importFrom('pathtosql_file or sql_contents');

/
* You can then dump the response like this. 
*
* @return object
/
var_dump($simpleBackup->getResponse());

<b>Note:</b> You can provide sql statements as the parameter. You may also overwrite the database configuration by passing it as second parameter to the importFrom(). e.g importFrom(pathtosql_file, array $db_config);.

Todo

  1. Add a scheduler method to use with cron
  2. Store backup in Dropbox, Google Drive.
  3. Send backup to emails

Contribution

To contribute to this project, send a pull request or find me on <a href="https://twitter.com/josiahoyahaya" target="_blank">Twitter</a>.

License

This project is licenced with the MIT license.


  Files folder image Files  
File Role Description
Files folder image.idea (7 files, 1 directory)
Files folder imagesrc (1 file, 2 directories)
Files folder image_docs (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file demo.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .idea  
File Role Description
Files folder imageinspectionProfiles (1 file)
  Accessible without login Plain text file misc.xml Data Auxiliary data
  Accessible without login Plain text file modules.xml Data Auxiliary data
  Accessible without login Plain text file php.xml Data Auxiliary data
  Accessible without login Plain text file simple-backup.iml Data Auxiliary data
  Accessible without login Plain text file symfony2.xml Data Auxiliary data
  Accessible without login Plain text file vcs.xml Data Auxiliary data
  Accessible without login Plain text file workspace.xml Data Auxiliary data

  Files folder image Files  /  .idea  /  inspectionProfiles  
File Role Description
  Accessible without login Plain text file Project_Default.xml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageExceptions (1 file)
Files folder imageFoundation (4 files)
  Plain text file SimpleBackup.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file NoTablesFoundException.php Class Class source

  Files folder image Files  /  src  /  Foundation  
File Role Description
  Plain text file Configurator.php Class Class source
  Plain text file Database.php Class Class source
  Plain text file Mysqldump.php Class Class source
  Plain text file Provider.php Class Class source

  Files folder image Files  /  _docs  
File Role Description
  Accessible without login Image file store_export.PNG Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:295
This week:1
All time:7,449
This week:560Up