Skip to content

Commit cf4ab2f

Browse files
Merge pull request #23 from JeffGepiga/theonly
Added optional argument --only="db|files" in Command Line to backup database or file only
2 parents e6c6689 + d487ab9 commit cf4ab2f

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Although packages provides GUI interface to manage backups, following commands a
6969

7070
```bash
7171
backupmanager:create Creates backup of files and/or database.
72+
backupmanager:create --only="db" Creates backup of database only.
73+
backupmanager:create --only="files" Creates backup of files only.
7274
backupmanager:list Shows list of backups taken.
7375
backupmanager:restore Restores a backup already taken.
7476
```

src/BackupManager.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public function getBackups()
4444
$files = Storage::disk($this->disk)->listContents($this->backupPath);
4545

4646
$filesData = [];
47-
4847
foreach ($files as $index => $file) {
4948
if ($file instanceof \League\Flysystem\FileAttributes) {
50-
$name = str_replace($this->backupPath,"",$file->path());
49+
$name = str_replace($this->backupPath,'',$file->path());
5150
$array = explode('_', $name);
5251
$filesData[] = [
5352
'name' => $name,
@@ -88,7 +87,6 @@ public function createBackup()
8887
$this->backupFiles();
8988
$this->backupDatabase();
9089
$this->deleteOldBackups();
91-
9290
return $this->getBackupStatus();
9391
}
9492

@@ -133,9 +131,9 @@ public function deleteBackups(array $files)
133131
/**
134132
* Backup files
135133
*/
136-
protected function backupFiles()
134+
public function backupFiles($bypass=false)
137135
{
138-
if (config('backupmanager.backups.files.enable')) {
136+
if (config('backupmanager.backups.files.enable') || $bypass===true) {
139137

140138
// delete previous backup for same date
141139
if (Storage::disk($this->disk)->exists($this->backupPath . $this->fBackupName)) {
@@ -180,15 +178,18 @@ function ($str) {
180178
// delete local file
181179
$storageLocal->delete($this->fBackupName);
182180
}
181+
if ($bypass===true) {
182+
$this->deleteOldBackups();
183+
}
183184
}
184185
}
185186

186187
/**
187188
* Backup Database
188189
*/
189-
protected function backupDatabase()
190+
public function backupDatabase($bypass=false)
190191
{
191-
if (config('backupmanager.backups.database.enable')) {
192+
if (config('backupmanager.backups.database.enable') || $bypass) {
192193

193194
// delete previous backup for same date
194195
if (Storage::disk($this->disk)->exists($this->backupPath . $this->dBackupName)) {
@@ -245,6 +246,10 @@ protected function backupDatabase()
245246
// delete local file
246247
$storageLocal->delete($this->dBackupName);
247248
}
249+
250+
if ($bypass===true) {
251+
$this->deleteOldBackups();
252+
}
248253
}
249254
}
250255

@@ -373,7 +378,6 @@ protected function deleteOldBackups()
373378
$now = time();
374379

375380
$files = Storage::disk($this->disk)->listContents($this->backupPath);
376-
377381
foreach ($files as $file) {
378382
if ($file['type'] !== 'file') {
379383
continue;

src/Console/BackupCommand.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,43 @@
88

99
class BackupCommand extends Command
1010
{
11-
protected $signature = 'backupmanager:create';
11+
12+
//added option for --only=files, --only=db
13+
protected $signature = 'backupmanager:create {--only=}';
1214
protected $description = 'Creates backup of files and/or database.';
1315

1416
public function handle()
1517
{
16-
$result = BackupManager::createBackup();
18+
$argument = $this->option('only');
19+
if ($argument!==null && !in_array($argument,['db','files']) ) {
20+
return $this->info('You can only select "files" or "db" argument!');
21+
}
22+
if ($argument===null) {
23+
$result = BackupManager::createBackup();
24+
}elseif($argument==='files'){
25+
$result = BackupManager::backupFiles(true);
26+
}else{
27+
$result = BackupManager::backupDatabase(true);
28+
}
1729

1830
// set status messages
19-
if ($result['f'] === true) {
31+
if (isset($result['f']) && $result['f'] === true) {
2032
$message = 'Files Backup Taken Successfully';
2133
$this->info($message);
2234
Log::info($message);
23-
} else {
35+
} elseif(isset($result['f']) && $result['f'] === false) {
2436
if (config('backupmanager.backups.files.enable')) {
2537
$message = 'Files Backup Failed';
2638
$this->error($message);
2739
Log::error($message);
2840
}
2941
}
3042

31-
if ($result['d'] === true) {
43+
if (isset($result['d']) && $result['d'] === true) {
3244
$message = 'Database Backup Taken Successfully';
3345
$this->info($message);
3446
Log::info($message);
35-
} else {
47+
} elseif(isset($result['d']) && $result['d'] === false) {
3648
if (config('backupmanager.backups.database.enable')) {
3749
$message = 'Database Backup Failed';
3850
$this->error($message);

0 commit comments

Comments
 (0)