Skip to content

Commit 1c7ca56

Browse files
committed
feat: backup all databases for mysql,mariadb,postgresql
1 parent bb6cb8e commit 1c7ca56

File tree

4 files changed

+88
-25
lines changed

4 files changed

+88
-25
lines changed

app/Jobs/DatabaseBackupJob.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ public function handle(): void
243243
try {
244244
if (str($databaseType)->contains('postgres')) {
245245
$this->backup_file = "/pg-dump-$database-".Carbon::now()->timestamp.'.dmp';
246+
if ($this->backup->dump_all) {
247+
$this->backup_file = '/pg-dump-all-'.Carbon::now()->timestamp.'.gz';
248+
}
246249
$this->backup_location = $this->backup_dir.$this->backup_file;
247250
$this->backup_log = ScheduledDatabaseBackupExecution::create([
248251
'database_name' => $database,
@@ -271,6 +274,9 @@ public function handle(): void
271274
$this->backup_standalone_mongodb($database);
272275
} elseif (str($databaseType)->contains('mysql')) {
273276
$this->backup_file = "/mysql-dump-$database-".Carbon::now()->timestamp.'.dmp';
277+
if ($this->backup->dump_all) {
278+
$this->backup_file = '/mysql-dump-all-'.Carbon::now()->timestamp.'.gz';
279+
}
274280
$this->backup_location = $this->backup_dir.$this->backup_file;
275281
$this->backup_log = ScheduledDatabaseBackupExecution::create([
276282
'database_name' => $database,
@@ -280,6 +286,9 @@ public function handle(): void
280286
$this->backup_standalone_mysql($database);
281287
} elseif (str($databaseType)->contains('mariadb')) {
282288
$this->backup_file = "/mariadb-dump-$database-".Carbon::now()->timestamp.'.dmp';
289+
if ($this->backup->dump_all) {
290+
$this->backup_file = '/mariadb-dump-all-'.Carbon::now()->timestamp.'.gz';
291+
}
283292
$this->backup_location = $this->backup_dir.$this->backup_file;
284293
$this->backup_log = ScheduledDatabaseBackupExecution::create([
285294
'database_name' => $database,
@@ -379,7 +388,11 @@ private function backup_standalone_postgresql(string $database): void
379388
if ($this->postgres_password) {
380389
$backupCommand .= " -e PGPASSWORD=$this->postgres_password";
381390
}
382-
$backupCommand .= " $this->container_name pg_dump --format=custom --no-acl --no-owner --username {$this->database->postgres_user} $database > $this->backup_location";
391+
if ($this->backup->dump_all) {
392+
$backupCommand .= " $this->container_name pg_dumpall --username {$this->database->postgres_user} | gzip > $this->backup_location";
393+
} else {
394+
$backupCommand .= " $this->container_name pg_dump --format=custom --no-acl --no-owner --username {$this->database->postgres_user} $database > $this->backup_location";
395+
}
383396

384397
$commands[] = $backupCommand;
385398
ray($commands);
@@ -400,8 +413,11 @@ private function backup_standalone_mysql(string $database): void
400413
{
401414
try {
402415
$commands[] = 'mkdir -p '.$this->backup_dir;
403-
$commands[] = "docker exec $this->container_name mysqldump -u root -p{$this->database->mysql_root_password} $database > $this->backup_location";
404-
ray($commands);
416+
if ($this->backup->dump_all) {
417+
$commands[] = "docker exec $this->container_name mysqldump -u root -p{$this->database->mysql_root_password} --all-databases --single-transaction --quick --lock-tables=false --compress | gzip > $this->backup_location";
418+
} else {
419+
$commands[] = "docker exec $this->container_name mysqldump -u root -p{$this->database->mysql_root_password} $database > $this->backup_location";
420+
}
405421
$this->backup_output = instant_remote_process($commands, $this->server);
406422
$this->backup_output = trim($this->backup_output);
407423
if ($this->backup_output === '') {
@@ -419,7 +435,11 @@ private function backup_standalone_mariadb(string $database): void
419435
{
420436
try {
421437
$commands[] = 'mkdir -p '.$this->backup_dir;
422-
$commands[] = "docker exec $this->container_name mariadb-dump -u root -p{$this->database->mariadb_root_password} $database > $this->backup_location";
438+
if ($this->backup->dump_all) {
439+
$commands[] = "docker exec $this->container_name mariadb-dump -u root -p{$this->database->mariadb_root_password} --all-databases --single-transaction --quick --lock-tables=false --compress > $this->backup_location";
440+
} else {
441+
$commands[] = "docker exec $this->container_name mariadb-dump -u root -p{$this->database->mariadb_root_password} $database > $this->backup_location";
442+
}
423443
ray($commands);
424444
$this->backup_output = instant_remote_process($commands, $this->server);
425445
$this->backup_output = trim($this->backup_output);

app/Livewire/Project/Database/BackupEdit.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class BackupEdit extends Component
3131
'backup.save_s3' => 'required|boolean',
3232
'backup.s3_storage_id' => 'nullable|integer',
3333
'backup.databases_to_backup' => 'nullable',
34+
'backup.dump_all' => 'required|boolean',
3435
];
3536

3637
protected $validationAttributes = [
@@ -40,6 +41,7 @@ class BackupEdit extends Component
4041
'backup.save_s3' => 'Save to S3',
4142
'backup.s3_storage_id' => 'S3 Storage',
4243
'backup.databases_to_backup' => 'Databases to Backup',
44+
'backup.dump_all' => 'Backup All Databases',
4345
];
4446

4547
protected $messages = [
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('scheduled_database_backups', function (Blueprint $table) {
15+
$table->boolean('dump_all')->default(false);
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('scheduled_database_backups', function (Blueprint $table) {
25+
$table->dropColumn('dump_all');
26+
});
27+
}
28+
};

resources/views/livewire/project/database/backup-edit.blade.php

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88
<livewire:project.database.backup-now :backup="$backup" />
99
@endif
1010
@if ($backup->database_id !== 0)
11-
<x-modal-confirmation
12-
title="Confirm Backup Schedule Deletion?"
13-
buttonTitle="Delete Backups and Schedule"
14-
isErrorButton
15-
submitAction="delete"
16-
:checkboxes="$checkboxes"
17-
:actions="['The selected backup schedule will be deleted.', 'Scheduled backups for this database will be stopped (if this is the only backup schedule for this database).']"
18-
confirmationText="{{ $backup->database->name }}"
19-
confirmationLabel="Please confirm the execution of the actions by entering the Database Name of the scheduled backups below"
20-
shortConfirmationLabel="Database Name"
21-
/>
11+
<x-modal-confirmation title="Confirm Backup Schedule Deletion?" buttonTitle="Delete Backups and Schedule"
12+
isErrorButton submitAction="delete" :checkboxes="$checkboxes" :actions="[
13+
'The selected backup schedule will be deleted.',
14+
'Scheduled backups for this database will be stopped (if this is the only backup schedule for this database).',
15+
]"
16+
confirmationText="{{ $backup->database->name }}"
17+
confirmationLabel="Please confirm the execution of the actions by entering the Database Name of the scheduled backups below"
18+
shortConfirmationLabel="Database Name" />
2219
@endif
2320
</div>
2421
<div class="w-48 pb-2">
@@ -36,23 +33,39 @@
3633
</div>
3734
@endif
3835
<div class="flex flex-col gap-2">
39-
<div class="flex gap-2">
36+
<h3>Settings</h3>
37+
<div class="flex gap-2 flex-col ">
4038
@if ($backup->database_type === 'App\Models\StandalonePostgresql')
41-
<x-forms.input label="Databases To Backup"
42-
helper="Comma separated list of databases to backup. Empty will include the default one."
43-
id="backup.databases_to_backup" />
39+
<div class="w-48">
40+
<x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave />
41+
</div>
42+
@if (!$backup->dump_all)
43+
<x-forms.input label="Databases To Backup"
44+
helper="Comma separated list of databases to backup. Empty will include the default one."
45+
id="backup.databases_to_backup" />
46+
@endif
4447
@elseif($backup->database_type === 'App\Models\StandaloneMongodb')
4548
<x-forms.input label="Databases To Include"
4649
helper="A list of databases to backup. You can specify which collection(s) per database to exclude from the backup. Empty will include all databases and collections.<br><br>Example:<br><br>database1:collection1,collection2|database2:collection3,collection4<br><br> database1 will include all collections except collection1 and collection2. <br>database2 will include all collections except collection3 and collection4.<br><br>Another Example:<br><br>database1:collection1|database2<br><br> database1 will include all collections except collection1.<br>database2 will include ALL collections."
4750
id="backup.databases_to_backup" />
4851
@elseif($backup->database_type === 'App\Models\StandaloneMysql')
49-
<x-forms.input label="Databases To Backup"
50-
helper="Comma separated list of databases to backup. Empty will include the default one."
51-
id="backup.databases_to_backup" />
52+
<div class="w-48">
53+
<x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave />
54+
</div>
55+
@if (!$backup->dump_all)
56+
<x-forms.input label="Databases To Backup"
57+
helper="Comma separated list of databases to backup. Empty will include the default one."
58+
id="backup.databases_to_backup" />
59+
@endif
5260
@elseif($backup->database_type === 'App\Models\StandaloneMariadb')
53-
<x-forms.input label="Databases To Backup"
54-
helper="Comma separated list of databases to backup. Empty will include the default one."
55-
id="backup.databases_to_backup" />
61+
<div class="w-48">
62+
<x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave />
63+
</div>
64+
@if (!$backup->dump_all)
65+
<x-forms.input label="Databases To Backup"
66+
helper="Comma separated list of databases to backup. Empty will include the default one."
67+
id="backup.databases_to_backup" />
68+
@endif
5669
@endif
5770
</div>
5871
<div class="flex gap-2">

0 commit comments

Comments
 (0)