Docs / Surfaces / Laravel

Laravel

You back up a Laravel app as one surface. Its database, its .env and storage/app go into one snapshot and get one drill result. The CLI runs on the app's host and reads the connection from .env at every backup, so you change nothing when the password rotates.

Set it up

From the app, with Composer

Use this on a host where the app's user has no root, such as Forge, Ploi or shared hosting. It needs Laravel 10 or later and PHP 8.1 or later.

composer require safegrd/laravel
php artisan safegrd:install

safegrd:install downloads the CLI, checks it against the release's checksums.txt and prints a code for you to approve in the console. Then it adds the app and takes the first backup. From then on, Laravel's scheduler runs php artisan safegrd:run every five minutes. That command backs up when the app is due and drills when a drill is due, so the host needs the scheduler's cron entry.

The package gives the CLI the connection as Laravel resolved it, so a password set in the host's environment instead of .env works too. The package's options are in its README.

Run safegrd:install as the user the scheduler runs as, which is the owner of storage/. To check the setup, run php artisan safegrd:doctor. A failed backup exits non-zero, writes an error to the app's log and dispatches a SafeGrd\Laravel\Events\BackupFailed event.

On the host, with the CLI

Use this on a server you manage. Enroll first, so the host's key is made with the custody you choose. Then add the app, running as the user the app runs as:

curl -fsSL https://safegrd.dev/install.sh | sh
safegrd init --laravel /var/www/shop
safegrd doctor
sudo safegrd daemon install

init --laravel checks the app, tells you which connection it found, and adds the surface to the config. The surface id is APP_NAME as a slug, unless you set one with --surface-id. The daemon then backs up and drills the surface on its schedule. If you would rather use Laravel's scheduler than a service, init prints the line to add to routes/console.php.

# what init --laravel writes to ~/.safegrd/config.yaml
surfaces:
  - id: shop
    type: laravel
    name: Shop
    roots: [/var/www/shop]

roots is the app's root directory, where artisan and .env are. Run the CLI as a user that can read .env and storage/app. That is usually the user PHP runs as: forge on a Forge server, www-data on Debian. Laravel Vapor and Laravel Cloud have no server for the CLI to run on.

What is captured

PartHow
The databaseBacked up first, the same way as that engine's own surface: mysqldump for MySQL and MariaDB, pg_dump and binary COPY for PostgreSQL, a page copy for SQLite
.envWhole, APP_KEY included, sealed like everything else
storage/appEvery file with its mode and time. A file that did not change since the last backup is not read or uploaded again
database/migrations and database/schemaA few kilobytes. The drill compares them with the migrations table

The backup leaves out vendor, node_modules, storage/framework, storage/logs, bootstrap/cache and the rest of the code, which lives in git. Inside storage/app it skips backup-temp, livewire-tmp and the zip archives spatie/laravel-backup writes there. To leave out more, add paths from the app's root to the surface's excludes, such as storage/app/exports.

One Laravel app root split in two. In the snapshot: the database, dumped first; .env with APP_KEY; storage/app; database/migrations; database/schema. Left out: vendor and node_modules, storage/framework, storage/logs, bootstrap/cache, and the rest of the code, which lives in git. /var/www/shop In the snapshot the database, dumped first .env, with APP_KEY storage/app database/migrations database/schema Left out vendor, node_modules storage/framework storage/logs bootstrap/cache the rest of the code (git)
What one backup of /var/www/shop takes. Restore the left-out half from git and Composer.

Why the .env is in the backup

Laravel encrypts cookies, signed URLs, encrypted casts and anything passed to Crypt with APP_KEY. If you restore the database without the key it was written with, you have those columns and nothing to read them with.

The .env is encrypted on the host with the host's key before it is uploaded. With a SafeGrd-managed key, SafeGrd keeps that key sealed and releases it only to your enrolled hosts. With a customer-managed key, only you can decrypt the backup, so keep a copy of the key somewhere safe.

Where the connection comes from

The backup reads the connection from .env the way Laravel's own config does. It uses DB_URL (or DATABASE_URL) when it is set. Otherwise it uses DB_CONNECTION with DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME and DB_PASSWORD. The mysql, mariadb, pgsql and sqlite connections are backed up. A SQLite path is relative to the app's root. An unset DB_DATABASE means database/database.sqlite.

The order the backup finds its database in. If the surface has its own database_url, it is used. Otherwise, if .env sets DB_URL or DATABASE_URL, that is used. Otherwise, if .env sets DB_CONNECTION, the URL is built from DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME and DB_PASSWORD. Otherwise the backup is refused. Surface has database_url? yes Use it no .env sets DB_URL? yes Use it no .env sets DB_CONNECTION? yes Build it from DB_HOST... no Refused set one of the three
Checked in this order at every backup. DB_URL includes DATABASE_URL.

To back up as a different role from the app's, such as a read-only one, or with a connection set outside .env, give the surface its own database_url or credential block. init --laravel --database-url env:VAR writes one. The .env is still backed up whole. If your migrations table has another name, set it in migrations_table.

The password read from .env is never written to the config or the state directory, and never sent to SafeGrd.

The backup refusesWhy, and what to do
No .envThe APP_KEY would not be in the backup. Create one, or back up the database alone as a database surface
No DB_CONNECTIONLaravel's default changed in version 11, so SafeGrd does not guess. Set it, or give the surface its own database_url
sqlsrvSQL Server is not backed up yet
DB_SOCKETThe CLI connects over TCP. Set database_url on the surface

With FILESYSTEM_DISK=s3, the uploads are in a bucket instead of storage/app. The backup still takes the database and the .env, and prints a warning. Back up the bucket as a files surface with an object_store block.

What a Fire Drill checks

The drill runs on every plan, in memory. A SQLite database is opened from a temporary copy, which is deleted afterwards. The drill checks that:

If any check fails, the drill fails. The drill does not boot the restored app yet.

Restoring an app

# TARGET_URL names an empty database: mysql://, postgres:// or sqlite:///path/new.sqlite
safegrd restore --snapshot <id> --target env:TARGET_URL --target-dir /tmp/shop-restored

The restore loads the database into an empty database of the same engine. It writes the .env, storage/app and the migration files to an empty directory. Then check out the app at the version the restore prints, and in that checkout:

  1. Copy .env and storage/app from the restored directory into it.
  2. Point DB_HOST, DB_DATABASE, DB_USERNAME and DB_PASSWORD at the restored database.
  3. Run php artisan config:clear, php artisan storage:link and php artisan migrate:status.