Backups | laravel-health | Spatie

 SPATIE

  Laravel Health
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-health](https://spatie.be/docs/laravel-health/v1)  Available-checks  Backups

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-health/v1)

- [ Introduction ](https://spatie.be/docs/laravel-health/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-health/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-health/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-health/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-health/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-health/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-health/v1/about-us)

Usage
-----

- [ Registering your first check ](https://spatie.be/docs/laravel-health/v1/basic-usage/registering-your-first-check)
- [ Creating custom checks ](https://spatie.be/docs/laravel-health/v1/basic-usage/creating-custom-checks)
- [ Manually running checks ](https://spatie.be/docs/laravel-health/v1/basic-usage/manually-running-checks)
- [ Conditionally running or modifying checks ](https://spatie.be/docs/laravel-health/v1/basic-usage/conditionally-running-or-modifying-checks)
- [ Endpoints ](https://spatie.be/docs/laravel-health/v1/basic-usage/endpoints)
- [ Pausing and resuming checks ](https://spatie.be/docs/laravel-health/v1/basic-usage/pausing-and-resuming-checks)
- [ Registering the same check multiple times ](https://spatie.be/docs/laravel-health/v1/basic-usage/registering-the-same-check-multiple-times)
- [ Testing ](https://spatie.be/docs/laravel-health/v1/basic-usage/testing)

Storing results
---------------

- [ General ](https://spatie.be/docs/laravel-health/v1/storing-results/general)
- [ As JSON ](https://spatie.be/docs/laravel-health/v1/storing-results/in-a-json-file)
- [ In the cache ](https://spatie.be/docs/laravel-health/v1/storing-results/in-cache)
- [ In the database ](https://spatie.be/docs/laravel-health/v1/storing-results/in-the-database)
- [ Not storing results ](https://spatie.be/docs/laravel-health/v1/storing-results/not-storing-results)

Configuring notifications
-------------------------

- [ General ](https://spatie.be/docs/laravel-health/v1/configuring-notifications/general)
- [ Via mail ](https://spatie.be/docs/laravel-health/v1/configuring-notifications/via-mail)
- [ Via Slack ](https://spatie.be/docs/laravel-health/v1/configuring-notifications/via-slack)
- [ Via Oh Dear ](https://spatie.be/docs/laravel-health/v1/configuring-notifications/via-oh-dear)

Viewing check results
---------------------

- [ General ](https://spatie.be/docs/laravel-health/v1/viewing-results/general)
- [ On a webpage ](https://spatie.be/docs/laravel-health/v1/viewing-results/on-a-webpage)
- [ On the CLI ](https://spatie.be/docs/laravel-health/v1/viewing-results/on-the-cli)
- [ As JSON ](https://spatie.be/docs/laravel-health/v1/viewing-results/as-json)

Available checks
----------------

- [ Overview ](https://spatie.be/docs/laravel-health/v1/available-checks/overview)
- [ Backups ](https://spatie.be/docs/laravel-health/v1/available-checks/backups)
- [ Application Cache ](https://spatie.be/docs/laravel-health/v1/available-checks/cache)
- [ Cached config, routes, and events ](https://spatie.be/docs/laravel-health/v1/available-checks/cached-config-routes-and-events)
- [ CPU load ](https://spatie.be/docs/laravel-health/v1/available-checks/cpu-load)
- [ DB connection ](https://spatie.be/docs/laravel-health/v1/available-checks/db-connection)
- [ DB connection count ](https://spatie.be/docs/laravel-health/v1/available-checks/db-connection-count)
- [ DB size ](https://spatie.be/docs/laravel-health/v1/available-checks/db-size-check)
- [ DB table size ](https://spatie.be/docs/laravel-health/v1/available-checks/db-table-size-check)
- [ Debug mode ](https://spatie.be/docs/laravel-health/v1/available-checks/debug-mode)
- [ Environment ](https://spatie.be/docs/laravel-health/v1/available-checks/environment)
- [ Flare error count ](https://spatie.be/docs/laravel-health/v1/available-checks/flare-error-count)
- [ Horizon ](https://spatie.be/docs/laravel-health/v1/available-checks/horizon)
- [ Meilisearch ](https://spatie.be/docs/laravel-health/v1/available-checks/meilisearch)
- [ Ping ](https://spatie.be/docs/laravel-health/v1/available-checks/ping)
- [ Queue ](https://spatie.be/docs/laravel-health/v1/available-checks/queue)
- [ Redis ](https://spatie.be/docs/laravel-health/v1/available-checks/redis)
- [ Redis memory usage ](https://spatie.be/docs/laravel-health/v1/available-checks/redis-memory-usage)
- [ Schedule ](https://spatie.be/docs/laravel-health/v1/available-checks/schedule)
- [ Security advisories ](https://spatie.be/docs/laravel-health/v1/available-checks/security-advisories)
- [ Used disk space ](https://spatie.be/docs/laravel-health/v1/available-checks/used-disk-space)

Security
--------

- [ Using Secret Token ](https://spatie.be/docs/laravel-health/v1/security/using-secret-token)

 Backups
=======

###  On this page

1. [ Usage ](#content-usage)

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.

```
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.

```
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.

```
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.

```
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.

```
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\BackupsCheck;

Health::checks([
    BackupsCheck::new()
        ->locatedAt('/path/to/backups/*.zip')
        ->atLeastSizeInMb(20),
]);
```

### Check backups on external filesystems

You can use the `onDisk` method to specify any disk you have configured in Laravel. This is useful when the backups are stored on an external filesystem.

```
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\BackupsCheck;

Health::checks([
    BackupsCheck::new()
        ->onDisk('backups')
        ->locatedAt('backups'),
]);
```

Checking backup files on external filesystems can be slow if you have a lot of backup files.

- You can use the `parseModifiedFormat` method to get the modified date of the file from the name instead of reaching out to the file and read its metadata. This strips out the file folder and file extension and uses the remaining string to parse the date with `Carbon::createFromFormat`.
- You can also limit the size check to only the first and last backup files by using the `onlyCheckSizeOnFirstAndLast` method. Otherwise the check needs to reach out to all files and check the file sizes.

These two things can speed up the check of ~200 files on an S3 bucket from about 30 seconds to about 1 second.

```
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\BackupsCheck;

Health::checks([
    BackupsCheck::new()
        ->onDisk('backups')
        ->parseModifiedFormat('Y-m-d_H-i-s')
        ->atLeastSizeInMb(20)
        ->onlyCheckSizeOnFirstAndLast(),
]);
```

For files that contains more than just the date you can use something like parseModifiedFormat('\\b\\a\\c\\k\\u\\p\_Ymd\_His') which would parse a file with the name similar to `backup_20240101_120000.sql.zip`.
