diff --git a/docs/available-checks/backups.md b/docs/available-checks/backups.md new file mode 100644 index 00000000..c6907a33 --- /dev/null +++ b/docs/available-checks/backups.md @@ -0,0 +1,88 @@ +--- +title: Backups +weight: 2 +--- + +Using a package like [spatie/laravel-backup](https://spatie.be/docs/laravel-backup) you can create backups of your application. The backups are stored as zip files in a directory. + +The `BackupCheck` will verify if your backups are up to date. It can check: +- if the youngest backup has been made before a certain date +- if the oldest backup was made after a certain date +- the number of backups +- the size of the backups + +## Usage + +Here's how you can register the check. + +You can use the `locatedAt` method to specify the directory where the backups are stored. The `locatedAt` method accepts a glob pattern. + +```php +use Spatie\Health\Facades\Health; +use Spatie\Health\Checks\Checks\BackupsCheck; + +Health::checks([ + BackupsCheck::new()->locatedAt('/path/to/backups/*.zip'), +]); +``` + +### Check the number of the backups + +You can use the `numberOfBackups` method to check if the number of backups is within a certain range. +The function accepts a `min` and/or a `max` parameter. + +```php +use Spatie\Health\Facades\Health; +use Spatie\Health\Checks\Checks\BackupsCheck; + +Health::checks([ + BackupsCheck::new()->locatedAt('/path/to/backups/*.zip')->numberOfBackups(min: 5, max: 10), +]); +``` + +### Check the age of the backups + +You can use the `youngestBackShouldHaveBeenMadeBefore` method to check if the youngest backup was made before a certain date. + +Here's an example where we make sure the most recent backup is not older than 1 day. + +```php +use Spatie\Health\Facades\Health; +use Spatie\Health\Checks\Checks\BackupsCheck; + +Health::checks([ + BackupsCheck::new() + ->locatedAt('/path/to/backups/*.zip') + ->youngestBackShouldHaveBeenMadeBefore(now()->subDays(1)), +]); +``` + +You can use the `oldestBackShouldHaveBeenMadeAfter` method to check if the oldest backup was made after a certain date. + +Here's an example where we make sure the oldest backup is older than 1 week. + +```php +use Spatie\Health\Facades\Health; +use Spatie\Health\Checks\Checks\BackupsCheck; + +Health::checks([ + BackupsCheck::new() + ->locatedAt('/path/to/backups/*.zip') + ->oldestBackShouldHaveBeenMadeAfter(now()->subWeeks(1)), +]); +``` + +### Specify a minimum size + +You can use the `atLeastSizeInMb` method to specify a minimum size for the backups. Only backups that are larger than the specified size will be considered valid. + +```php +use Spatie\Health\Facades\Health; +use Spatie\Health\Checks\Checks\BackupsCheck; + +Health::checks([ + BackupsCheck::new() + ->locatedAt('/path/to/backups/*.zip') + ->atLeastSizeInMb(20), +]); +``` diff --git a/docs/available-checks/cache.md b/docs/available-checks/cache.md index c695dedd..6ad351a0 100644 --- a/docs/available-checks/cache.md +++ b/docs/available-checks/cache.md @@ -1,6 +1,6 @@ --- title: Application Cache -weight: 2 +weight: 3 --- This check makes sure the application can connect to your cache system and read/write to the cache keys. By default, this check will make sure the `default` connection is working. diff --git a/docs/available-checks/cached-config-routes-and-events.md b/docs/available-checks/cached-config-routes-and-events.md index 64780f52..0a50dce5 100644 --- a/docs/available-checks/cached-config-routes-and-events.md +++ b/docs/available-checks/cached-config-routes-and-events.md @@ -1,6 +1,6 @@ --- title: Cached config, routes, and events -weight: 3 +weight: 4 --- To improve performance, Laravel can cache configuration files, routes and events. Using the `OptimizedAppCheck` you can make sure these things are actually cached. diff --git a/docs/available-checks/cpu-load.md b/docs/available-checks/cpu-load.md index 81cffb96..a7437bf5 100644 --- a/docs/available-checks/cpu-load.md +++ b/docs/available-checks/cpu-load.md @@ -1,6 +1,6 @@ --- title: CPU load -weight: 4 +weight: 5 --- This check makes sure that your CPU load isn't too high. diff --git a/docs/available-checks/db-connection-count.md b/docs/available-checks/db-connection-count.md index 7a630256..786a5bd5 100644 --- a/docs/available-checks/db-connection-count.md +++ b/docs/available-checks/db-connection-count.md @@ -1,6 +1,6 @@ --- title: DB connection count -weight: 6 +weight: 7 --- This check makes sure your database doesn't have too much active connections. This check supports MySQL and Postgres. diff --git a/docs/available-checks/db-connection.md b/docs/available-checks/db-connection.md index 4040d563..ebfcebcb 100644 --- a/docs/available-checks/db-connection.md +++ b/docs/available-checks/db-connection.md @@ -1,6 +1,6 @@ --- title: DB connection -weight: 5 +weight: 6 --- This check makes sure your application can connect to a database. If the `default` database connection does not work, this check will fail. diff --git a/docs/available-checks/db-size-check.md b/docs/available-checks/db-size-check.md index a1e99599..db7b2ef4 100644 --- a/docs/available-checks/db-size-check.md +++ b/docs/available-checks/db-size-check.md @@ -1,6 +1,6 @@ --- title: DB size -weight: 7 +weight: 8 --- This check makes sure that your database is not too big. This check supports MySQL and Postgres. diff --git a/docs/available-checks/db-table-size-check.md b/docs/available-checks/db-table-size-check.md index 0aa42053..2c235a13 100644 --- a/docs/available-checks/db-table-size-check.md +++ b/docs/available-checks/db-table-size-check.md @@ -1,6 +1,6 @@ --- title: DB table size -weight: 8 +weight: 9 --- This check makes sure the tables of your database are not too big. This check supports MySQL and Postgres. diff --git a/docs/available-checks/debug-mode.md b/docs/available-checks/debug-mode.md index c8e36fc8..4484acc6 100644 --- a/docs/available-checks/debug-mode.md +++ b/docs/available-checks/debug-mode.md @@ -1,6 +1,6 @@ --- title: Debug mode -weight: 9 +weight: 10 --- This check will make sure that debug mode is set to `false`. It will fail when debug mode is `true`. diff --git a/docs/available-checks/environment.md b/docs/available-checks/environment.md index cb3bf616..7da03d36 100644 --- a/docs/available-checks/environment.md +++ b/docs/available-checks/environment.md @@ -1,6 +1,6 @@ --- title: Environment -weight: 10 +weight: 11 --- This check will make sure your application is running used the right environment. By default, this check will fail when the environment is not equal to `production`. diff --git a/docs/available-checks/flare-error-count.md b/docs/available-checks/flare-error-count.md index 9010e795..ac5f88c9 100644 --- a/docs/available-checks/flare-error-count.md +++ b/docs/available-checks/flare-error-count.md @@ -1,6 +1,6 @@ --- title: Flare error count -weight: 11 +weight: 12 --- This check will monitor the amount of errors and exceptions your application throws. For this check you'll need to have an account on [Flare](https://flareapp.io). diff --git a/docs/available-checks/horizon.md b/docs/available-checks/horizon.md index 74bae479..c12f53da 100644 --- a/docs/available-checks/horizon.md +++ b/docs/available-checks/horizon.md @@ -1,6 +1,6 @@ --- title: Horizon -weight: 12 +weight: 13 --- This check will make sure Horizon is running. It will report a warning when Horizon is paused, and a failure when Horizon is not running. diff --git a/docs/available-checks/meilisearch.md b/docs/available-checks/meilisearch.md index ffa26707..6bd99cf8 100644 --- a/docs/available-checks/meilisearch.md +++ b/docs/available-checks/meilisearch.md @@ -1,8 +1,8 @@ --- title: MeiliSearch -weight: 13 +weight: 14 --- - +[meilisearch.md](meilisearch.md) This check will verify if MeiliSearch is running. It will call MeiliSearch's [built-in health endpoint](https://docs.meilisearch.com/reference/api/health.html) and verify that its status returns `available`. ## Usage diff --git a/docs/available-checks/overview.md b/docs/available-checks/overview.md index 9f57f454..79fdf440 100644 --- a/docs/available-checks/overview.md +++ b/docs/available-checks/overview.md @@ -8,6 +8,7 @@ Using this package you can register one or more checks to verify the health of y These are the checks created by us: - [Application Cache](cache) +- [Backups](backups) - [CPU Load](cpu-load) - [Database Connection](db-connection) - [Database Connection Count](db-connection-count) diff --git a/docs/available-checks/ping.md b/docs/available-checks/ping.md index 30e39dde..3137d4da 100644 --- a/docs/available-checks/ping.md +++ b/docs/available-checks/ping.md @@ -1,6 +1,6 @@ --- title: Ping -weight: 14 +weight: 15 --- This check will send a request to a given URL. It will report a failure when that URL doesn't respond with a successful response code within a second. diff --git a/docs/available-checks/queue.md b/docs/available-checks/queue.md index 1f43707f..ed152c30 100644 --- a/docs/available-checks/queue.md +++ b/docs/available-checks/queue.md @@ -1,6 +1,6 @@ --- title: Queue -weight: 15 +weight: 16 --- This check will make sure that queued jobs are running. This check works by dispatching a test job (this will be done via a scheduled command), and verify if that test job is handled on time. diff --git a/docs/available-checks/redis-memory-usage.md b/docs/available-checks/redis-memory-usage.md index 3a8c812a..1e9d63ba 100644 --- a/docs/available-checks/redis-memory-usage.md +++ b/docs/available-checks/redis-memory-usage.md @@ -1,6 +1,6 @@ --- title: Redis memory usage -weight: 17 +weight: 18 --- This check makes sure that Redis is not consuming too much memory. diff --git a/docs/available-checks/redis.md b/docs/available-checks/redis.md index 0a0648b7..6874f6f6 100644 --- a/docs/available-checks/redis.md +++ b/docs/available-checks/redis.md @@ -1,6 +1,6 @@ --- title: Redis -weight: 16 +weight: 17 --- This check will make sure Redis is running. By default, this check will make sure the `default` connection is working. diff --git a/docs/available-checks/schedule.md b/docs/available-checks/schedule.md index 6c54521f..2b40f0d6 100644 --- a/docs/available-checks/schedule.md +++ b/docs/available-checks/schedule.md @@ -1,6 +1,6 @@ --- title: Schedule -weight: 18 +weight: 19 --- This check will make sure the schedule is running. If the check detects that the schedule is not run every minute, it will fail. diff --git a/docs/available-checks/security-advisories.md b/docs/available-checks/security-advisories.md index 5a79c82f..0e440628 100644 --- a/docs/available-checks/security-advisories.md +++ b/docs/available-checks/security-advisories.md @@ -1,6 +1,6 @@ --- title: Security advisories -weight: 19 +weight: 20 --- This check will check if the PHP packages installed in your project have known security vulnerabilities. This check works using [Packagist's security vulnerability API](https://php.watch/articles/composer-audit#packagist-vuln-list-api). diff --git a/docs/available-checks/used-disk-space.md b/docs/available-checks/used-disk-space.md index 1234ecdf..a14223a2 100644 --- a/docs/available-checks/used-disk-space.md +++ b/docs/available-checks/used-disk-space.md @@ -1,6 +1,6 @@ --- title: Used disk space -weight: 20 +weight: 21 --- This check will monitor the percentage of available disk space. diff --git a/src/Checks/Checks/BackupsCheck.php b/src/Checks/Checks/BackupsCheck.php index 4db63d1d..8332e457 100644 --- a/src/Checks/Checks/BackupsCheck.php +++ b/src/Checks/Checks/BackupsCheck.php @@ -18,7 +18,9 @@ class BackupsCheck extends Check protected int $minimumSizeInMegabytes = 0; - protected int $minimumNumberOfBackups = 0; + protected ?int $minimumNumberOfBackups = null; + protected ?int $maximumNumberOfBackups = null; + public function locatedAt(string $globPath): self { @@ -48,9 +50,10 @@ public function atLeastSizeInMb(int $minimumSizeInMegabytes): self return $this; } - public function atLeastNumberOfBackups(int $minimumNumberOfBackups): self + public function numberOfBackups(int $min = null, int $max = null): self { - $this->minimumNumberOfBackups = $minimumNumberOfBackups; + $this->minimumNumberOfBackups = $min; + $this->maximumNumberOfBackups = $max; return $this; } @@ -75,8 +78,16 @@ public function run(): Result return Result::make()->failed('No backups found that are large enough'); } - if ($eligableBackups->count() < $this->minimumNumberOfBackups) { - return Result::make()->failed('Not enough backups found'); + if ($this->minimumNumberOfBackups) { + if ($eligableBackups->count() < $this->minimumNumberOfBackups) { + return Result::make()->failed('Not enough backups found'); + } + } + + if ($this->maximumNumberOfBackups) { + if ($eligableBackups->count() > $this->maximumNumberOfBackups) { + return Result::make()->failed('Too many backups found'); + } } if ($this->youngestShouldHaveBeenMadeBefore) { diff --git a/tests/Checks/BackupsCheckTest.php b/tests/Checks/BackupsCheckTest.php index ccffe4d6..fdd3a329 100644 --- a/tests/Checks/BackupsCheckTest.php +++ b/tests/Checks/BackupsCheckTest.php @@ -118,7 +118,7 @@ $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) - ->atLeastNumberOfBackups(2) + ->numberOfBackups(min: 2) ->run(); expect($result)->status->toBe(Status::failed()); @@ -126,10 +126,27 @@ $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) - ->atLeastNumberOfBackups(2) + ->numberOfBackups(min: 2) ->run(); expect($result)->status->toBe(Status::ok()); }); +it('can make sure that there are not too much backups', function() { + addTestFile($this->temporaryDirectory->path('first.zip')); + addTestFile($this->temporaryDirectory->path('second.zip')); + + $result = $this->backupsCheck + ->locatedAt($this->temporaryDirectory->path('*.zip')) + ->numberOfBackups(max: 2) + ->run(); + expect($result)->status->toBe(Status::ok()); + addTestFile($this->temporaryDirectory->path('third.zip')); + + $result = $this->backupsCheck + ->locatedAt($this->temporaryDirectory->path('*.zip')) + ->numberOfBackups(max: 2) + ->run(); + expect($result)->status->toBe(Status::failed()); +});