Skip to content

Commit 8b78577

Browse files
Add a keep option to skip the cleanup. (#105)
Co-authored-by: Stefan Zweifel <stefan@stefanzweifel.dev>
1 parent 81913ad commit 8b78577

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ php artisan backup:restore
6868
--connection=mysql
6969
--password=my-secret-password
7070
--reset
71+
--keep
7172
```
7273

7374
Note that we used `latest` as the value for `--backup`. The command will automatically download the latest available backup and restore its database.
@@ -88,6 +89,9 @@ Password used to decrypt a possible encrypted backup. Defaults to encryption pas
8889
#### `--reset`
8990
Reset the database before restoring the backup. Defaults to `false`.
9091

92+
#### `--keep`
93+
Keeps the downloaded backup (and the decrypted backup folder) in existence. You need to delete it by hand. Useful for extracting and restoring backuped files. Defaults to `false`.
94+
9195
---
9296

9397
The command asks for confirmation before starting the restore process. If you run the `backup:restore`-command in an environment where you can't confirm the process (for example through a cronjob), you can use the `--no-interaction`-option to bypass the question.

src/Actions/CleanupLocalBackupAction.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Wnx\LaravelBackupRestore\Actions;
66

77
use Illuminate\Support\Facades\Storage;
8+
use Wnx\LaravelBackupRestore\Events\LocalBackupRemoved;
89
use Wnx\LaravelBackupRestore\PendingRestore;
910

1011
class CleanupLocalBackupAction
@@ -16,5 +17,7 @@ public function execute(PendingRestore $pendingRestore): void
1617

1718
Storage::disk($pendingRestore->restoreDisk)
1819
->deleteDirectory($pendingRestore->getPathToLocalDecompressedBackup());
20+
21+
event(new LocalBackupRemoved($pendingRestore));
1922
}
2023
}

src/Commands/RestoreCommand.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class RestoreCommand extends Command
4040
{--backup= : The backup to restore. Defaults to the latest backup.}
4141
{--connection= : The database connection to restore the backup to. Defaults to the first connection in config/backup.php.}
4242
{--password= : The password to decrypt the backup.}
43-
{--reset : Drop all tables in the database before restoring the backup.}';
43+
{--reset : Drop all tables in the database before restoring the backup.}
44+
{--keep : Keeps the downloaded zip and decrypted folder from the backup in existence. Handy for moving files afterwards by hand to the correct location.}
45+
';
4446

4547
public $description = 'Restore a database backup dump from a given disk to a database connection.';
4648

@@ -93,8 +95,10 @@ public function handle(
9395

9496
$importDumpAction->execute($pendingRestore);
9597

96-
info('Cleaning up …');
97-
$cleanupLocalBackupAction->execute($pendingRestore);
98+
if (!$this->option('keep')) {
99+
info('Cleaning up …');
100+
$cleanupLocalBackupAction->execute($pendingRestore);
101+
}
98102

99103
return $this->runHealthChecks($pendingRestore);
100104
}

src/Events/LocalBackupRemoved.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wnx\LaravelBackupRestore\Events;
6+
7+
use Wnx\LaravelBackupRestore\PendingRestore;
8+
9+
class LocalBackupRemoved
10+
{
11+
public function __construct(public readonly PendingRestore $pendingRestore) {}
12+
}

tests/Commands/RestoreCommandTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\Event;
77
use Wnx\LaravelBackupRestore\Commands\RestoreCommand;
88
use Wnx\LaravelBackupRestore\Events\DatabaseReset;
9+
use Wnx\LaravelBackupRestore\Events\LocalBackupRemoved;
910
use Wnx\LaravelBackupRestore\Exceptions\NoBackupsFound;
1011

1112
// MySQL
@@ -21,7 +22,7 @@
2122
->expectsOutputToContain('All health checks passed.')
2223
->assertSuccessful();
2324

24-
$result = DB::connection('mysql')->table('users')->count();
25+
$result = DB::connection('mysql-restore')->table('users')->count();
2526

2627
expect($result)->toBe(10);
2728
})->with([
@@ -124,7 +125,7 @@
124125
->expectsQuestion('Proceed to restore "Laravel/2023-01-28-mysql-no-compression-encrypted.zip" using the "mysql-restore" database connection. (Database: laravel_backup_restore, Host: 127.0.0.1, username: root)', true)
125126
->assertSuccessful();
126127

127-
$result = DB::connection('mysql')->table('users')->count();
128+
$result = DB::connection('mysql-restore')->table('users')->count();
128129

129130
expect($result)->toBe(10);
130131
})->group('mysql');
@@ -175,3 +176,24 @@
175176
->expectsOutputToContain('Database has not tables after restore.')
176177
->assertFailed();
177178
});
179+
180+
it('does not clear downloaded backup if --keep option is being used', function () {
181+
Event::fake([LocalBackupRemoved::class]);
182+
183+
$this->artisan(RestoreCommand::class, [
184+
'--disk' => 'remote',
185+
'--backup' => 'Laravel/2023-02-28-sqlite-no-compression-no-encryption.zip',
186+
'--connection' => 'sqlite-restore',
187+
'--password' => null,
188+
'--no-interaction' => true,
189+
'--keep' => true,
190+
])
191+
->expectsQuestion('Proceed to restore "Laravel/2023-02-28-sqlite-no-compression-no-encryption.zip" using the "sqlite-restore" database connection. (Database: database/database.sqlite)', true)
192+
->assertSuccessful();
193+
194+
Event::assertNotDispatched(LocalBackupRemoved::class);
195+
$files = \Illuminate\Support\Facades\Storage::disk('local')->allFiles('backup-restore-temp');
196+
197+
expect($files)->not->toBeEmpty();
198+
199+
})->group('sqlite');

tests/Databases/MySqlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
return $event->absolutePathToDump === $dumpFile;
2222
});
2323

24-
$result = DB::connection('mysql')->table('users')->count();
24+
$result = DB::connection('mysql-restore')->table('users')->count();
2525
expect($result)->toBe(10);
2626
})->with([
2727
__DIR__.'/../storage/Laravel/2023-01-28-mysql-no-compression-no-encryption.sql',

0 commit comments

Comments
 (0)