Installation

Existing Project just simply run composer require codexshaper/database-backup-restore
New project. Create a composer.json in your root directory then add below code

{
    "require": {
        "codexshaper/database-backup-restore": "^1.1"
    }
}
                                 
composer install

Mysql

Dump

Mustbe installed mysqldump in your system

<?php 

$options    = [
    'host'            => 'HOST',
    'port'            => 'PORT',
    'dbName'          => 'DATABASE_NAME',
    'username'        => 'DATABASE_USERNAME',
    'password'        => 'DATABASE_PASSWORD',
    'destinationPath' => 'STORAGE_PATH_WITH_FILE_NAME', // /path/to/backups/mysql/dump
];
                                   
Use constructor with options
 
$dumper = new \CodexShaper\Dumper\Drivers\MysqlDumper($options);
$dumper->dump();
                                    
Use create method

\CodexShaper\Dumper\Drivers\MysqlDumper::create($options)->dump();
                                      
                                   
Dynamically
 
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setHost('localhost')
  ->setPort('3306')
  ->setDbName('dbName')
  ->setUserName('userName')
  ->setPassword('password')
  ->setDestinationPath('destination_path')
  ->dump();
                                      
                                   
Archive

\CodexShaper\Dumper\Drivers\MysqlDumper::create($options)
  ->useCompress("gzip") // This command apply gzip to zip
  ->dump();

                                      
                                   

Restore

Mustbe installed mysql in your system
Restore from without archive
 
$dumper = new \CodexShaper\Dumper\Drivers\MysqlDumper($options);
$dumper->setRestorePath($restorePath); // /path/to/backups/mysql/dump.sql 
$dumper->restore();
                                    
Restore from archive
 
\CodexShaper\Dumper\Drivers\MysqlDumper::create($options)
  ->useCompress("gunzip") // this command unzip the file
  ->setRestorePath($restorePath) // /path/to/backups/mysql/dump.sql.gz
  ->restore();

                                      
                                   

Pgsql

Dump

Mustbe installed pg_dump in your system

<?php 

$options    = [
    'host'            => 'HOST',
    'port'            => 'PORT',
    'dbName'          => 'DATABASE_NAME',
    'username'        => 'DATABASE_USERNAME',
    'password'        => 'DATABASE_PASSWORD',
    'destinationPath' => 'STORAGE_PATH_WITH_FILE_NAME', // /path/to/backups/pgsql/dump
];


                                   
Use constructor with options
 
$dumper = new \CodexShaper\Dumper\Drivers\PgsqlDumper($options);
$dumper->dump();


                                    
Use create method
 
\CodexShaper\Dumper\Drivers\PgsqlDumper::create($options)->dump();
                                      
                                   
Dynamically
 
\CodexShaper\Dumper\Drivers\PgsqlDumper::create()
  ->setHost('localhost')
  ->setPort('3306')
  ->setDbName('dbName')
  ->setUserName('userName')
  ->setPassword('password')
  ->setDestinationPath('destination_path')
  ->dump();

                                      
                                   
Archive
 
\CodexShaper\Dumper\Drivers\PgsqlDumper::create($options)
  ->useCompress("gzip") // This command apply gzip to zip
  ->dump();

                                      
                                   

Restore

Mustbe installed psql in your system
Restore from without archive
 
$dumper = new \CodexShaper\Dumper\Drivers\PgsqlDumper($options);
$dumper->setRestorePath($restorePath); // /path/to/backups/pgsql/dump.sql 
$dumper->restore();
                                    
Restore from archive
 
\CodexShaper\Dumper\Drivers\PgsqlDumper::create($options)
  ->useCompress("gunzip") // this command unzip the file
  ->setRestorePath($restorePath) // /path/to/backups/pgsql/dump.sql.gz
  ->restore();

                                      
                                   

Sqlite

Dump

Mustbe installed sqlite3 in your system

