Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 17a519b

Browse files
committed
Added sorting to backup finder. (sort by newest to oldest). Updated readme with backup examples. Added backup sort testing.
1 parent 5874c58 commit 17a519b

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

README.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ Usage Example (all options)
7676

7777
```php
7878
$db = new \Filebase\Database([
79-
'dir' => 'path/to/database/dir',
80-
'format' => \Filebase\Format\Json::class,
81-
'cache' => true,
82-
'cache_expires' => 1800,
83-
'pretty' => true,
79+
'dir' => 'path/to/database/dir',
80+
'backupLocation' => 'path/to/database/backup/dir',
81+
'format' => \Filebase\Format\Json::class,
82+
'cache' => true,
83+
'cache_expires' => 1800,
84+
'pretty' => true,
8485
'validate' => [
8586
'name' => [
8687
'valid.type' => 'string',
@@ -93,6 +94,7 @@ $db = new \Filebase\Database([
9394
|Name |Type |Default Value |Description |
9495
|--- |--- |--- |--- |
9596
|`dir` |string |current directory |The directory where the database files are stored. |
97+
|`backupLocation` |string |current directory (`/backups`) |The directory where the backup zip files will be stored. |
9698
|`format` |object |`\Filebase\Format\Json` |The format class used to encode/decode data |
9799
|`validate` |array | |Check [Validation Rules](https://github.com/tmarois/Filebase#6-validation-optional) for more details |
98100
|`cache` |bool |false |Stores [query](https://github.com/tmarois/Filebase#8-queries) results into cache for faster loading. |
@@ -400,13 +402,34 @@ Cached queries will only be used if a specific saved cache is less than the expi
400402
## (10) Database Backups
401403
By default you can backup your database using `$db->backup()->save()`, this will create a `.zip` file of your entire database based on your `dir` path.
402404

403-
### Methods used with invoking the Backup class:
404-
These methods can be used when invoking `$db->backup()` on your `Database`.
405+
### Methods:
406+
These methods can be used when invoking `backup()` on your `Database`.
405407

406-
- `save()` Saves a backup of your database.
407-
- `clean()` Purges all existing .zip files within the backup location
408+
- `save()` Saves a backup of your database (in your backup location `.zip`)
409+
- `clean()` Purges all existing backups (`.zip` files in your backup location)
408410
- `find()` Returns an `array` of all existing backups (array key by `time()` when backup was created)
409-
- `rollback()` Restore your database to an existing backup. (by default restores the latest backup if any exist)
411+
412+
**Example:**
413+
414+
```php
415+
416+
// invoke your database
417+
$database = new \Filebase\Database([
418+
'dir' => '/storage/users',
419+
'backupLocation' => '/storage/backup',
420+
]);
421+
422+
// save a new backup of your database
423+
// will look something like /storage/backup/1504631092.zip
424+
$database->backup()->save();
425+
426+
// delete all existing backups
427+
$database->backup()->clean();
428+
429+
// get a list of all existing backups (organized from new to old)
430+
$backups = $database->backup()->find();
431+
432+
```
410433

411434
## Why Filebase?
412435

src/Backup.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public function save()
6565

6666
if ($results = $this->zip($this->config->dir, $backupFile))
6767
{
68-
return $backupFile;
68+
$basename = basename($backupFile);
69+
return $basename;
6970
}
7071

7172
throw new \Exception('Error backing up database.');
@@ -88,9 +89,11 @@ public function find()
8889
foreach($files as $file)
8990
{
9091
$basename = str_replace('.zip','',basename($file));
91-
$backups[$basename] = $file;
92+
$backups[$basename] = $this->backupLocation.'/'.$basename.'.zip';
9293
}
9394

95+
krsort($backups);
96+
9497
return $backups;
9598
}
9699

tests/BackupTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,35 @@ public function testBackupFind()
7878
}
7979

8080

81+
public function testBackupFindSort()
82+
{
83+
$db = new \Filebase\Database([
84+
'dir' => __DIR__.'/databases/mydatabasetobackup',
85+
'backupLocation' => __DIR__.'/databases/storage/backups'
86+
]);
87+
88+
$db->flush(true);
89+
90+
for ($x = 1; $x <= 25; $x++)
91+
{
92+
$user = $db->get(uniqid());
93+
$user->name = 'John';
94+
$user->save();
95+
}
96+
97+
$db->backup()->save();
98+
$db->backup()->save();
99+
$last = str_replace('.zip','',$db->backup()->save());
100+
101+
$backups = $db->backup()->find();
102+
$backupCurrent = current($backups);
103+
104+
$lastBackup = str_replace('.zip','',basename($backupCurrent));
105+
106+
$this->assertEquals($last,$lastBackup);
107+
}
108+
109+
81110
public function testBackupCleanup()
82111
{
83112
$db = new \Filebase\Database([

0 commit comments

Comments
 (0)