Giter VIP home page Giter VIP logo

mysql-backup's Introduction

MySQLBackup

Introduction

This is a backup utility used to dump a database for backup. The backup file contains the queries to build the tables and insert the datas.
You can select some or all tables, compress the backup in ZIP or GZIP and start downloading automatically.

Installation

With Composer, add this line to your require section :

"phelium/mysql-backup": "dev-master"

Then run composer update.

Initialization

require 'vendor/autoload.php';

use Phelium\Component\MySQLBackup;

To initialize MySQLBackup, you must provide your database information :

$Dump = new MySQLBackup('server name', 'user name', 'password', 'db name');

Usage

Add tables

By default, all tables are saved. If you want to save one or more tables, the functions addTable or addTables must be used:

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->addTable('posts'); // Add the posts table
$Dump->addTables(array('users', 'comments')); // Add the users and comments tables

Set the filename

By default, the name of the generated file is "dump_{database name}_{date}". With setFilename method, you can specify a different file name:

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->setFilename('bkp_'.time());

Do not dump the table schema or the datas

By default, the table schema (structure) and datas of each table are saved. However, it's possible not to save one or the other with setDumpStructure and setDumpDatas (default : true) :

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->setDumpStructure(false); // Not the structure
$Dump->setDumpDatas(false); // Not the datas

Do not add DROP TABLE

By default, the DROP TABLE IF EXISTS directive is added before each CREATE TABLE. You can disable it with addDropTable (default : true) :

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->addDropTable(false); // Not add the DROP TABLE IF EXISTS statment

Do not add IF NOT EXISTS

By default, the IF NOT EXISTS directive is added during a CREATE TABLE. You can disable it with addIfNotExists (default : true) :

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->addIfNotExists(false); // Not add the IF NOT EXISTS statment

Compress file

To compress the output file, setCompress allows to use ZIP or GZIP (default : no compression) :

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->setCompress('zip'); // zip | gz | gzip

Delete file

The generated file can be deleted automatically with setDelete (default : false) :

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->setDelete(true);

Auto download

To download automatically the generated file, use setDownload (default : false) :

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->setDownload(true); // starts downloading

Execute the backup

To start backing up your database, use dump:

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->dump();

Full example

Here is an example to backup the blog database. Only the tables posts, comments and users are kept. Then compressed in ZIP format. The generated file is deleted from the server and the download starts automatically:

<?php
require 'vendor/autoload.php';
    
use Phelium\Component\MySQLBackup;

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump->addTables(array('posts', 'comments', 'users'));
$Dump->setCompress('zip');
$Dump->setDelete(true);
$Dump->setDownload(true);
$Dump->dump();

// or

$Dump = new MySQLBackup('localhost', 'root', '', 'blog');
$Dump
    ->addTables(array('posts', 'comment', 'users'));
    ->setCompress('zip');
    ->setDelete(true);
    ->setDownload(true);
    ->dump();

mysql-backup's People

Contributors

f1-djerba avatar phlyper avatar shevabam avatar

Watchers

 avatar  avatar  avatar

Forkers

f1-djerba

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.