$options    = [
    'dbName'          => 'DATABASE_PATH', // /path/to/database.sqlite
    'destinationPath' => 'STORAGE_PATH_WITH_FILE_NAME', // /path/to/backups/sqlite/dump.sql
];
                                   
Use constructor with options
 
$dumper = new \CodexShaper\Dumper\Drivers\SqliteDumper($options);
$dumper->dump();

                                    
Use create method
 
\CodexShaper\Dumper\Drivers\SqliteDumper::create($options)->dump();
                                      
                                   
Dynamically
 
\CodexShaper\Dumper\Drivers\SqliteDumper::create()
  ->setDbName($database)
  ->setDestinationPath($destinationPath)
  ->dump();

                                      
                                   
Archive

\CodexShaper\Dumper\Drivers\SqliteDumper::create()
  ->setDbName($database)
  ->setDestinationPath($destinationPath)
  ->useCompress("gzip") // This command apply gzip to zip
  ->dump();

                                      
                                   

Restore

Mustbe installed sqlite3 in your system
 
$options    = [
    'dbName'          => 'DATABASE_PATH', // /path/to/database.sqlite
    'restorePath' => 'RESTORE_PATH_WITH_FILE_NAME', // /path/to/backups/sqlite/dump.sql
];
                                    
Use Construct
 
$dumper = new \CodexShaper\Dumper\Drivers\SqliteDumper($options);
$dumper->restore();
                                    
Use Dynamic
 
$dumper = new \CodexShaper\Dumper\Drivers\SqliteDumper($options);
$dumper->restore();
                                    
Restore from archive

\CodexShaper\Dumper\Drivers\SqliteDumper::create()
  ->setDbName($database)
  ->setRestorePath($restorePath)
  ->useCompress("gunzip") // This command apply gzip to zip
  ->restore();

                                      
                                   

Mongodb

Dump

Mustbe installed mongodump in your system

$options    = [
    'host'            => 'HOST',
    'port'            => 'PORT',
    'dbName'          => 'DATABASE_NAME',
    'username'        => 'DATABASE_USERNAME',
    'password'        => 'DATABASE_PASSWORD',
    'destinationPath' => 'STORAGE_PATH_WITH_FILE_NAME', // /path/to/backups/mongodb/dump
];
                                   
Use constructor with options
  
$dumper = new \CodexShaper\Dumper\Drivers\MongoDumper($options);
$dumper->dump();
                                    
Use create method
  
\CodexShaper\Dumper\Drivers\MongoDumper::create($options)->dump();
                                      
                                   
Dynamically
 
\CodexShaper\Dumper\Drivers\MongoDumper::create()
  ->setHost($host)
  ->setPort($port)
  ->setDbName($database)
  ->setUserName($username)
  ->setPassword($password)
  ->setDestinationPath($destinationPath)
  ->dump();
                                      
                                   
Archive
 
\CodexShaper\Dumper\Drivers\MongoDumper::create($options)
  ->useCompress("gzip") // This command will add --archive with --gzip
  ->dump();
                                      
                                   

Use URI

 
$options = [
    'uri'             => $uri,
    "destinationPath" => $destinationPath, // /path/to/backups/mongodb/dump 
];
$dumper = new \CodexShaper\Dumper\Drivers\MongoDumper($options);
$dumper->dump();
                                      
                                   
OR

\CodexShaper\Dumper\Drivers\MongoDumper::create(['uri' => $uri])
  ->setDestinationPath($destinationPath)
  ->dump()
                                      
                                   
Dynamic

\CodexShaper\Dumper\Drivers\MongoDumper::create()
  ->setUri($uri)
  ->setDestinationPath($destinationPath)
  ->dump()
                                      
                                   
Compress

\CodexShaper\Dumper\Drivers\MongoDumper::create(['uri' => $uri])
  ->useCompress("gzip")
  ->setDestinationPath($destinationPath)
  ->dump();
                                      
                                   

Restore

Mustbe installed mongorestore in your system
Restore from without archive
  
