Taking backups | laravel-backup | Spatie

 SPATIE

  Laravel Backup
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-backup](https://spatie.be/docs/laravel-backup/v10)  Taking-backups  Taking backups

 Version   v10   v9   v8   v7   v6   v5   v4   v3

 Other versions for crawler [v10](https://spatie.be/docs/laravel-backup/v10) [v9](https://spatie.be/docs/laravel-backup/v9) [v8](https://spatie.be/docs/laravel-backup/v8) [v7](https://spatie.be/docs/laravel-backup/v7) [v6](https://spatie.be/docs/laravel-backup/v6) [v5](https://spatie.be/docs/laravel-backup/v5) [v4](https://spatie.be/docs/laravel-backup/v4) [v3](https://spatie.be/docs/laravel-backup/v3)

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

Taking Backups
--------------

- [ Taking backups ](https://spatie.be/docs/laravel-backup/v10/taking-backups/overview)
- [ Events ](https://spatie.be/docs/laravel-backup/v10/taking-backups/events)

Cleaning up old backups
-----------------------

- [ Cleaning up old backups ](https://spatie.be/docs/laravel-backup/v10/cleaning-up-old-backups/overview)
- [ Events ](https://spatie.be/docs/laravel-backup/v10/cleaning-up-old-backups/events)

Monitoring the health of all backups
------------------------------------

- [ Monitoring the health of all backups ](https://spatie.be/docs/laravel-backup/v10/monitoring-the-health-of-all-backups/overview)
- [ Creating your custom health check ](https://spatie.be/docs/laravel-backup/v10/monitoring-the-health-of-all-backups/creating-your-custom-health-check)
- [ Events ](https://spatie.be/docs/laravel-backup/v10/monitoring-the-health-of-all-backups/events)

Sending notifications
---------------------

- [ Sending notifications ](https://spatie.be/docs/laravel-backup/v10/sending-notifications/overview)
- [ Adding extra notification channels ](https://spatie.be/docs/laravel-backup/v10/sending-notifications/adding-extra-notification-channels)
- [ Customizing the notifiable ](https://spatie.be/docs/laravel-backup/v10/sending-notifications/customizing-the-notifiable)

Advanced Usage
--------------

- [ Adding extra files to a backup ](https://spatie.be/docs/laravel-backup/v10/advanced-usage/adding-extra-files-to-a-backup)
- [ Backing up a non-laravel application ](https://spatie.be/docs/laravel-backup/v10/advanced-usage/backing-up-a-non-laravel-application)
- [ Binary database dumps with PostgreSQL ](https://spatie.be/docs/laravel-backup/v10/advanced-usage/binary-database-dumps-with-postgresql)
- [ Encrypt backup archives ](https://spatie.be/docs/laravel-backup/v10/advanced-usage/encrypt-backup-archives)
- [ Isolated mode ](https://spatie.be/docs/laravel-backup/v10/advanced-usage/isolated-mode)

 Taking backups
==============

###  On this page

1. [ Configuration ](#content-configuration)
2. [ Get notifications when a backup goes wrong ](#content-get-notifications-when-a-backup-goes-wrong)

You can backup your app by running:

```
php artisan backup:run
```

If you want to backup to a specific disk instead of all disks, run:

```
php artisan backup:run --only-to-disk=name-of-your-disk
```

If you only need to backup the db, run:

```
php artisan backup:run --only-db
```

If you only need to backup the files, and want to skip dumping the databases, run:

```
php artisan backup:run --only-files
```

You can set a custom filename for the backup zip:

```
php artisan backup:run --filename=my-backup.zip
```

Or append a suffix to the default filename:

```
php artisan backup:run --filename-suffix=-extra
```

You can exclude additional directories or files at runtime:

```
php artisan backup:run --exclude=storage/logs --exclude=storage/debugbar
```

You can override the backup destination path:

```
php artisan backup:run --destination-path=custom/path
```

To disable notifications for a specific run:

```
php artisan backup:run --disable-notifications
```

You can set a PHP timeout (in seconds) for the backup process:

```
php artisan backup:run --timeout=600
```

You can configure the number of retry attempts:

```
php artisan backup:run --tries=3
```

To run a backup with a specific configuration file:

```
php artisan backup:run --config=backup_database
```

Be very careful with `--only-db` and `--only-files`. When monitoring backups, the package **does not** make a distinction between full backups and a backup which only contains files or databases. It may be the case that you will not be able to recover from a partial backup.

Configuration
-----------------------------------------------------------------------------------------------

### Determining the content of the backup

This section of the configuration determines which files and databases will be backed up. Most options should be self explanatory.

```
'backup' => [
    /*
     * The name of this application. You can use this name to monitor
     * the backups.
     */
    'name' => env('APP_NAME', 'laravel-backup'),

    'source' => [
        'files' => [
            /*
             * The list of directories and files that will be included in the backup.
             */
            'include' => [
                base_path(),
            ],

            /*
             * These directories and files will be excluded from the backup.
             *
             * Directories used by the backup process will automatically be excluded.
             */
            'exclude' => [
                base_path('vendor'),
                base_path('node_modules'),
                storage_path('framework'),
            ],

            /*
             * Determines if symlinks should be followed.
             */
            'follow_links' => false,

            /*
             * Determines if it should avoid unreadable folders.
             */
            'ignore_unreadable_directories' => false,

            /*
             * This path is used to make directories in resulting zip-file relative
             * Set to `null` to include complete absolute path
             * Example: base_path()
             */
            'relative_path' => null,
        ],

        /*
         * The names of the connections to the databases that should be backed up
         * MySQL, PostgreSQL, SQLite and Mongo databases are supported.
         */
        'databases' => [
            env('DB_CONNECTION', 'mysql'),
        ],
    ],

    'destination' => [
        /*
         * The disk names on which the backups will be stored.
         */
        'disks' => [
            'local',
        ],
    ],
]
```

The specified databases will be dumped and, together with the selected files, zipped. The zip file will be named`/.zip`.

The more files you need to backup, the bigger the zip will become. Make sure there's enough free space on your disk to create the zip file. After the source zip file has been copied to all destinations, it will be deleted.

### Running backups with a specific configuration

If you want to back up different areas of your Laravel application separately – for example with different schedules, database connections, filesystem disks, or cleanup settings – you can create custom backup configuration files.

#### Example

Additional config files placed in the `config/` directory:

- `config/backup_database.php`
- `config/backup_invoices.php`
- `config/backup_uploads.php`

You can then run backups and cleanup commands individually:

```
php artisan backup:run --config=backup_database
php artisan backup:clean --config=backup_database

php artisan backup:run --config=backup_invoices
php artisan backup:clean --config=backup_invoices

php artisan backup:run --config=backup_uploads
php artisan backup:clean --config=backup_uploads
```

This allows full flexibility in scheduling, retention, and target destinations for each backup scope.

### Determining the destination of the backup

The zipped backup can be copied to one or more filesystems. This section of the configuration is where you specify those destination filesystems.

```
    'destination' => [

       /*
        * The disk names on which the backups will be stored.
        */
        'disks' => [
            'local'
        ],
    ],
```

The default value of `config('backup.destination.disks')` is an array with only one key: `local`. Beware! If you only use the local disk to take backups and that disk crashes you will have nothing left but tears. Having a backup is not the same as having a backup strategy!

We highly recommend that you configure some extra disks in `app/config/filesystems.php` and add them as destination filesystems for the backup. Those disks should use external servers or services (such as S3 or Dropbox).

If you need to pass extra options to the underlying Flysystem driver of the disk, you can do so by adding a `backup_options` array to the configuration of that disk. In most cases this is not needed.

```
// in config filesystems.php

return [

    // ..

    'disks' => [
        's3' => [
            'driver' => 's3',
            // ...
            'backup_options' => [
               // add extra options here
            ],
        ],
    ],
];
```

If something goes wrong copying the zip file to one filesystem, the package will still try to copy zipped backup to all other configured filesystems.

Get notifications when a backup goes wrong
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can receive a notification when a backup goes wrong. Read the section on [notifications](/docs/laravel-backup/v10/sending-notifications/overview) to find out more.
