DBBackup it's a library that provides classes to execute mysqldump to backup the given database, relocate the backup file in the local filesystem and compress the resulting backup file.
- PHP 7.1+
- MySQL client/server version 5.6.x
- Gzip on the server (only tested on unix)
You can install the package via composer:
composer require lily-labs/db-backupFor the backup process to work, it requires that you pre-configure a local login for mysql on your server using mysql_config_editor as follows:
mysql_config_editor set --login-path=local --host=localhost --user=username --passwordYour database password will be asked after entering the command.
You can se more information here
First create a BackupFileNameGenerator, this will generate the desire filename for the backup file, the default implementation LilyLabs\DBBackup\BasicBackupFileNameGenerator will generate a file name in this format: "[database_name]_[year]-[month]-[day].sql"
use LilyLabs\DBBackup\BasicBackupFileNameGenerator;
use LilyLabs\DBBackup\MysqldumpBackupProcessor;
$db_name = "DBTest";
$date = new DateTime('now');
$filename_generator = new BasicBackupFileNameGenerator($db_name, $date);
$filename = $filename_generator->getName(); // DBTest_[year]-[month]-[day].sqlNow, create an instance of LilyLabs\DBBackup\MysqldumpBackupProcessor to execute the backup process
$backup_processor = new MysqldumpBackupProcessor(
$db_name,
$filename_generator
);
$backup_file = $backup_processor->execute();The returned object is an instance of LilyLabs\DBBackup\DBBackupFile, all backups are generated on the temp folder obtained by sys_get_temp_dir(), you can now move the file to your desired storage location.
$moved_backup_file = $backup_file->move("/final/location/of/the/backup");Finally, you can use the class LilyLabs\DBBackup\GzipCompressor to gzip the backupfile:
$compressor = new \LilyLabs\DBBackup\GzipCompressor;
$compressed_backup_file = $compressor->compress($moved_backup_file);composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email mindacj@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.