Giter VIP home page Giter VIP logo

laravel-db-sync's Introduction

Community

There is a Discord community. https://discord.gg/VYau8hgwrm For quick help, ask questions in the appropriate channel.

Laravel DB Sync

DB Dync

Introduction

Sync remote database to a local database

A word of warning you should only sync a remote database into a local database if you have permission to do so within your organisation's policies. I'm syncing during early phases of development where the data is largely test data and not actual customer data.

Connection can be made over SSH or using a remote MySQL connection.

Install

Install the package.

composer require dcblogdev/laravel-db-sync

Config

You can publish the config file with:

php artisan vendor:publish --provider="Dcblogdev\DbSync\DbSyncServiceProvider" --tag="config"

.env

Set the remote database credentials in your .env file

When using SSH Add:

REMOTE_USE_SSH=true
REMOTE_SSH_PORT=22
REMOTE_SSH_USERNAME=
REMOTE_DATABASE_HOST=

REMOTE_DATABASE_USERNAME=
REMOTE_DATABASE_PORT=3306
REMOTE_DATABASE_NAME=
REMOTE_DATABASE_PASSWORD=
REMOTE_DATABASE_IGNORE_TABLES=''

REMOTE_REMOVE_FILE_AFTER_IMPORT=true
REMOTE_IMPORT_FILE=true

For only MySQL remote connections:

REMOTE_DATABASE_HOST=
REMOTE_DATABASE_USERNAME=
REMOTE_DATABASE_PORT=3306
REMOTE_DATABASE_NAME=
REMOTE_DATABASE_PASSWORD=
REMOTE_DATABASE_IGNORE_TABLES=''

REMOTE_REMOVE_FILE_AFTER_IMPORT=true
REMOTE_IMPORT_FILE=true

Set a comma seperate list of tables NOT to export in REMOTE_DATABASE_IGNORE_TABLES

To generate a SQL with a custom file name REMOTE_DEFAULT_FILE_NAME

To specify a different local database connection:

LOCAL_TARGET_CONNECTION=different_mysql_connection

Set the mysql command path:

LOCAL_MYSQL_PATH=/usr/bin/mysql

For only mysqldump:

REMOTE_MYSQLDUMP_SKIP_TZ_UTC=true

Usage

To export a remote database to OVERRIDE your local database by running:

php artisan db:production-sync

Provide a filename for export on the fly by passing the option --filename, remember to provide .sql

php artisan db:production-sync --filename=other.sql

Run the command without attempting to export:

php artisan db:production-sync --test

Aliases

There are shortcuts that can be used:

-T = will use --test F = will use --filename

Alternative name

When connecting to a none production database, say a staging database you can choose to use this alternative name:

db:remote-sync may be used instead of db:production-sync

laravel-db-sync's People

Contributors

aleksandar-mitic avatar davidbirkin avatar dcblogdev avatar estrma avatar imsus avatar m-popa avatar thursdaydan avatar witti avatar xfolder avatar zaryabakhtar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-db-sync's Issues

Getting Allowed memory

Hello,

I have a big DB, when I try the sync I get error something like: Allowed memory size of 536870912 bytes exhausted (tried to allocate 477174084 bytes)

Connecting via SSH

First, congrats, I think that this package it's super useful! ๐ŸŽ‰

While trying to make it work with my production database, I notice that I need to open and SSH server to connect to my production database because it's protected.

With something similar to this, it could be possible:

// Open a tunnel from remote host with remote user and bind it in localhost:3307
shell_exec("ssh -f -L 3307:127.0.0.1:3306 $remoteUser@$remoteHost sleep 60 >> logfile");

// Then connect to localhost 3307
exec("mysqldump -h 127.0.0.1 -P 3307 -u $username -p$password $database --column-statistics=0 $ignoreString > file.sql",
                $output);

What do you think?

Error handling

If there can be feedback when a connection cannot be established that would be a better experiance.

remote to remote

I have 2 scripts running on different servers I want to copy data from 1 to server 2 every 30 mins

It didn't work, but I don't know how to debug it

It didn't work โ€” my local database was left unmodified.
Here's the console output:

$ php artisan db:production-sync
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 
Warning: A dump from a server that has GTIDs enabled will by default include the GTIDs of all transactions, even those that were executed during its extraction and might not be represented in the dumped data. This might result in an inconsistent data dump. 
In order to ensure a consistent backup of the database, pass --single-transaction or --lock-all-tables or --master-data. 

DB Synced

Database port option?

Hello,

In certain conditions, there might be a different port of the database server e.g DigitalOcean use another port instead of 3306 for their MySQL managed service.

progress bar

It would be better if there was a progress bar when importing for cases when importing a large database.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.