$dumper = new \CodexShaper\Dumper\Drivers\MongoDumper($options);
$dumper->setRestorePath($restorePath); // /path/to/backups/mongodb/dump 
$dumper->restore();
                                    
Restore from archive
 
\CodexShaper\Dumper\Drivers\MongoDumper::create($options)
  ->useCompress("gzip")
  ->setRestorePath($restorePath) // /path/to/backups/mongodb/dump.gz
  ->restore();
                                      
                                   
Use URI
  
\CodexShaper\Dumper\Drivers\MongoDumper::create(['uri' => $uri])
    ->setRestorePath($restorePath)
    ->restore();
                                      
                                   
Restore from archive using URI
 
\CodexShaper\Dumper\Drivers\MongoDumper::create(['uri' => $uri])
  ->useCompress("gzip")
  ->setRestorePath($restorePath)
  ->restore();
                                      
                                   
Set Dump Binary Path

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create($options)
  ->setCommandBinaryPath($binaryPath) // /path/to/mysql/bin
  ->dump();
                                    
                                

Mysql Methods

useSingleTransaction : only for mysql

useSingleTransaction - use single transaction

Description
useSingleTransaction () : $this

This method enable the single transaction and return the current object instance

Parameters

This method doesn't support any parameter

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->useSingleTransaction();
                                            
useSkipLockTables : only for mysql

useSkipLockTables - skip lock tables

Description
useSkipLockTables () : $this

This method enable the skip lock tables and return the current object instance

Parameters

This method doesn't support any parameter

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->useSkipLockTables();
                                            
useQuick : only for mysql

useQuick - use quick

Description
useQuick () : $this

This method enable the quick transaction and return the current object instance

Parameters

This method doesn't support any parameter

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->useQuick();
                                            
doNotUseSkipComments : only for mysql

doNotUseSkipComments - don not skip comments

Description
doNotUseSkipComments () : $this

This method disable the skip comments and return the current object instance

Parameters

This method doesn't support any parameter

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->doNotUseSkipComments();
                                            
doNotCreateTables : only for mysql and pgsql

doNotCreateTables - don not create tables

Description
doNotCreateTables () : $this

This method disable the create table and return the current object instance

Parameters

This method doesn't support any parameter

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->doNotCreateTables();
                                            
setDefaultCharacterSet : only for mysql

setDefaultCharacterSet - Set default charecter set

Description
setDefaultCharacterSet ( string $charecterSet) : $this

This method set the charecterSet and return the current object instance

Parameters

charecterSet

Accept charecterSet as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setDefaultCharacterSet("utf8");
                                            
getDumpCommand : only for mysql

getDumpCommand - get dump command for mysql

Description
getDumpCommand ( string $credentialFile, $destinationPath) : string

This method return the dump command for mysql

Parameters

credentialFile

Optional. Accept credentialFile as string. The default value is empty string

destinationPath

Optional. Accept destinationPath as string. The default value is empty string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getDumpCommand('credentials.txt', '/path/to/destination/dump.sql');
                                            
getRestoreCommand : only for mysql

getRestoreCommand - get restore command for mysql

Description
getRestoreCommand ( string $credentialFile, $filePath) : string

This method return the restore command for mysql

Parameters

credentialFile

Optional. Accept credentialFile as string. The default value is empty string

filePath

Optional. Accept filePath as string. The default value is empty string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getRestoreCommand('credentials.txt', '/path/to/rstore/dump.sql');
                                            

Pgsql methods

useInserts : only for pgsql

useInserts - use inserts

Description
useInserts () : $this

This method enble the inserts and return the current object instance

Parameters

This method doesn't support any parameter

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\PgsqlDumper::create()
  ->useInserts();
                                            

MongoDB methods

setUri : only for MongoDB

setUri - set uri

Description
setUri ( string $uri) : $this

This method set the database uri and return the current object instance

Parameters

uri

Uri as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MongoDumper::create()
  ->setUri("uri");
                                            
setCollection : only for MongoDB

setCollection - set collection

Description
setCollection ( string $collection) : $this

