Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone Database into Different Name in Laravel

Hi I am new to Laravel.

In our project ,we have to clone the current database into same server with different name.For example if our current database name is "test" means , I have to clone the database(test) into same server with different name like "test_2018" and I need all the tables in database(test) to our clone database(test_2018).

How can I achieve this in Laravel programmatically and I have used laravel version 5.0.

like image 798
Maheshwaran S Avatar asked Feb 08 '18 04:02

Maheshwaran S


People also ask

What does clone do in Laravel?

Have you ever needed to duplicate or clone a database record? Laravel provides a very handy function for this called replicate which will take an Eloquent model and make a copy so you can then make changes and save it. Here's an example of how you might use this.


1 Answers

I'm assuming you're using a MySql database.

You can create a batch file, then calling the file using the task scheduler in Laravel. inside your Laravel project in the App\Console\Kernel.php file, add this line to the schedule function to run the command every year.

protected function schedule(Schedule $schedule)
    {
         $schedule->exec('/batch.bat')->yearly();
    }

You need to create a scheduled task using the task scheduler if you have Windows, and if you have Linux (90% you have Linux especially if your project is running on the hosting server), then you should create a CRON job and place this script inside your script(according to Laravel documentation):

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Then create a batch.bat(Windows) or batch.sh(Linux) file inside your project folder, and place this content inside it:

mysql -u [USER-NAME] -p[PASSWORD] [YOUR-CURRENT-DATABASE-NAME] > /dumpfile.sql
CREATE DATABASE [NEW-DATABASE-NAME];
[NEW-DATABASE-NAME] < /dumpfile.sql

Don't wait and test it manually right away using the command:

php artisan schedule:run
like image 150
Ali Ali Avatar answered Oct 20 '22 00:10

Ali Ali