Taking backups | laravel-backup-go | Spatie       

 SPATIE  

laravel-backup-go
=================

spatie.be/open-source

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

 Version   v1      

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

  Taking backups    
- [ Introduction ](https://spatie.be/docs/laravel-backup-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-backup-go/v1/installation-and-setup)
- [ Requirements ](https://spatie.be/docs/laravel-backup-go/v1/requirements)
- [ High level overview ](https://spatie.be/docs/laravel-backup-go/v1/high-level-overview)
- [ Questions and issues ](https://spatie.be/docs/laravel-backup-go/v1/questions-and-issues)
- [ About us ](https://spatie.be/docs/laravel-backup-go/v1/about-us)
- [ Testing ](https://spatie.be/docs/laravel-backup-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-backup-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-backup-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-backup-go/v1/support-us)

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

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

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

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

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

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

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

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

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

- [ Adding extra files to a backup ](https://spatie.be/docs/laravel-backup-go/v1/advanced-usage/adding-extra-files-to-a-backup)
- [ Backing up a non-Laravel application ](https://spatie.be/docs/laravel-backup-go/v1/advanced-usage/backing-up-a-non-laravel-application)
- [ Binary database dumps with PostgreSQL ](https://spatie.be/docs/laravel-backup-go/v1/advanced-usage/binary-database-dumps-with-postgresql)
- [ Encrypt backup archives ](https://spatie.be/docs/laravel-backup-go/v1/advanced-usage/encrypt-backup-archives)
- [ Isolated mode ](https://spatie.be/docs/laravel-backup-go/v1/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 back up your app by running:

```
laravel backup:run
```

To back up to a specific configured disk, use `--only-to-disk=s3`. Use `--only-db` to dump only databases or `--only-files` to skip database dumps. These two options cannot be combined. Repeat `--db-name=reporting` to select specific configured database connections.

You can set a custom archive filename with `--filename=my-backup.zip`, or append a suffix with `--filename-suffix=extra`. You can repeat `--exclude=storage/logs` to exclude additional paths or globs and use `--destination-path=custom/path` to override the backup directory. Paths must stay inside the destination disk. Filenames must be basenames.

Use `--disable-notifications` to suppress notifications for one run. Lifecycle events still fire. `--timeout=600` cancels the operation after 600 seconds. `--tries=3` overrides the configured number of attempts. Interrupting the command cancels the context and removes temporary files.

Be very careful with `--only-db` and `--only-files`. The monitor does not distinguish partial backups from full backups. You may not be able to recover from a partial backup.

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

### Determining the content of the backup

```
backup.New(func(cfg *backup.Config) {
    cfg.Files.Include = []string{"public", "storage/app"}
    cfg.Files.Exclude = []string{"storage/app/cache", "storage/app/*.tmp"}
    cfg.Files.RelativePath = "."
    cfg.Files.FollowLinks = false
    cfg.Files.IgnoreUnreadableDirectories = false
    cfg.Databases = []string{"default", "reporting"}
    cfg.Disks = []string{"local", "s3"}
})
```

The selected files and database dumps are zipped. The default destination is `<Name>/<2006-01-02-15-04-05>.zip`, using Go's timestamp layout and Laravel's clock. Dotfiles and empty subdirectories are included. Missing include paths are skipped. Symlinks are skipped unless `FollowLinks` is true. Link cycles fail rather than looping forever.

Set `RelativePath` to the application's base directory to make ZIP entry names relative to it. Otherwise entries use their full path without a leading slash or Windows volume. Database dumps always live in `db-dumps` inside the ZIP. Duplicate entry names fail rather than silently overwriting a file.

Set `cfg.Archive.Store = true` to disable ZIP compression, or use `cfg.Archive.CompressionLevel` from 0 to 9 for plain Deflate. Set `VerifyBackup` to read every archive entry and verify its integrity before copying it to destinations.

### Customizing database dumps

```
cfg.Dump.Configure = func(name string, dumper *dbdumper.Dumper) error {
    dumper.ExcludeTables = []string{"sessions"}
    dumper.SingleTransaction = true
    return nil
}
cfg.Dump.FilenameBase = backup.ConnectionFilename
cfg.Dump.TimestampLayout = "2006-01-02"
cfg.Dump.Extension = "sql"
cfg.Dump.Compressor = dbdumper.GzipCompressor{}
```

Import `dbdumper "github.com/spatie/db-dumper-go"`. All DB Dumper settings remain available. `Configure` runs on each fresh dumper before the dump starts. For a completely custom driver, supply `Config.ResolveDatabase`. It returns a `Database` containing a safe `Filename` and a `Dump(context.Context, string) error` function.

Connections come from Laravel's final database configuration. The first `ReadHosts` entry is used when replicas are configured. Host-only entries inherit the base URL's credentials, database and port. Otherwise the writer URL is used. SQLite paths are relative to the application base path, and in-memory databases cannot be backed up by an external process.

Dump filenames use `<driver>-<database>.sql` by default, or `.archive` for MongoDB. SQLite uses `<driver>-<connection>-database.sql`. `ConnectionFilename` uses the connection name instead of the database name. A timestamp and compressor extension are appended when configured. Duplicate or unsafe filenames fail before dumping.

### Running backups with a specific configuration

PHP's alternate configuration files become named typed profiles:

```
cfg.Profiles = map[string]func(*backup.Config){
    "database": func(cfg *backup.Config) {
        cfg.Files.Include = []string{}
        cfg.Databases = []string{"default"}
        cfg.Name = "database"
    },
}
```

```
laravel backup:run --config=database
laravel backup:clean --config=database
```

The profile applies to the whole operation, including cleanup, notification routes, storage, health checks and dump settings. It does not modify the default profile.

### Determining the destination of the backup

Set `cfg.Disks` to one or more named Laravel disks. Local, S3, S3-compatible and Laravel Cloud disks use the framework's storage drivers. `Config.ResolveDisk` can supply a custom `backup.Disk` implementation. Driver-specific options belong in that disk implementation rather than an untyped Flysystem options array.

By default a destination failure stops the backup. With `ContinueOnFailure = true`, the other destinations are tried and failed targets emit failure events. The operation succeeds only if at least one destination accepts the archive. Temporary files are removed on success, failure and cancellation.

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

Read the section on [notifications](../sending-notifications/overview).

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
