dbrisinajumi/dbbackup

Sheduled MySQL database backups

dev-master 2022-04-15 09:32 UTC

This package is auto-updated.

Last update: 2024-11-15 15:25:34 UTC


README

##Install

Add the repository to composer.json

    "repositories": [
    {
      "type": "git",
      "url": "https://github.com/DBRisinajumi/db-backup.git"
    }
  ],

Add to require section

"require": {
   "dbrisinajumi/dbbackup": "dev-master"
},

##Database dumper

Usage via Yii2 console command and .env

Set the DB access variables in .env file

DB_ENV_MYSQL_DATABASE=dbname
DB_ENV_MYSQL_USER=dbname_php
DB_ENV_MYSQL_PASSWORD=secret
BACKUP_DUMP_FILE_NAME_FORMAT=weekday-number

//Optional - set dumper engine to mydumper instead default mysqldump 
BACKUP_DUMP_ENGINE=mydumper

Add the controller to config/console.php

'controllerMap' => [
        'backup' => [
            'class' => 'DbBackup\controllers\yii2\BackupController',
        ],

Example cron command

0 4 * * *  /usr/bin/php [SITE PATH]/yii backup/dump -i=daily >> [SITE PATH]/vendor/dbrisinajumi/dbbackup/runtime/log/db-dump-daily.log 2>&1

Usage via custom script

use DbBackup\Dumper;

// include your own config files with access constants and autoload;
require dirname(__FILE__, 2) . '/config.inc';
require CMS_VENDOR_PATH . 'autoload.php';

// Run the Dumper with necessary params
$dumper = new Dumper(
    [
        'dbUser' => CMS_DBUSER,               // Database user
        'dbPassword' => CMS_DBPASS,           // Database password
        'dbName' => CMS_DBASE,                // Database name
        'dbHost' => CMS_DBSERVER,             // Database host
        'interval' => Dumper::INTERVAL_DAILY, // Interval name (used for dump file and backup folder naming)
        'cronPath' => __FILE__,               // Path for cron (used for generated cron command example)  
        
        // Optional params
        'dumpFileName' => Dumper::FILE_NAME_WEEKDAY_NUMBER, // Set the dump file names to rewritable (weekday numbers like 1.sql, 2.sql) to reduce disk space
        'dumperApp' => parent::DUMPER_MYDUMPER,             // Set mydumper as alternative (may not work correctly yet)
    ]
);
echo $dumper->run();

Upload Backups to remote server

Usage via Yii2 console command and .env

Set the backup server access variables in .env file

BACKUP_DUMP_FILE_NAME_FORMAT = weekday-number
BACKUP_SERVER_HOST=backups.example.com
BACKUP_SERVER_USERNAME=backupuser
BACKUP_SERVER_PASSWORD=secretpassword
BACKUP_SERVER_PATH=my.website.com

Add the controller to config/console.php

'controllerMap' => [
        'backup' => [
            'class' => 'DbBackup\controllers\yii2\BackupController',
        ],

Example cron command

0 4 * * *  /usr/bin/php [SITE PATH]/yii backup/sync -i=daily >> [SITE PATH]/vendor/dbrisinajumi/dbbackup/runtime/log/backup-sync-daily.log 2>&1

Usage via custom script

use DbBackup\Sync;

// include your own config files with access constants and autoload;
require dirname(__FILE__, 2) . '/config.inc';
require CMS_VENDOR_PATH . 'autoload.php';

$sync = new Sync([
    'host' => BACKUP_SERVER_HOST,
    'user' => BACKUP_SERVER_USERNAME,
    'password' => BACKUP_SERVER_PASSWORD,
    'remotePath' => BACKUP_SERVER_PATH,
    'cronPath' => __FILE__,
]);
echo $sync->run();