demi/backup

Basic Yii2 site backup methods

Installs: 18 435

Dependents: 1

Suggesters: 0

Security: 0

Stars: 20

Watchers: 6

Forks: 5

Open Issues: 4

Type:yii2-extension

1.1.1 2021-08-05 16:26 UTC

This package is auto-updated.

Last update: 2024-11-05 23:37:15 UTC


README

Basic Yii2 site backup methods.
Also pay attention to dropbox-backup.

Installation

Run

composer require "demi/backup" "~1.0"

Configurations

Minimal config

Configure /common/config/main.php or console config:

'components' => [
    'backup' => [
        'class' => 'demi\backup\Component',
        // The directory for storing backups files
        'backupsFolder' => dirname(dirname(__DIR__)) . '/backups', // <project-root>/backups
        // Directories that will be added to backup
        'directories' => [
            'images' => '@frontend/web/images',
            'uploads' => '@backend/uploads',
        ],
    ],
]

Will create backup for:

directories:
/frontend/web/images/*
/backend/uploads/*
database:
Yii::$app->db

Result:

/backups/2015_08_11-05_45_48.tar/
>images.tar
>uploads.tar
>sql/blog.sql.gz

Maximal config

[
'class' => 'demi\backup\Component',

// The directory for storing backups files
'backupsFolder' => dirname(dirname(__DIR__)) . '/backups', // <project-root>/backups
// You can use alias:
'backupsFolder' => '@backend/backups', // <project-root>/backend/backups

// Name template for backup files.
// if string - return date('Y_m_d-H_i_s')
'backupFilename' => 'Y_m_d-H_i_s',
// also can be callable:
'backupFilename' => function (\demi\backup\Component $component) {
    return date('Y_m_d-H_i_s');
},

// Directories that will be added to backup
'directories' => [
    // format: <inner backup filename> => <path/to/dir>
    'images' => '@frontend/web/images',
    'uploads' => '@backend/uploads',
],

// Name of Database component. By default Yii::$app->db.
// If you don't want backup project database
// you can set this param as NULL/FALSE.
'db' => 'db',
// Default DB host value
'defaultDbHost' => 'localhost',
// List of databases connections config.
// If you set $db param, then $databases automatically
// will be extended with params from Yii::$app->$db.
'databases' => [
    // It will generate "/sql/logs_table.sql.gz" with 
    // dump file "logs_table.sql" of database 'logs'.
    // You can set custom 'mysqldump' command for each database,
    // just add 'command' param.
    'logs_table' => [
        'db' => 'logs', // database name. If not set, then will be used key 'logs_table'
        'host' => 'localhost', // connection host
        'username' => 'root', // database username
        'password' => 'BXw2DKyRbz', // user password
        'command' => 'mysqldump --add-drop-table --allow-keywords -q -c -u "{username}" -h "{host}" -p\'{password}\' {db} | gzip -9', // custom `mysqldump` command
    ],
],
// CLI command for creating each database backup.
// If $databases password is empty,
// then will be executed: str_replace('-p\'{password}\'', '', $command);
// it helpful when mysql password is not set.
// You can override this command with you custom params,
// just add them to $databases config.
'mysqldump' => 'mysqldump --add-drop-table --allow-keywords -q -c -u "{username}" -h "{host}" -p\'{password}\' {db} | gzip -9',

// Number of seconds after which the file is considered deprecated and will be deleted.
// To prevent deleting any files you can set this param as NULL/FALSE/0.
'expireTime' => 2592000, // 1 month
],

What's next

You can use this component anywhere.
For example, you can create console command
/console/controllers/ToolsController.php:

<?php
namespace console\controllers;

class ToolsController extends \yii\console\Controller
{
    public function actionBackup()
    {
        /** @var \demi\backup\Component $backup */
        $backup = \Yii::$app->backup;
        
        $file = $backup->create();

        $this->stdout('Backup file created: ' . $file . PHP_EOL, \yii\helpers\Console::FG_GREEN);
    }
}