Skip to content

Commit

Permalink
add support for extended insert + adhere to psr-2
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Apr 30, 2015
1 parent 0bb9852 commit 38633b8
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 51 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,20 @@ return [
'maxAgeInDays' => 90,
],

/*
* The path to the mysqldump binary. You can leave this empty
* if the binary is installed in the default location.
*/
'mysql' => [
/*
* The path to the mysqldump binary. You can leave this empty
* if the binary is installed in the default location.
*/
'dump_command_path' => '',

/*
* If your server supports it you can turn on extended insert.
* This will result in a smaller dump file and speeds up the backup process.
*
* See: https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_extended-insert
*/
'useExtendedInsert' => false,
],
];
```
Expand Down
11 changes: 5 additions & 6 deletions src/BackupHandlers/Database/DatabaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function __construct()

public function getDatabase(array $realConfig)
{

try {
$this->buildMySQL($realConfig);
} catch (Exception $e) {
Expand Down Expand Up @@ -45,21 +44,21 @@ protected function buildMySQL(array $config)
}

/**
* Determine the host from the given config
* Determine the host from the given config.
*
* @param array $config
*
* @return string
*
* @throws Exception
*/
public function determineHost(array $config)
{
if (isset($config['host']))
{
if (isset($config['host'])) {
return $config['host'];
}

if (isset($config['read']['host']))
{
if (isset($config['read']['host'])) {
return $config['read']['host'];
}

Expand Down
19 changes: 14 additions & 5 deletions src/BackupHandlers/Database/Databases/MySQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function dump($destinationFile)
);
$temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];

$command = sprintf('%smysqldump --defaults-extra-file=%s --skip-comments --skip-extended-insert %s > %s %s',
$command = sprintf('%smysqldump --defaults-extra-file=%s --skip-comments '.($this->useExtendedInsert() ? '--extended-insert' : '--skip-extended-insert').' %s > %s %s',
$this->getDumpCommandPath(),
escapeshellarg($temporaryCredentialsFile),
escapeshellarg($this->database),
Expand Down Expand Up @@ -86,15 +86,24 @@ protected function getDumpCommandPath()
}

/**
* Set the socket if one is specified in the configuration
* Determine if the dump should use extended-insert.
*
* @return string
*/
protected function useExtendedInsert()
{
return config('laravel-backup.mysql.useExtendedInsert');
}

/**
* Set the socket if one is specified in the configuration.
*
* @return string
*/
protected function getSocketArgument()
{
if($this->socket != '')
{
return '--socket=' . $this->socket;
if ($this->socket != '') {
return '--socket='.$this->socket;
}

return '';
Expand Down
2 changes: 2 additions & 0 deletions src/BackupHandlers/Files/FilesBackupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function getFilesToBeBackedUp()
* Make a unique array of all filepaths from a given array of files.
*
* @param array $fileArray
*
* @return array
*/
public function getAllPathFromFileArray($fileArray)
Expand All @@ -85,6 +86,7 @@ public function getAllPathFromFileArray($fileArray)
* Recursively get all the files within a given directory.
*
* @param $directory
*
* @return array
*/
protected function getAllFilesFromDirectory($directory)
Expand Down
18 changes: 9 additions & 9 deletions src/Commands/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ protected function getAllFilesToBeBackedUp()
$this->comment('Database dumped');
}

if($this->option('only-db'))
{
if ($this->option('only-db')) {
return $files;
}

Expand Down Expand Up @@ -172,21 +171,22 @@ protected function writeIgnoreFile($disk, $dumpDirectory)
protected function getBackupDestinationFileName()
{
$backupDirectory = config('laravel-backup.destination.path');
$backupFilename = $this->getPrefix() . date('YmdHis') . $this->getSuffix() . '.zip';
return $backupDirectory . '/' . $backupFilename;
$backupFilename = $this->getPrefix().date('YmdHis').$this->getSuffix().'.zip';

return $backupDirectory.'/'.$backupFilename;
}

/**
* Get the prefix to be used in the filename of the backup file.
*
*
* @return string
*/
public function getPrefix()
{
if ($this->option('prefix') != '') {
return $this->option('prefix');
}

return config('laravel-backup.destination.prefix');
}

Expand All @@ -205,20 +205,20 @@ public function getSuffix()
}

/**
* Copy the given file to given filesystem
* Copy the given file to given filesystem.
*
* @param string $file
* @param $fileSystem
*/
public function copyFileToFileSystem($file, $fileSystem)
{
$this->comment('Start uploading backup to ' . $fileSystem . '-filesystem...');
$this->comment('Start uploading backup to '.$fileSystem.'-filesystem...');

$disk = Storage::disk($fileSystem);

$this->copyFile($file, $disk, $this->getBackupDestinationFileName(), $fileSystem == 'local');

$this->comment('Backup stored on ' . $fileSystem . '-filesystem in file "' . $this->getBackupDestinationFileName() . '"');
$this->comment('Backup stored on '.$fileSystem.'-filesystem in file "'.$this->getBackupDestinationFileName().'"');
}

/**
Expand Down
20 changes: 7 additions & 13 deletions src/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Carbon\Carbon;
use Spatie\Backup\FileHelpers\FileSelector;

class CleanCommand extends Command {

class CleanCommand extends Command
{
/**
* The console command name.
*
Expand All @@ -30,26 +30,22 @@ public function fire()

$this->info('Start cleaning up back-up files that are older than '.config('laravel-backup.clean.maxAgeInDays').' days...');

foreach($this->getTargetFileSystems() as $filesystem)
{
foreach ($this->getTargetFileSystems() as $filesystem) {
$disk = Storage::disk($filesystem);
$path = config('laravel-backup.destination.path');

$filesToBeDeleted = (new FileSelector($disk, $path))->getFilesOlderThan($expireDate, ['zip']);

$filesDeleted = 0;
foreach($filesToBeDeleted as $file)
{
foreach ($filesToBeDeleted as $file) {
$disk->delete($file);
$filesDeleted++;
}
$this->comment('deleted '.$filesDeleted.' old backup(s) on the ' . $filesystem . '-filesystem.');
$this->comment('deleted '.$filesDeleted.' old backup(s) on the '.$filesystem.'-filesystem.');
$this->comment($filesystem.'-filesystem cleaned up.');
}

$this->info('All done!');


}

/**
Expand All @@ -72,13 +68,11 @@ private function guardAgainstInvalidConfiguration()
{
$maxAgeInDays = config('laravel-backup.clean.maxAgeInDays');

if (! is_numeric($maxAgeInDays))
{
if (! is_numeric($maxAgeInDays)) {
throw new Exception('maxAgeInDays should be numeric');
}

if ($maxAgeInDays <= 0)
{
if ($maxAgeInDays <= 0) {
throw new Exception('maxAgeInDays should be higher than 0');
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/FileHelpers/FileSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use DateTime;

class FileSelector {

class FileSelector
{
protected $disk;
protected $path;

Expand All @@ -15,48 +15,50 @@ public function __construct($disk, $path)

/**
* Get all files older than $date
* Only files with an extension present in $onlyIncludeFilesWithExtension will be returned
* Only files with an extension present in $onlyIncludeFilesWithExtension will be returned.
*
* @param DateTime $date
* @param array $onlyIncludeFilesWithExtension
* @param array $onlyIncludeFilesWithExtension
*
* @return array
*/
public function getFilesOlderThan(DateTime $date, array $onlyIncludeFilesWithExtension)
{
$allFiles = $this->disk->allFiles($this->path);

foreach($onlyIncludeFilesWithExtension as $extension)
{
foreach ($onlyIncludeFilesWithExtension as $extension) {
$backupFiles = $this->filterFilesOnExtension($allFiles, $extension);
}

return $this->filterFilesOnDate($backupFiles, $date);
}

/**
* Return $files with included $extension
* Return $files with included $extension.
*
* @param $backupFiles
* @param $extension
*
* @return array
*/
private function filterFilesOnExtension($backupFiles, $extension)
{
return array_filter($backupFiles, function($file) use($extension){
return array_filter($backupFiles, function ($file) use ($extension) {
return strtolower(pathinfo($file, PATHINFO_EXTENSION)) == $extension;
});
}

/**
* Filter files on given $date
* Filter files on given $date.
*
* @param $files
* @param DateTime $date
*
* @return array
*/
private function filterFilesOnDate($files, DateTime $date)
{
return array_filter($files, function($file) use($date){
return array_filter($files, function ($file) use ($date) {
return $this->disk->lastModified($file) < $date->getTimeStamp();
});
}
Expand Down
16 changes: 12 additions & 4 deletions src/config/laravel-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,19 @@
'maxAgeInDays' => 90,
],

/*
* The path to the mysqldump binary. You can leave this empty
* if the binary is installed in the default location.
*/
'mysql' => [
/*
* The path to the mysqldump binary. You can leave this empty
* if the binary is installed in the default location.
*/
'dump_command_path' => '',

/*
* If your server supports it you can turn on extended insert.
* This will result in a smaller dump file and speeds up the backup process.
*
* See: https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_extended-insert
*/
'useExtendedInsert' => false,
],
];

0 comments on commit 38633b8

Please sign in to comment.