PHPackages                             joegreen0991/db-sync - 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. joegreen0991/db-sync

ActiveLibrary[Database &amp; ORM](/categories/database)

joegreen0991/db-sync
====================

Efficient MySQL comparison and sync utility

v3.3.0(9y ago)2941.7k89[9 issues](https://github.com/mrjgreen/db-sync/issues)[2 PRs](https://github.com/mrjgreen/db-sync/pulls)MITPHP

Since Sep 14Pushed 7y ago34 watchersCompare

[ Source](https://github.com/mrjgreen/db-sync)[ Packagist](https://packagist.org/packages/joegreen0991/db-sync)[ RSS](/packages/joegreen0991-db-sync/feed)WikiDiscussions v3 Synced today

READMEChangelog (10)Dependencies (7)Versions (17)Used By (0)

DbSync
======

[](#dbsync)

[![Build Status](https://camo.githubusercontent.com/121390f44970c9906f11b652110c920e2d182a72bcae9057a40a719eb66877ae/68747470733a2f2f7472617669732d63692e6f72672f6d726a677265656e2f64622d73796e632e7376673f6272616e63683d7633)](https://travis-ci.org/mrjgreen/db-sync)[![Coverage Status](https://camo.githubusercontent.com/151b665d3656a1c6ae50074ea558a123b1667db612edceee080e4d33fda6c242/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d726a677265656e2f64622d73796e632f62616467652e7376673f6272616e63683d763326736572766963653d676974687562)](https://coveralls.io/github/mrjgreen/db-sync?branch=v3)[![Latest Stable Version](https://camo.githubusercontent.com/8013e40ea1371ccf562026a33cae41b274446102fa2e35c446c85272aedbfef6/68747470733a2f2f706f7365722e707567782e6f72672f6d726a677265656e2f64622d73796e632f762f737461626c65)](https://packagist.org/packages/mrjgreen/db-sync)[![License](https://camo.githubusercontent.com/4603c045d0c25a0bfbb1299cb0051db355cadf98c20bc3d1c66a97f94ac6ef1f/68747470733a2f2f706f7365722e707567782e6f72672f6d726a677265656e2f64622d73796e632f6c6963656e7365)](https://packagist.org/packages/mrjgreen/db-sync)[![Total Downloads](https://camo.githubusercontent.com/1a19e2b00d2d9a78c3256bc2afe54526c8fab374e4e574469998919131cecbfb/68747470733a2f2f706f7365722e707567782e6f72672f6d726a677265656e2f64622d73796e632f646f776e6c6f616473)](https://packagist.org/packages/mrjgreen/db-sync)[![SensioLabsInsight](https://camo.githubusercontent.com/2ae072717fc5fde9d45b3a48abcfac97677741ae6ce453767dcfb2a667551d1a/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f33343538356637342d373433362d343763302d386234302d3431323635656635613162612f6d696e692e706e67)](https://insight.sensiolabs.com/projects/34585f74-7436-47c0-8b40-41265ef5a1ba)

### WARNING - This package modifies database tables. Use with extreme caution and back up databases before running.

[](#warning---this-package-modifies-database-tables-use-with-extreme-caution-and-back-up-databases-before-running)

#### Always perform a dry run (this is the default action) first before specifying the `--execute (-e)` option.

[](#always-perform-a-dry-run-this-is-the-default-action-first-before-specifying-the---execute--e-option)

What is it?
-----------

[](#what-is-it)

DbSync is a tool for efficiently comparing and synchronising two or more remote MySQL database tables.

In order to do this without comparing every byte of data, the tool preforms a checksum (MD5, SHA1, CRC32) over a range of rows on both the source and destination tables, and compares only the hash. If a block is found to have an inconsistency, the tool performs a checksum on each half of the block, recursively (down to a minimum block transfer size), until it finds the inconsistency.

Notes About Deletion
--------------------

[](#notes-about-deletion)

DbSync will only delete rows from the destination that no longer exist on the source when the `--delete` option is specified. Use this option with extreme caution. Always perform a dry run first.

If you use DbSync to synchronise a table which has row deletions on the source without using the `--delete` option, DbSync will find inconsistencies in any block with a deleted row on every run but will not be able to remove the rows from the target.

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

[](#installation)

Via composer - run the following command in your project directory:

```
composer require mrjgreen/db-sync

```

Or use the packaged archive directly

```
wget https://github.com/mrjgreen/db-sync/raw/v3/db-sync.phar -O db-sync.phar
chmod a+x db-sync.phar

```

Optionally make the command available globally

```
sudo mv db-sync.phar /usr/bin/db-sync

```

```
Usage:
db-sync [options]

Sync a mysql database table from one host to another using an efficient checksum algorithm to find differences.

Arguments:
  source                                     The source host ip to use.
  target                                     The target host ip to use.
  table                                      The fully qualified database table to sync.

Options:
  -b, --block-size=BLOCK-SIZE                The maximum block to use for when comparing. [default: 1024]
      --charset=CHARSET                      The charset to use for database connections. [default: "utf8"]
  -c, --columns=COLUMNS                      Columns to sync - all columns not "ignored" will be included....
  -C, --comparison=COMPARISON                Columns from the list of synced columns to use to create the...
  -f, --config-file=CONFIG-FILE              A path to a config.ini file from which to read values. [default: "dbsync.ini"]
      --delete                               Remove rows from the target table that do not exist in the source.
  -e, --execute                              Perform the data write on non-matching blocks.
  -h, --help                                 Show this usage information.
  -H, --hash                                 Specify the hash algorithm used to generate the comparison hash. [default: "md5"]
  -i, --ignore-columns=IGNORE-COLUMNS        Columns to ignore. Will not be copied or used to create the hash....
  -I, --ignore-comparison=IGNORE-COMPARISON  Columns to ignore from the hash. Columns will still be copied....
  -p, --password[=PASSWORD]                  The password for the specified user. Will be solicited on the tty if...
  -u, --user=USER                            The name of the user to connect with. [default: "USER"]
  -s, --transfer-size=TRANSFER-SIZE          The maximum copy size to use for when comparing. [default: 8]
      --target.user=TARGET.USER              The name of the user to connect to the target host with if different...
      --target.table=TARGET.TABLE            The name of the table on the target host if different to the source.
      --target.password=TARGET.PASSWORD      The password for the target host if the target user is specified....
      --where=WHERE                          A where clause to apply to the tables.
  -v, --verbose                              Enable verbose output.
  -q, --quiet                                Disable output, overrides "verbose" option.

```

### Examples

[](#examples)

*Note - All of these commands will perform a "dry-run" only. To execute the insert/update statement against the target database, you must specify the --execute (-e) option*

##### Example 1

[](#example-1)

Sync the table `web.customers` from one host to another (non-standard port on target):

```
db-sync --user root --password mypass 127.0.0.1 111.222.3.44:13306 web.customers

```

##### Example 2

[](#example-2)

Sync the table `web.customers` from one host to another, deleting rows from the target that no longer exist on the source (using SHA1 hash for comparison):

```
db-sync --user root --password mypass --hash sha1 --delete 127.0.0.1 111.222.3.44 web.customers

```

##### Example 3

[](#example-3)

Sync the table `web.customers` from one host to another using different credentials:

```
db-sync --user root --password mypass --target.user admin --target.password password 127.0.0.1 111.222.3.44 web.customers:

```

##### Example 4

[](#example-4)

Sync only the `email` and `name` fields from the table `web.customers`:

> NB. The primary key will automatically be included in the column set

```
db-sync --user root --password mypass 127.0.0.1 111.222.3.44 web.customers -c email -c name

```

##### Example 5

[](#example-5)

Sync every column except for the `updated_at` fields from the table `web.customers`:

```
db-sync --user root --password mypass 127.0.0.1 111.222.3.44 web.customers -i updated_at

```

##### Example 6

[](#example-6)

Sync every column from the table `web.customers` but only use the `updated_at` fields when calculating the hash:

> Inconsistencies in other fields will not be detected. In the event of a hash inconsistency in fields which are included, the excluded fields will still be copied to the target host.

```
db-sync --user root --password mypass 127.0.0.1 111.222.3.44 web.customers -C updated_at

```

##### Example 7

[](#example-7)

Sync every column from the table `web.customers` and use all fields except for the `notes` or `info` fields when calculating the hash:

> Inconsistencies in excluded fields will not be detected. In the event of a hash inconsistency in fields which are included, the excluded fields will still be copied to the target host.

> This is especially useful for tables with long text fields that don't change after initial insert, or which are associated with an `on update CURRENT_TIMESTAMP` field. For large tables this can offer a big performance boost.

```
db-sync --user root --password mypass 127.0.0.1 111.222.3.44 web.customers -I notes -I info

```

##### Example 8

[](#example-8)

Sync the table `web.customers` to a table under a different name in a different database `web_backup.customers_2`:

```
db-sync --user root --password mypass --target.table web_backup.customers_2 127.0.0.1 111.222.3.44 web.customers

```

##### Example 9

[](#example-9)

Sync the table `web.customers` only including active records and delete records in the target table, that are no longer active:

```
db-sync --user root --password mypass 127.0.0.1 111.222.3.44:13306 web.customers --delete --where="active = 1"

```

Config File
-----------

[](#config-file)

To avoid having to specify options repeatedly, and to avoid exposing your password on the tty you can specify a config file. By default DbSync will look for a file named `dbsync.ini` in the current working directory.

Example:

```
user=root
password=mypass
target.user=admin
target.password=myadminpass
```

Use library within project (non-commandline)
--------------------------------------------

[](#use-library-within-project-non-commandline)

You can include the library within your project and use the components directly:

```
use \DbSync\DbSync;
use \DbSync\Transfer\Transfer;
use \DbSync\Hash\ShaHash;
use \DbSync\Table;
use \DbSync\ColumnConfiguration;

$sync = new DbSync(new Transfer(new ShaHash(), $blockSize, $transferSize));

$sync->setLogger(new YourPsrLogger());

$sync->dryRun(false);

$sync->delete(true);

$sourceTable = new Table($sourceConnection, $sourceDb, $sourceTable);
$targetTable = new Table($targetConnection, $targetDb, $targetTable);

// if you only want specific columns
$columnConfig = new ColumnConfiguration($syncColumns, $ignoreColumns);

// if you only want to use specific columns for the comparison
$compareConfig = new ColumnConfiguration($compareColumns, $ignoreCompareColumns);

// optionally apply a where clause - this can be useful when sync-ing large tables, where
// you can make use of a column to rule out large portions of the data
// that you know haven't changed, such as columns with "on update CURRENT_TIMESTAMP" etc..
$sourceTable->setWhereClause(new WhereClause("column_name = ?", ['value']));
$targetTable->setWhereClause(new WhereClause("column_name > ?", ['value']));

$sync->sync($sourceTable, $targetTable, $columnConfig, $compareConfig);
```

Roadmap
-------

[](#roadmap)

- 100% test coverage via full stack integration tests
- Allow option to delete data from target where not present on the source
- Use symfony console command for sync
- Option to re-try with back-off on lock wait timeouts
- Option to create missing tables on target
- Option to skip duplicate key errors
- Speed up initial sync of empty table - Maybe offer combination with other tool for full fast outfile based replacement

Requirements
------------

[](#requirements)

PHP 5.4 or above PDO MySQL Extension

License
-------

[](#license)

DbSync is licensed under the MIT License - see the LICENSE file for details

Acknowledgments
---------------

[](#acknowledgments)

- Inspiration for this project came from the Percona Tools `pt-table-sync`.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 98.2% 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.

###  Release Activity

Cadence

Every ~109 days

Recently: every ~266 days

Total

14

Last Release

2836d ago

Major Versions

v1.1.1 → v2.0.02014-10-22

v2.5.0 → v3.0.02015-09-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/b3593b2ddeb932b3e7ac41221afd89becb377734b74416fcdb2114965c9f498a?d=identicon)[mrjgreen](/maintainers/mrjgreen)

---

Top Contributors

[![mrjgreen](https://avatars.githubusercontent.com/u/2183391?v=4)](https://github.com/mrjgreen "mrjgreen (108 commits)")[![aydancoskun](https://avatars.githubusercontent.com/u/10948793?v=4)](https://github.com/aydancoskun "aydancoskun (2 commits)")

---

Tags

databasemysqlmysql-backupmysql-replicationsync-data

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/joegreen0991-db-sync/health.svg)

```
[![Health](https://phpackages.com/badges/joegreen0991-db-sync/health.svg)](https://phpackages.com/packages/joegreen0991-db-sync)
```

###  Alternatives

[kimai/kimai

Kimai - Time Tracking

4.6k7.4k1](/packages/kimai-kimai)[mrjgreen/db-sync

Efficient MySQL comparison and sync utility

29438.6k](/packages/mrjgreen-db-sync)[guikingone/scheduler-bundle

A Symfony bundle that allows to schedule and create repetitive tasks

114217.4k](/packages/guikingone-scheduler-bundle)[bartlett/php-compatinfo-db

Reference Database of all functions, constants, classes, interfaces on PHP standard distribution and about 110 extensions

1183.0k1](/packages/bartlett-php-compatinfo-db)[worksome/foggy

Foggy is a tool for making database dumps with some data removed/changed.

26571.7k1](/packages/worksome-foggy)[perplorm/perpl

Perpl is an improved and still maintained fork of Propel2, an open-source Object-Relational Mapping (ORM) for PHP.

203.7k](/packages/perplorm-perpl)

PHPackages © 2026

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