Chapter 11. Framework integrations

Laravel

The laravel-phpbu package integrates phpbu into the laravel framework so you can use the artisan command to execute your backups and a laravel style php configuration to set them up.

Requirements

  • PHP 5.5

  • Laravel 5.*

  • phpbu 3.2.*

Installation

Use composer to install the package.

composer require "phpbu/phpbu-laravel"

Add the package ServiceProvider to your config/app.php configuration file.

'providers' => [
    /*
    * phpbu Backup Service Providers...
    */
    phpbu\Laravel\ServiceProvider::class,
];

Finally use the laravel artisan command to create a configuration skeleton by publishing the package.

php artisan vendor:publish --provider="phpbu\Laravel\ServiceProvider"

After publishing the ServiceProvider a phpbu.php configuration file is created in your config directory.

Configuration

There are two ways of configuring your backup.

  1. Use the created phpbu.php configuration file

  2. Use a standard phpbu.xml or phpbu.json configuration file

The main difference between those two options is the implemented feature set.

While the phpbu configuration files give you full access to all features of phpbu the laravel configuration file currently does not support all features of phpbu f.e. encryption.

Using the laravel configuration

The main concept of configuring backups stays the same. A backup consists of a source and a target. The source represents the data you want to backup. The target represents the location where you want to store your backup. You can add checks, cleanups and syncs to a backup to validate, cleanup or sync your backup to another location.

In the laravel configuration file you can specify two types of backups, directories and databases (MySQL, PostgreSQL).

Directories

Define the directory you want to backup and the target location where to store your backup.

    'directories' => [
        [
            'source' => [
                'path'    => storage_path('app'),
                'options' => [],
            ],
            'target' => [
                'dirname'     => storage_path('/backup/app'),
                'filename'    => 'app-%Y%m%d-%H%i.tar',
                'compression' => 'bzip2',
            ]
        ]
    ],

Databases

Just define the laravel database connection configured in your databases.php configuration you want to backup and you are done. You can overwrite every setting defined with the specific option f.e. username or password in case you have to use different credentials to backup your database.

You can use every option the mysqldump source supports f.e. to exclude some tables. Just use the option-name as index and the option-value as value.

    'databases' => [<emphasise>
        [
            'source' => [
                'connection' => 'mysql',
                'options'    => []
            ],
            'target' => [
                'dirname'     => storage_path('backup/db'),
                'filename'    => 'dump-%Y%m%d-%H%i.sql',
                'compression' => 'bzip2',
            ]
        ],
    ],

Adding Check, Sync and Cleanup

Add check, sync and cleanup definitions to your backup configuration.

            'check' => [
                'type'  => 'SizeMin',
                'value' => '10B',
            ],
            'sync' => [
              'filesystem' => 's3',
              'path'       => '/backups/db'
            ],
            'cleanup' => [
                'type'    => 'quantity',
                'options' => [
                    'amount' => '20'
                ]
            ]

For a list of available checks and cleanups check the corresponding documentation sections.

To sync your backups you can use every filesystem defined in your laravel filesystems.php configuration file.

Using a phpbu configuration file

If you want to use a phpbu configuration file your laravel phpbu.php configuration should look something like this.

<?php
return [
    /*
    |--------------------------------------------------
    | phpbu configuration
    |--------------------------------------------------
    |
    | Path to a phpbu configuration xml or json file.
    */

    'phpbu' => base_path('phpbu.xml'),
];

For this to work you have to setup a valid phpbu.xml configuration file in your project root.

Remember that all paths in a phpbu configuration have to to absolute or relative to the specified phpbu configuration file.

Usage

To execute the backup use the artisan command line tool.

php artisan phpbu:backup

To execute a dry-run without making any changes.

php artisan phpbu:backup --phpbu-simulate
Please open a ticket on GitHub to suggest improvements to this page. Thanks!