Skip to content

Commit

Permalink
Simplify config
Browse files Browse the repository at this point in the history
  • Loading branch information
miks committed Dec 11, 2022
1 parent d62e586 commit 5075f44
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Laravel MySQL to S3 Backup

This is a very simple database backup script for Laravel. It takes a `mysqldump` and saves it to [Amazon S3](http://aws.amazon.com/s3/) or compatible object storage.
It also supports trimming backups to only have X days worth on S3.

This package is very opinionated. Other backup scripts can support other database types or other places besides S3 to store your backup. This does not.

Expand All @@ -19,18 +18,7 @@ This package is very opinionated. Other backup scripts can support other databas
php artisan vendor:publish --provider="LaravelMysqlS3Backup\ServiceProvider"
```
Edit `config/laravel-mysql-s3-backup.php`:
```php
's3' => [
'key' => 'AMAZON_API_KEY',
'secret' => 'AMAZON_API_SECRET',
'bucket' => 'your-bucket-name',
'region' => 'your-bucket-region',
'endpoint' => env('AWS_ENDPOINT'),
'folder' => env('BACKUP_FOLDER'),
],
```
Edit `config/mysql-s3-backup.php`
## Usage
Expand Down
24 changes: 12 additions & 12 deletions src/LaravelMysqlS3Backup/MysqlS3Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ public function handle()
escapeshellarg(config('database.connections.mysql.password'))
);

if (config('laravel-mysql-s3-backup.custom_mysqldump_args')) {
$cmd .= ' ' . config('laravel-mysql-s3-backup.custom_mysqldump_args');
if (config('mysql-s3-backup.custom_mysqldump_args')) {
$cmd .= ' ' . config('mysql-s3-backup.custom_mysqldump_args');
}

$cmd .= ' ' . escapeshellarg(config('database.connections.mysql.database'));

$fileName = config('laravel-mysql-s3-backup.backup_dir') . '/' . sprintf(config('laravel-mysql-s3-backup.filename'), date('Ymd-His'));
$fileName = config('mysql-s3-backup.backup_dir') . '/' . sprintf(config('mysql-s3-backup.filename'), date('Ymd-His'));

// Handle gzip
if (config('laravel-mysql-s3-backup.gzip')) {
if (config('mysql-s3-backup.gzip')) {
$fileName .= '.gz';
$cmd .= sprintf(' | gzip > %s', escapeshellarg($fileName));
} else {
Expand All @@ -64,7 +64,7 @@ public function handle()
}

$process = Process::fromShellCommandline('bash -o pipefail -c "' . $cmd . '"');
$process->setTimeout(config('laravel-mysql-s3-backup.sql_timeout'));
$process->setTimeout(config('mysql-s3-backup.sql_timeout'));
$process->run();

if (!$process->isSuccessful()) {
Expand All @@ -78,19 +78,19 @@ public function handle()
// Upload to S3
$s3 = new S3Client([
'credentials' => [
'key' => config('laravel-mysql-s3-backup.s3.key'),
'secret' => config('laravel-mysql-s3-backup.s3.secret'),
'key' => config('mysql-s3-backup.s3.key'),
'secret' => config('mysql-s3-backup.s3.secret'),
],
'endpoint' => config('laravel-mysql-s3-backup.s3.endpoint'),
'region' => config('laravel-mysql-s3-backup.s3.region'),
'endpoint' => config('mysql-s3-backup.s3.endpoint'),
'region' => config('mysql-s3-backup.s3.region'),
'version' => 'latest',
'use_path_style_endpoint' => config('laravel-mysql-s3-backup.s3.use_path_style_endpoint'),
'use_path_style_endpoint' => config('mysql-s3-backup.s3.use_path_style_endpoint'),
]);

$bucket = config('laravel-mysql-s3-backup.s3.bucket');
$bucket = config('mysql-s3-backup.s3.bucket');
$key = basename($fileName);

if ($folder = config('laravel-mysql-s3-backup.s3.folder')) {
if ($folder = config('mysql-s3-backup.s3.folder')) {
$key = $folder . '/' . $key;
}

Expand Down
10 changes: 6 additions & 4 deletions src/LaravelMysqlS3Backup/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class ServiceProvider extends BaseServiceProvider
public function boot()
{
$this->publishes([
__DIR__.'/config/config.php' => config_path('laravel-mysql-s3-backup.php'),
__DIR__ . '/config/config.php' => config_path('laravel-mysql-s3-backup.php'),
], 'config');

$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
$schedule->call(fn() => Artisan::call('db:backup'))->cron(config('laravel-mysql-s3-backup.scheduler_cron'));
});
if (config('mysql-s3-backup.scheduler_enabled')) {
$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
$schedule->call(fn() => Artisan::call('db:backup'))->cron(config('mysql-s3-backup.scheduler_cron'));
});
}
}

/**
Expand Down
23 changes: 14 additions & 9 deletions src/LaravelMysqlS3Backup/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* to a specified bucket
*/
's3' => [
'key' => env('AWS_API_KEY'),
'secret' => env('AWS_API_SECRET'),
'bucket' => env('AWS_S3_BUCKET'),
'region' => env('AWS_S3_REGION'),
'endpoint' => env('AWS_ENDPOINT'),
'folder' => env('BACKUP_FOLDER'),
'use_path_style_endpoint' => env('USE_PATH_STYLE_ENDPOINT', false),
'key' => env('MYSQL_S3_BACKUP_AWS_API_KEY'),
'secret' => env('MYSQL_S3_BACKUP_AWS_API_SECRET'),
'bucket' => env('MYSQL_S3_BACKUP_AWS_S3_BUCKET'),
'region' => env('MYSQL_S3_BACKUP_AWS_S3_REGION'),
'endpoint' => env('MYSQL_S3_BACKUP_AWS_ENDPOINT'),
'folder' => env('MYSQL_S3_BACKUP_FOLDER'),
'use_path_style_endpoint' => env('MYSQL_S3_BACKUP_USE_PATH_STYLE_ENDPOINT', false),
],

/*
Expand All @@ -36,10 +36,15 @@
/*
* Where to store the backup file locally
*/
'backup_dir' => '/tmp',
'backup_dir' => env('MYSQL_S3_BACKUP_LOCAL_DIR', '/tmp'),

/*
* Configure whether scheduler is enabled
*/
'scheduler_enabled' => env('MYSQL_S3_BACKUP_SCHEDULER_ENABLED', false),

/*
* Scheduler backup job cron time
*/
'scheduler_cron' => '42 2 * * *',
'scheduler_cron' => env('MYSQL_S3_BACKUP_SCHEDULER_CRON', '10 0 * * *'),
];

0 comments on commit 5075f44

Please sign in to comment.