helbrary/db-synchronizer

用于同步远程数据库到本地数据库的库

v0.1.3 2016-07-29 16:35 UTC

This package is auto-updated.

Last update: 2024-08-29 04:14:35 UTC


README

DbSynchronizer是一个库,用于加速某些不能从外部连接数据库的网站远程数据库的经常重复操作。该库适用于nette框架。

功能

  • 备份远程数据库
  • 下载远程数据库的转储
  • 将远程数据库的转储导入到本地数据库

快速入门

配置参数

		dbSyncParams:
			web:
				productionBaeUrl: 'http://somewebpage.cz'
				authAction: 'db-dump-login' # it means thath authorization action must be available on url 'http://somewebpage.cz/db-dump-login'
				dumpDownloadAction: 'db-download-dump' # it means thath download dump action must be available on url 'http://somewebpage.cz/db-download-dump'
				authUsername: 'admin'
				authPassword: 'admin_password'
			database:
				production:
					host: 'some_host'
					dbname: 'my_remote_db'
					user: 'admin'
					password: 'admin_password'
				local:
					host: 'localhost'
					dbname: 'my_db'
					user: 'root'
					password:

注册控制台命令

	console:
		commands:
			- Helbrary\DbSynchronizer\Commands\DatabaseCommand(%tempDir%, %dbSyncParams%)

服务器端

现在需要在服务器端实现db同步器的API。它可以看起来像这样

    // function for authorization
	public function actionAuthenticateForDump($username, $password)
	{
		try {
			$identity = $this->authenticator->authenticate( array( $username, $password ) );
			if (in_array('admin', $identity->roles)) {
				$this->dbSynchronizer->setAsAuthorized();
			}
		} catch (AuthenticationException $e) {
			Debugger::log($e, Base::LOG_DIRECTORY_NAME);
		}
		$this->terminate();
	}



	public function actionDownloadDump($host, $username, $password, $db)
	{
		try {
			$dump = $this->dbSynchronizer->dump($host, $username, $password, $db);
			$fileResponse = new FileResponse($dump, 'dump.sql', 'application/sql', TRUE);
			$this->presenter->sendResponse($fileResponse);
		} catch (\Exception $e) {
			Debugger::log($e, Base::LOG_DIRECTORY_NAME);
			header("HTTP/1.0 404 Not Found");
		}
		$this->terminate();
	}

客户端

打开控制台并执行

php index.php db --sync-local # for download remote dump and import to local db
php index.php db --backup-remote # for backup remote db, dump of db will be saved in temp/dumps directory of web