PHPackages                             amoracr/yii2-backup - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. amoracr/yii2-backup

ActiveYii2-extension[Debugging &amp; Profiling](/categories/debugging)

amoracr/yii2-backup
===================

Component for creating backups of Yii2 sites.

1.4.0(4y ago)45.9k8[3 issues](https://github.com/amoracr/yii2-backup/issues)[1 PRs](https://github.com/amoracr/yii2-backup/pulls)MITPHPPHP ^5.1 || ^7.0 || ^8.0

Since Jul 11Pushed 1y ago2 watchersCompare

[ Source](https://github.com/amoracr/yii2-backup)[ Packagist](https://packagist.org/packages/amoracr/yii2-backup)[ Docs](https://github.com/amoracr/yii2-backup)[ Fund](https://paypal.me/adelfunscr)[ RSS](/packages/amoracr-yii2-backup/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (21)Used By (0)

Yii2-backup
===========

[](#yii2-backup)

Backup and Restore functionality for Yii2 applications.

This extension is based on:

- [yii2-backup](https://github.com/demisang/yii2-backup) by [Ivan Orlov](https://github.com/demisang),
- [yii2-backup](https://github.com/elleracompany/yii2-backup) by [Ellera](https://github.com/elleracompany).
- [php-sqlite-dump](https://github.com/ephestione/php-sqlite-dump) by [ephestione](https://github.com/ephestione)
- [extract-folder-content-using-ziparchive](https://stackoverflow.com/questions/8102379/extract-folder-content-using-ziparchive)
- [loading-sql-files-from-within-php](https://stackoverflow.com/questions/147821/loading-sql-files-from-within-php)

I combined those sources and made a more powerful and easier to use extension.

Supported databases:

- MySQL
- MariaDB (via MySQL driver)
- SQLite
- PostgreSQL

Supported compression methods:

- Bzip2
- Gzip
- Zip

By default the backup files is a tar file with sql dumps and folders.

Current limitations:

- Requires a linux system.
- Currently only MySQL on localhost is supported.
- Currently only MariaDB on localhost is supported.
- Currently only PostgreSQL on localhost is supported.

Getting started
---------------

[](#getting-started)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist amoracr/yii2-backup "*"

```

or add

```
"amoracr/yii2-backup": "*"

```

to the require section of your `composer.json` file.

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

[](#configuration)

Once the extension is installed, add it in your config file:

Basic `config/console.php`

Advanced `console/config/main.php`

Minimal Config

```
'components' => [
    ...
    'backup' => [
            'class' => 'amoracr\backup\Backup',
            // Path for storing the backups
            'backupDir' => '@app/backups',
            // Directories that will be added to backup
            'directories' => [
                // format:  =>
                'images' => '@app/web/images',
                'uploads' => '@app/web/uploads',
            ],
        ],
]
```

#### Will create backup for:

[](#will-create-backup-for)

**directories:**
*/web/images/\**
*/web/uploads/\**
**database:**
*Yii::$app-&gt;db*

#### Result:

[](#result)

**/backups/2020-06-29T182436-0600\_backup.tar/**
&gt;images/
&gt;uploads/
&gt;sql/db.sql

Advanced Config

```
'components' => [
    ...
    'backup' => [
            'class' => 'amoracr\backup\Backup',
            // Name for the backup
            'fileName' => 'myapp-backup',
            // Maximum age in seconds for a valid backup.
            // Older files are considered deprecated and can be deleted.
            // Minimum age is 86400 secs (1 day).
            // Maximum age is 31536000 secs (1 year).
            'expireTime'=> 86400 * 3,
            // Path for storing the backups
            'backupDir' => '@app/backups',
            // Database components to backup
            'databases' => ['db', 'db1'],
            // Compression method to apply to backup file.
            // Available options:
            // 'none' or 'tar' for tar files, backup file is not compressed.
            // 'bzip2' for tar.bz2 files, backup file is compressed with Bzip2 compression.
            // 'gzip' for tar.gz files, backup file is compressed with Gzip compression.
            // 'zip' for zip files, backup file is compressed with Zip compression.
            'compression' => 'zip',
            // Directories that will be added to backup
            'directories' => [
                // format:  =>
                'images' => '@app/web/images',
                'uploads' => '@app/web/uploads',
                // format:  => array('path'=>,'regex'=>)
                // Key 'path' for setting the directory to include
                // Key 'regex' for setting the regular expression for selecting the files to include
                'pdf' => [
                   'path' => '@app/web/documents',
                   'regex' => '/\.pdf$/',
                ],
            ],
            // Files to avoid in backup accross all directories
            'skipFiles' => [
                '.gitignore',
            ]
        ],
]
```

#### Result:

[](#result-1)

**/backups/2020-06-29T182436-0600\_myapp-backup.zip/**
&gt;images/
&gt;uploads/
&gt;pdf/
&gt;sql/db.sql
&gt;sql/db1.sql

Usage
-----

[](#usage)

You can use this component in a console command.

**/console/controllers/BackupController.php:/**

```