This method set the database collection and return the current object instance

Parameters

collection

Collection as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MongoDumper::create()
  ->setCollection("test1");
                                            
setAuthenticationDatabase : only for MongoDB

setAuthenticationDatabase - set authentication database

Description
setAuthenticationDatabase ( string $authenticationDatabase) : $this

This method set the authentication database name and return the current object instance

Parameters

authenticationDatabase

Authentication database name as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MongoDumper::create()
  ->setAuthenticationDatabase("admin");
                                            

Common Methods

setDbName

setDbName - Set database name

Description
setDbName ( string $name) : $this

This method set the database name and return the current object instance

Parameters

name

Database as a string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setDbName("dbname");
                                            
setUserName

setUserName - Set database username

Description
setUserName ( string $username) : $this

This method set the database username and return the current object instance

Parameters

username

Username for the selected database

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setUserName("root");
                                            
setPassword

setPassword - Set database password

Description
setPassword ( string $password) : $this

This method set the database password and return the current object instance

Parameters

password

Password for the selected database

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setPassword("password");
                                            
setHost

setHost - Set database host

Description
setHost ( string $host) : $this

This method set the database host and return the current object instance

Parameters

host

Host for the selected database

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setHost("localhost");
                                            
setPort

setPort - Set database port

Description
setPort ( string $port) : $this

This method set the database port and return the current object instance

Parameters

port

Port for the selected database

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setPort("3306");
                                            
setSocket

setSocket - Set database socket

Description
setSocket ( string $socket) : $this

This method set the database socket and return the current object instance

Parameters

socket

Socket for the selected database

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setSocket("socket");
                                            
setTimeOut

setTimeOut - Set timeout

Description
setTimeOut ( string $timeout) : $this

This method set the database timeout and return the current object instance

Parameters

timeout

Timeout in milliseconds

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setTimeOut("milliseconds");
                                            
setTables

setTables - Set include tables

Description
setTables ( string|array $tables) : $this

This method include tables to download only provided tables and return the current object instance

Parameters

tables

Accept tables string or array

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setTables(['test1', 'test2', 'test3']);
                                            
setIgnoreTables

setIgnoreTables - Set exclude tables

Description
setIgnoreTables ( string|array $tables) : $this

This method ignore provided tables to download and return the current object instance

Parameters

tables

Accept tables string or array

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setIgnoreTables(['test1', 'test2', 'test3']);
                                            
setCommandBinaryPath

setCommandBinaryPath - Set binary path to dump or restore

Description
setCommandBinaryPath ( string $path) : $this

This method sets the binary path for dump or restore command and return the current object instance

Parameters

path

Accept path as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setCommandBinaryPath('/path/to/mysql/bin');
                                            
setDestinationPath

setDestinationPath - Set the destination path

Description
setDestinationPath ( string $path) : $this

This method set the destination path for dumper to store and return the current object instance

Parameters

path

Accept path as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setDestinationPath('/path/to/mysql/dump.sql');
                                            
setRestorePath

setRestorePath - Set the retore path

Description
setRestorePath ( string $path) : $this

This method set the retore path for dumper to restore and return the current object instance

Parameters

path

Accept path as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setRestorePath('/path/to/mysql/dump.sql');
                                            
setCompressBinaryPath

setCompressBinaryPath - Set the compressor command binary path

Description
setCompressBinaryPath ( string $path) : $this

This method set the compressor command binary path and return the current object instance

Parameters

path

Accept path as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setCompressBinaryPath('/path/to/bin');
                                            
setCompressCommand

setCompressCommand - Set the compress command

Description
setCompressCommand ( string $command) : $this

This method set the compress command and return the current object instance

Parameters

command

Accept command as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setCompressCommand('gzip');
                                            
setCompressExtension

setCompressExtension - Set the compress extension

Description
setCompressExtension ( string $extension) : $this

This method set the compress extension and return the current object instance

Parameters

extension

Accept extension as string

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->setCompressExtension('.gz');
                                            
useCompress

