diff --git a/README.md b/README.md index 87f49a5f..bd12a8eb 100644 --- a/README.md +++ b/README.md @@ -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, ], ]; ``` diff --git a/src/BackupHandlers/Database/DatabaseBuilder.php b/src/BackupHandlers/Database/DatabaseBuilder.php index 5a4a35e3..b0f9fb05 100644 --- a/src/BackupHandlers/Database/DatabaseBuilder.php +++ b/src/BackupHandlers/Database/DatabaseBuilder.php @@ -17,7 +17,6 @@ public function __construct() public function getDatabase(array $realConfig) { - try { $this->buildMySQL($realConfig); } catch (Exception $e) { @@ -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']; } diff --git a/src/BackupHandlers/Database/Databases/MySQLDatabase.php b/src/BackupHandlers/Database/Databases/MySQLDatabase.php index 9c08edfb..0a37c296 100644 --- a/src/BackupHandlers/Database/Databases/MySQLDatabase.php +++ b/src/BackupHandlers/Database/Databases/MySQLDatabase.php @@ -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), @@ -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 ''; diff --git a/src/BackupHandlers/Files/FilesBackupHandler.php b/src/BackupHandlers/Files/FilesBackupHandler.php index 96d84499..9be47049 100644 --- a/src/BackupHandlers/Files/FilesBackupHandler.php +++ b/src/BackupHandlers/Files/FilesBackupHandler.php @@ -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) @@ -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) diff --git a/src/Commands/BackupCommand.php b/src/Commands/BackupCommand.php index ec68bd8d..4a0eb81f 100644 --- a/src/Commands/BackupCommand.php +++ b/src/Commands/BackupCommand.php @@ -66,8 +66,7 @@ protected function getAllFilesToBeBackedUp() $this->comment('Database dumped'); } - if($this->option('only-db')) - { + if ($this->option('only-db')) { return $files; } @@ -172,13 +171,14 @@ 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() @@ -186,7 +186,7 @@ public function getPrefix() if ($this->option('prefix') != '') { return $this->option('prefix'); } - + return config('laravel-backup.destination.prefix'); } @@ -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().'"'); } /** diff --git a/src/Commands/CleanCommand.php b/src/Commands/CleanCommand.php index 222a2a35..75baeedb 100644 --- a/src/Commands/CleanCommand.php +++ b/src/Commands/CleanCommand.php @@ -6,8 +6,8 @@ use Carbon\Carbon; use Spatie\Backup\FileHelpers\FileSelector; -class CleanCommand extends Command { - +class CleanCommand extends Command +{ /** * The console command name. * @@ -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!'); - - } /** @@ -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'); } } diff --git a/src/FileHelpers/FileSelector.php b/src/FileHelpers/FileSelector.php index 196a8146..8005fd90 100644 --- a/src/FileHelpers/FileSelector.php +++ b/src/FileHelpers/FileSelector.php @@ -2,8 +2,8 @@ use DateTime; -class FileSelector { - +class FileSelector +{ protected $disk; protected $path; @@ -15,18 +15,18 @@ 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); } @@ -34,29 +34,31 @@ public function getFilesOlderThan(DateTime $date, array $onlyIncludeFilesWithExt } /** - * 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(); }); } diff --git a/src/config/laravel-backup.php b/src/config/laravel-backup.php index a00467bf..308cb0bf 100644 --- a/src/config/laravel-backup.php +++ b/src/config/laravel-backup.php @@ -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, ], ];