This package allows you to download, export and import your application's database backups.
Important
This package will not work if you have disabled "proc_open" in your PHP configuration.
- Store your local database in a file
- Non-productive developer machines can download the live server database
- A central backup server can collect backups from multiple live servers
- Download and optionally import databases from a server
- Import existing database files
- Export the local database to a file
- User authentication through Laravel Sanctum tokens
- Transport encryption using Sodium
- Protector only supports MySQL, MariaDB and PostgreSQL databases at the moment.
- Source and destination databases are currently not checked. Please make sure you run the same software in the same versions to prevent issues.
- Because of different dump formats, pulling dumps from MariaDB and restoring them to MySQL will not work. The same, of course, applies to cross-database operations between MySQL and PostgreSQL.
- Enabling Laravel Telescope will prevent remote files from being downloaded, as it opens and discards the HTTP stream!
To save a copy of your local database, run
php artisan protector:exportBy default, dumps are stored in storage/app/protector on your default project disk.
You can configure the target disk, filename, etc. by publishing the protector config file to your project
artisan vendor:publish --tag=protector.configRun the following command for an interactive shell
php artisan protector:importTo download and import the server database in one go, run
php artisan protector:import --remoteWhen used with other options, remote will serve as fallback behavior.
To import a specific database file that you downloaded earlier, run
php artisan protector:import --file=<absolute path to database file>Or just reference the database file name in the protector folder (default folder is storage/app/protector).
php artisan protector:import --dump=<name of database file>To import the latest existing database file, run
php artisan protector:import --latestIf you want to run migrations after the import of the database file, run
php artisan protector:import --migrateFor automation, also consider the flush option to clean up older database files, and the force option to bypass user interaction.
php artisan protector:import --remote --migrate --flush --forceTo learn more about import options, run
php artisan protector:import --helpFind below three common scenarios of usage. These are not mutually exclusive.
If you only want to store a copy of your local database to a disk, the setup is pretty straightforward.
Install the package via composer.
composer require cybex/laravel-protectorYou can optionally publish the protector config to set the following options
fileName: the file name of the database dumpbaseDirectory: where files are being storeddiskName: a dedicated Laravel disk defined in config/filesystems.php. These can point to a specific local folder or a cloud file bucket like AWS S3
artisan vendor:publish --tag=protector.configYou can now use the artisan command to write a backup to the protector storage folder.
php artisan protector:exportBy default, the file will be stored in storage/protector and have a timestamp in the name. You can also specify the filename.
You could also automate this by
- installing a cronjob on linux
- running it when you deploy to your server
- creating a Laravel Job and queueing it
php artisan protector:export --file=storage/database.sqlThis package can run on both servers and client machines of the same software repository. You set up authorized developers on the server and give them the key for their local machine.
Install the package via composer.
composer require cybex/laravel-protectorIn your User model class, add the following trait.
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
...
}Publish the protector database migration and optionally modify it to work with your project.
php artisan vendor:publish --tag=protector.migrationsStarting with Laravel 11, you may need to publish the Laravel Sanctum migration, to make the personal_access_tokens table available.
php artisan vendor:publish --tag=sanctum-migrationsRun the migrations on the client and server repository.
php artisan migrateYou can optionally publish the protector config to set options regarding the storage, access and transmission of the files.
php artisan vendor:publish --tag=protector.configRun the following command to receive
- the public key to give to your server admin
- the private key to save in your .env file
php artisan protector:keysImportant
Do not give your private key to anyone and keep it protected at all times!
Your server admin will then give you the token and dump endpoint URL to save in your .env file.
PROTECTOR_CLIENT_AUTH_TOKEN=
PROTECTOR_CLIENT_DUMP_ENDPOINT_URL=See Usage on how to import the remote database.
Note
Downloaded database dump files are stored unencrypted.
Make sure that the server is accessible to the client machine via HTTPS.
When one of your developers gives you their public key, you can authorize them with:
php artisan protector:token --publicKey=<public key> <user id>You will receive the token and dump endpoint URL to give back to the developer, who has to save them in their .env file.
The developer can then download and import the server database on their own.
You can develop a custom client that can access and store remote server backups. The servers can be different Laravel projects that have the protector package installed.
See the previous chapter on how to give your backup client access to all servers. The backup client will need an according user on each target server.
- All the backup users on the target servers will have the same public key from the client
- For each target server, the client will store the according url and token
See cybex-gmbh/collector for an example implementation.
The protector.php config file sets initial settings for the Protector instance.
Additional settings can be configured on the Protector instance. For all available options, take a look at
the HasConfiguration trait.
For example, to configure a specific auth token and dump endpoint URL:
Protector::withAuthToken($authToken)->withDumpEndpointUrl($dumpEndpointUrl);Customize the metadata appended to a dump by adding providers to the dump.metadata.providers array in your config/protector.php file:
'providers' => [
\Cybex\Protector\Classes\Metadata\Providers\EnvMetadataProvider::class,
\Cybex\Protector\Classes\Metadata\Providers\GitMetadataProvider::class,
\Path\To\Your\CustomMetadataProvider::class,
],Available metadata providers:
DatabaseMetadataProvider: Will always be appended. Adds general information about the dump, such as the database connection and dumped at date.ProtectorMetadataProvider: Adds information about the settings set on theProtectorinstance.EnvMetadataProvider: Adds information based on an .env value. The default .env key used for this isPROTECTOR_METADATA.GitMetadataProvider: Adds information about the Git repository, such as the current branch and revision.JsonMetadataProvider: Adds information from a JSON file. The default file path used for this isprotector_metadata.json.
Note
You can create your own metadata providers by implementing the Cybex\Protector\Contracts\MetadataProvider interface.
Duplicate provider keys will be merged in the final metadata array, so choose a unique key.
Tip
An example of using the JsonMetadataProvider would be to add custom metadata from a CI/CD pipeline.
For example, in a GitHub Actions workflow, you could add a step that writes Git information to protector_metadata.json
- name: Protector Metadata
shell: bash
run: >
jq -n \
--arg repo ${{ github.repository }} \
--arg branch ${{ github.ref_name }} \
--arg revision ${{ github.sha }} \
--arg buildDate "$(date --iso-8601=seconds --utc)" \
'{gitRepo: $repo, gitBranch: $branch, gitRevision: $revision, buildDate: $buildDate}' > protector_metadata.jsonThere is an example app with the Laravel Protector package installed.
The file structure in the container is as follows:
- /var/www/html: example app
- /var/www/package: Protector package
./sail up -d./sail shellcomposer installNote
We disable composer security checking for this package, as vulnerabilities would block the development. The project requiring our package should be responsible for evaluating possible vulnerabilities. For more information, see the composer documentation.
Specific to the example app, for demo data:
php artisan migrate --seedRun tests on the MySQL database:
composer testRun tests on the PostgreSQL database:
composer test-postgresPlease see CONTRIBUTING for details.
If you discover any security-related issues, please email webdevelopment@cybex-online.com instead of using the issue tracker.
- Web Development team at Cybex GmbH - cybex-online.com
- Gael Connan
- Jörn Heusinger
- Fabian Holy
- Oliver Matla
- Marco Szulik
- All Contributors
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.