├── .gitignore └── .gitignore ├── README.md ├── composer.json └── src ├── DBSync.php └── ServiceProvider.php /.gitignore/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Usage 2 | This package allows you to sync your remote database to your local db for local dev purposes when trying to use live data. 3 | 4 | # Set Up 5 | Add the following environment variables to your .env file ( LOCAL ONLY ) 6 | ``` 7 | REMOTE_SYNC_URL= 8 | REMOTE_SYNC_DB_NAME= 9 | REMOTE_SYNC_SSH_USERNAME= 10 | REMOTE_SYNC_SSH_PASSWORD= 11 | ``` 12 | 13 | ## Artisan Command 14 | ```shell 15 | php aritsan mwi:db:sync 16 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "name": "mwi/laravel-db-sync", 4 | "description": "Sync remote database to local", 5 | "keywords": ["laravel", "db", "sync", "database"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Midwestern Interactive", 10 | "email": "support@buildmidwestern.com" 11 | } 12 | ], 13 | "require": { 14 | "phpseclib/phpseclib": "^2.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { "MWI\\LaravelDBSync\\": "src/" } 18 | }, 19 | "config": { 20 | "preferred-install": "dist", 21 | "sort-packages": true, 22 | "optimize-autoloader": true 23 | }, 24 | "extra": { 25 | "laravel": { 26 | "providers": [ 27 | "MWI\\LaravelDBSync\\ServiceProvider" 28 | ] 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/DBSync.php: -------------------------------------------------------------------------------- 1 | error("Please don't try and run this in production... will not end well."); 53 | return; 54 | } 55 | 56 | if(!$remote_db || !$remote_db || !$ssh_user || !$ssh_pass){ 57 | $this->error('Add your environment variables!'); 58 | return; 59 | } 60 | 61 | // Connect via ssh to dump the db on the remote server. 62 | $ssh = new SSH2($remote_url); 63 | if (!$ssh->login($ssh_user, $ssh_pass)) { 64 | $this->error('Login failed make sure your ssh username and password is set in your env file.'); 65 | return; 66 | } 67 | $ssh->exec('mysqldump -u ' . $ssh_user . ' -pxyzzy ' . $remote_db . ' > sync_backup.sql'); 68 | 69 | // Connect via sftp to d/l the dump 70 | $sftp = new SFTP($remote_url); 71 | 72 | if (!$sftp->login($ssh_user, $ssh_pass)) { 73 | $this->error('Login failed make sure your SSH username and password is set in your env file.'); 74 | return; 75 | } 76 | 77 | // Temporarily remove memory limit 78 | ini_set('memory_limit', '-1'); 79 | 80 | $this->info('Getting the backup.'); 81 | 82 | $sftp->get('sync_backup.sql', storage_path('sync_backup.sql')); 83 | 84 | $this->info('Importing...'); 85 | DB::unprepared(File::get(storage_path('sync_backup.sql'))); 86 | 87 | $this->info('Migrating...'); 88 | $this->call('migrate'); 89 | $this->info('Removing back up files.'); 90 | $ssh->exec('rm sync_backup.sql'); 91 | File::delete(storage_path('sync_backup.sql')); 92 | $this->info('Complete! You are synced with the remote DB.'); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 12 | $this->commands([ 13 | Commands\DBSync::class 14 | ]); 15 | } 16 | } 17 | } --------------------------------------------------------------------------------