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.
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:
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.
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
| Part | How |
|---|---|
| The database | Backed 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 |
.env | Whole, APP_KEY included, sealed like everything else |
storage/app | Every 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/schema | A 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.
/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.
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 refuses | Why, and what to do |
|---|---|
No .env | The APP_KEY would not be in the backup. Create one, or back up the database alone as a database surface |
No DB_CONNECTION | Laravel's default changed in version 11, so SafeGrd does not guess. Set it, or give the surface its own database_url |
sqlsrv | SQL Server is not backed up yet |
DB_SOCKET | The 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:
- The database passes its engine's drill: the dump is complete and every table's rows match the manifest.
- Every file reads back and matches its SHA-256.
- The
.envin the snapshot holds anAPP_KEYof the length Laravel's cipher takes, 32 bytes for AES-256 or 16 for AES-128. The drill reports this check asAppKey. - The dump holds the
migrationstable with the rows the backup read, and at least one row for each migration file that had run. The drill reports this check asMigrations. If a migration had not run yet when the backup was taken, the backup's output names it.
If any check fails, the drill fails. The drill does not boot the restored app yet.
Restoring an app
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:
- Copy
.envandstorage/appfrom the restored directory into it. - Point
DB_HOST,DB_DATABASE,DB_USERNAMEandDB_PASSWORDat the restored database. - Run
php artisan config:clear,php artisan storage:linkandphp artisan migrate:status.