Skip to content

Commit

Permalink
backup command
Browse files Browse the repository at this point in the history
Resolves #4075
  • Loading branch information
brandonkelly committed Mar 29, 2019
1 parent 5fef15f commit 73e2f7e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Added
- Added the `backup` command, which creates a new database backup. ([#4075](https://github.com/craftcms/cms/issues/4075))
- Added the `queue/retry` command, which can be passed a failed job ID, or `all` to retry all failed jobs. ([#4072](https://github.com/craftcms/cms/issues/4072))
- Added `craft\queue\Queue::retryAll()`.
- Added `craft\services\sections::$autoResaveEntries`, which can be set to `false` from `config/app.php` to prevent Craft from auto-resaving entries after sections and entry types are updated. ([#3482](https://github.com/craftcms/cms/issues/3482))
Expand Down
81 changes: 81 additions & 0 deletions src/console/controllers/BackupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\console\controllers;

use Craft;
use craft\helpers\Console;
use craft\helpers\FileHelper;
use yii\console\Controller;
use yii\console\ExitCode;

/**
* Creates a new database backup
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.1.21
*/
class BackupController extends Controller
{
/**
* @inheritdoc
*/
public $defaultAction = 'db';

/**
* Creates a new database backup
*
* @param string|null The path the database backup should be created at.
* Can be any of the following:
*
* - A full file path
* - A folder path (backup will be saved in there with a dynamically-generated name)
* - A filename (backup will be saved in the working directory with the given name)
* - Blank (backup will be saved to the config/backups/ folder with a dynamically-generated name)
*
* @return int
*/
public function actionDb(string $path = null): int
{
$this->stdout('Backing up the database ... ');
$db = Craft::$app->getDb();

if ($path !== null) {
// Prefix with the working directory if a relative path or no path is given
if (strpos($path, '.') === 0 || strpos(FileHelper::normalizePath($path, '/'), '/') === false) {
$path = getcwd() . DIRECTORY_SEPARATOR . $path;
}

$path = FileHelper::normalizePath($path);

if (is_dir($path)) {
$path .= DIRECTORY_SEPARATOR . basename($db->getBackupFilePath());
} else if (is_file($path)) {
if (!$this->confirm("{$path} already exists. Overwrite?")) {
$this->stdout('Aborting' . PHP_EOL);
return ExitCode::OK;
}
unlink($path);
}
} else {
$path = $db->getBackupFilePath();
}

try {
$db->backupTo($path);
} catch (\Throwable $e) {
Craft::$app->getErrorHandler()->logException($e);
$this->stderr('error: ' . $e->getMessage() . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
$size = Craft::$app->getFormatter()->asShortSize(filesize($path));
$this->stdout("Backup file: {$path} ({$size})" . PHP_EOL);
return ExitCode::OK;
}
}

0 comments on commit 73e2f7e

Please sign in to comment.