useCompress - Set the compress command, extension, binary path

Description
useCompress ( string $command, string $extension, string $binary_path) : $this

This method set the compress command, extension, and binary path in single call and return the current object instance

Parameters

command

Optional. Accept command as string. The default value is gzip

extension

Optional. Accept extension as string. The default value is .gz

binary_path

Optional. Accept binary_path as string. The default value is empty string.

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->useCompress('gzip', '.gz', '/path/to/bin');
                                            
enableDebug

enableDebug - Set the enable debug

Description
enableDebug () : $this

This method set the compress extension and return the current object instance

Parameters

This method doesn't support any parameter

Return

This method return the current object

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->enableDebug();
                                            
dump

dump - get dump command

Description
dump ( string $destinationPath) : void

This method dump sql data and return nothing

Parameters

destinationPath

Optional. Accept destinationPath as string. Default is empty string

Return

This method return nothing

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\SqliteDumper::create()
  ->dump('/path/to/destination/dump.sql');
                                            
restore

restore - get restore command

Description
restore ( string $restorePath) : void

This method restore sql data and return nothing

Parameters

restorePath

Optional. Accept restorePath as string. Default is empty string

Return

This method return nothing

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\SqliteDumper::create()
  ->restore('/path/to/restore/dump.sql');
                                            
getDbName

getDbName - get database name

Description
getDbName () : string

This method return the database name

Parameters

This method doesn't support any parameter

Return

This method return the database name

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getDbName();
                                            
getUserName

getUserName - get database username

Description
getUserName () : string

This method return the database username

Parameters

This method doesn't support any parameter

Return

This method return the database username

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getUserName();
                                            
getPassword

getPassword - get database password

Description
getPassword () : string

This method return the database password

Parameters

This method doesn't support any parameter

Return

This method return the database password

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getPassword();
                                            
getHost

getHost - get host name

Description
getHost () : string

This method return the host name

Parameters

This method doesn't support any parameter

Return

This method return the host name

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getHost();
                                            
getPort

getPort - get port

Description
getPort () : string

This method return the port

Parameters

This method doesn't support any parameter

Return

This method return the port

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getPort();
                                            
getSocket

getSocket - get socket

Description
getSocket () : string

This method return the socket

Parameters

This method doesn't support any parameter

Return

This method return the socket

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getSocket();
                                            
getTimeOut

getTimeOut - get timeout

Description
getTimeOut () : string

This method return the timeout

Parameters

This method doesn't support any parameter

Return

This method return the timeout

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getTimeOut();
                                            
getCommandBinaryPath

getCommandBinaryPath - get command binary path

Description
getCommandBinaryPath () : string

This method return the command binary path

Parameters

This method doesn't support any parameter

Return

This method return the command binary path

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getCommandBinaryPath();
                                            
getDestinationPath

getDestinationPath - get destination path

Description
getDestinationPath () : string

This method return the destination path

Parameters

This method doesn't support any parameter

Return

This method return the destination path

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getDestinationPath();
                                            
getRestorePath

getRestorePath - get restore path

Description
getRestorePath () : string

This method return the restore path

Parameters

This method doesn't support any parameter

Return

This method return the restore path

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\MysqlDumper::create()
  ->getRestorePath();
                                            
getDumpCommand

getDumpCommand - get dump command

Description
getDumpCommand ( string $destinationPath) : string

This method return the dump command

Parameters

destinationPath

Optional. Accept destinationPath as string. Default is empty string

Return

This method return the dump command

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\SqliteDumper::create()
  ->getDumpCommand('/path/to/destination/dump.sql');
                                            
getRestoreCommand

getRestoreCommand - get dump command

Description
getRestoreCommand ( string $filePath) : string

This method return the dump command

Parameters

filePath

Optional. Accept filePath as string. Default is empty string

Return

This method return the dump command

Examples

// Same for other driver
\CodexShaper\Dumper\Drivers\SqliteDumper::create()
  ->getRestoreCommand('/path/to/restore/dump.sql');