PHPackages                             chenhakim/migrate-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. [Database &amp; ORM](/categories/database)
4. /
5. chenhakim/migrate-backup

ActiveYii2-extension[Database &amp; ORM](/categories/database)

chenhakim/migrate-backup
========================

yii2命令行中使用migration备份和还原数据库

0129PHP

Since May 8Pushed 9y agoCompare

[ Source](https://github.com/chenhakim/migrate-backup)[ Packagist](https://packagist.org/packages/chenhakim/migrate-backup)[ RSS](/packages/chenhakim-migrate-backup/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependenciesVersions (1)Used By (0)

migrate-backup
==============

[](#migrate-backup)

yii2 migration 备份数据库数据与恢复

### 原作者有话说：

[](#原作者有话说)

yii2使用migration备份和还原数据库
=======================

[](#yii2使用migration备份和还原数据库)

yii2使用migration备份和还原数据库，最初只想做一个在命令行中备份的功能，后来将类重组了，增加了其他扩展使用方法。

安装 Installation
---------------

[](#安装-installation)

安装此扩展的首选方式是通过 [composer](http://getcomposer.org/download/).

运行

```
composer require --prefer-dist chenhakim/migrate-backup "*"

```

或者添加

```
"chenhakim/migrate-backup": "*"

```

到 `composer.json` 文件的对应地方.

命令行中备份数据表：
----------

[](#命令行中备份数据表)

在`console\config\main.php`中添加 :

```
'controllerMap' => [
    'migrates' => [
        'class' => 'chenhakim\migration\ConsoleController',
    ],
],
```

在命令行中使用方式：

```
php ./yii migrate/backup all #备份全部表
php ./yii migrate/backup table1,table2,table3... #备份多张表
php ./yii migrate/backup table1 #备份一张表

php ./yii migrate/up #恢复全部表

```

在后台中备份数据表：
----------

[](#在后台中备份数据表)

在后台的控制器中，例如`PublicController`中加入下面的代码：

```
public function actions()
{
    return [
        'backup' => [
            'class' => 'chenhakim\migration\WebAction',
            'returnFormat' => 'json',
            'migrationPath' => '@console/migrations'
        ]
    ];
}
```

在后台中发送一个ajax请求到`/admin/public/backup?tables=yii2_ad,yii2_admin`即可。

其他使用方法：
-------

[](#其他使用方法)

对于想做更多扩展的朋友，可以直接继承`chenhakim\migration\components\MigrateCreate`

或者使用一下代码：

```
$migrate = Yii::createObject([
        'class' => 'chenhakim\migration\components\MigrateCreate',
        'migrationPath' => $this->migrationPath
]);
$migrate->create($table);
```

---

备注：改代码是在e282486518的基础上修改更新的：

新增修改代码，备份为`batchInsert`

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c68729735077806dd9400fdf7b2af3ed37d89acf25321fd7f06c832323f33c9?d=identicon)[chenhakim](/maintainers/chenhakim)

---

Top Contributors

[![chenhakim](https://avatars.githubusercontent.com/u/22048862?v=4)](https://github.com/chenhakim "chenhakim (1 commits)")

### Embed Badge

![Health badge](/badges/chenhakim-migrate-backup/health.svg)

```
[![Health](https://phpackages.com/badges/chenhakim-migrate-backup/health.svg)](https://phpackages.com/packages/chenhakim-migrate-backup)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[pgvector/pgvector

pgvector support for PHP

198628.3k10](/packages/pgvector-pgvector)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
