Skip to content

Commit

Permalink
add support for sufix/prefix and add suport for read/write dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Apr 26, 2015
1 parent 1eb5bbe commit 2850c0d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 14 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,20 @@ return [
* accidentally end up committing these backups.
*/
'path' => 'backups',

/*
* By default the backups will be stored as a zipfile with a
* timestamp as the filename. With these options You can
* specify a prefix and a suffix for the filename.
*/
'prefix' => '',
'suffix' => '',
],

'clean' => [
/*
* The clean command will remove all backups on all configured filesystems
* that are older than this amount of days.
* that are older then this amount of days.
*/
'maxAgeInDays' => 90,
],
Expand All @@ -109,8 +117,6 @@ return [
'dump_command_path' => '',
],
];


```

## Usage
Expand All @@ -123,12 +129,18 @@ Use this command start the backup and store the zipfile to the filesystem(s) you
php artisan backup:run
```

A zip-file, containing all files in the directories you specified along the dump of your database, will be created on the filesystem(s) you specified in the config-file.

If you want to take a backup of only the db (without all other files that you might have configured) you can use this command:
``` bash
php artisan backup:run --only-db
```

A zip-file, containing all files in the directories you specified along the dump of your database, will be created on the filesystem(s) you specified in the config-file.
You can also manually specify a prefix and suffix to be used in the filename of the zipfile.
``` bash
php artisan backup:run --prefix="backup-" --suffix="-manual".
```
These arguments take precedence over the prefix and suffix specified in the config file.

### Cleanup

Expand Down
24 changes: 23 additions & 1 deletion src/BackupHandlers/Database/DatabaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,31 @@ protected function buildMySQL(array $config)
$config['database'],
$config['username'],
$config['password'],
$config['host'],
$this->determineHost($config),
$port,
$socket
);
}

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

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

throw new Exception('could not determine host from config');
}
}
59 changes: 51 additions & 8 deletions src/Commands/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ public function fire()
$backupZipFile = $this->createZip($files);

foreach ($this->getTargetFileSystems() as $fileSystem) {
$this->comment('Start uploading backup to '.$fileSystem.'-filesystem...');

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

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

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

$this->info('Backup successfully completed');
Expand Down Expand Up @@ -177,7 +171,54 @@ protected function writeIgnoreFile($disk, $dumpDirectory)
*/
protected function getBackupDestinationFileName()
{
return config('laravel-backup.destination.path').'/'.date('YmdHis').'.zip';
$backupDirectory = config('laravel-backup.destination.path');

This comment has been minimized.

Copy link
@spaceemotion

spaceemotion Apr 27, 2015

You left out the trailing slash. Now all the files are stored as backup{prefix}XXXXXXXXXX{suffix}.zip instead of inside a backup directory, is that intentional?

This comment has been minimized.

Copy link
@freekmurze

freekmurze Apr 27, 2015

Author Member

Nope, this is a bug. Fixed in 2.3.1.

$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');
}

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

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

/**
* 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...');

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

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

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

/**
Expand All @@ -189,6 +230,8 @@ protected function getOptions()
{
return [
['only-db', null, InputOption::VALUE_NONE, 'Only backup the database.'],
['prefix', null, InputOption::VALUE_REQUIRED, 'The name of the zip file will get prefixed with this string.'],
['suffix', null, InputOption::VALUE_REQUIRED, 'The name of the zip file will get suffixed with this string.'],
];
}
}
8 changes: 8 additions & 0 deletions src/config/laravel-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
* accidentally end up committing these backups.
*/
'path' => 'backups',

/*
* By default the backups will be stored as a zipfile with a
* timestamp as the filename. With these options You can
* specify a prefix and a suffix for the filename.
*/
'prefix' => '',
'suffix' => '',
],

'clean' => [
Expand Down
11 changes: 11 additions & 0 deletions tests/DatabaseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ public function testMySQL()

$this->assertInstanceOf('Spatie\Backup\BackupHandlers\Database\Databases\MySQLDatabase', $database);
}

public function testDetermineHost()
{
$databaseBuilder = new DatabaseBuilder();

$determineHostResult = $databaseBuilder->determineHost(['host' => 'testhost']);
$this->assertSame('testhost', $determineHostResult);

$determineHostResult = $databaseBuilder->determineHost(['read' => ['host' => 'testhost']]);
$this->assertSame('testhost', $determineHostResult);
}
}
Empty file.

0 comments on commit 2850c0d

Please sign in to comment.