<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>laravel 10 backup database Archives - Codeplaners</title>
	<atom:link href="https://codeplaners.com/tag/laravel-10-backup-database/feed/" rel="self" type="application/rss+xml" />
	<link>https://codeplaners.com/tag/laravel-10-backup-database/</link>
	<description>Code Solution</description>
	<lastBuildDate>Wed, 01 Jan 2025 06:38:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>

<image>
	<url>https://codeplaners.com/wp-content/uploads/2020/09/cropped-favicon-social-32x32.png</url>
	<title>laravel 10 backup database Archives - Codeplaners</title>
	<link>https://codeplaners.com/tag/laravel-10-backup-database/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Laravel 10  Backup Database While Excluding Specific Tables</title>
		<link>https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/</link>
					<comments>https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 15 Dec 2024 04:04:06 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 10]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[Laravel 9]]></category>
		<category><![CDATA[laravel 10 backup database]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=1661</guid>

					<description><![CDATA[<p>This post was last updated on January 1st, 2025 at 06:38 amHi Dev, Today, we will show you laravel 10 backup database while excluding specific tables. This article will give you simple example of laravel 10 backup database while excluding specific tables. Let&#8217;s discuss laravel 10 backup database while excluding specific tables. In this article, &#8230; <a href="https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/" class="more-link">Continue reading<span class="screen-reader-text"> "Laravel 10  Backup Database While Excluding Specific Tables"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/">Laravel 10  Backup Database While Excluding Specific Tables</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p class="last-modified">This post was last updated on January 1st, 2025 at 06:38 am</p><p>Hi Dev,</p>
<p>Today, we will show you laravel 10  backup database while excluding specific tables. This article will give you simple example of laravel 10  backup database while excluding specific tables. Let&#8217;s discuss laravel 10  backup database while excluding specific tables. In this article, we will implement a laravel 10  backup database while excluding specific tables. </p>
<p>So let’s follow few step to create example of laravel 10  backup database while excluding specific tables.</p>
<h3 class="step_code">Install the Laravel 10</h3>
<pre class="brush: php; title: ; notranslate">
composer create-project laravel/laravel example-app
</pre>
<h3 class="step_code">Database Configuration</h3>
<p><strong>.env</strong></p>
<pre class="brush: php; title: ; notranslate">
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database name(laravel)
DB_USERNAME=username(root)
DB_PASSWORD=password(root)
</pre>
<h3 class="step_code">Create Migration</h3>
<p><strong>Create Products Table</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan make:migration create_products_table --create=products
</pre>
<p><strong>database\migrations\create_products_table.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string('name');
            $table-&gt;text('detail');
            $table-&gt;string('price');
            $table-&gt;string('image');
            $table-&gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};
</pre>
<p><strong>Create Orders Table</strong><br />
<strong>database\migrations\create_orders_table.php</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan make:migration create_orders_table --create=orders
</pre>
<pre class="brush: php; title: ; notranslate">
&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('orders', function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string('detail');
            $table-&gt;string('quantity');
            $table-&gt;string('total');
            $table-&gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('orders');
    }
};
</pre>
<h3 class="step_code">run this migration by following the command</h3>
<pre class="brush: php; title: ; notranslate">
php artisan migrate
</pre>
<h3 class="step_code">Create Scheduler Command</h3>
<p><strong>We need to create a scheduler command to backup the database ignoring specific tables. To create a scheduler command, run the following command:</strong></p>
<pre class="brush: php; title: ; notranslate">
php artisan make:command DatabaseBackup --command=databasebackup:cron
</pre>
<p><strong>app/Console/Commands/DatabaseBackup.php</strong><br />
Set code to &#8220;DatabaseBackup.php&#8221; file.</p>
<p><strong>DatabaseBackup.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Redirect;
use Carbon\Carbon;
use Carbon\CarbonPeriod;

class DatabaseBackup extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'databasebackup:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        // Genrate backup files 
        $filename = \Carbon\Carbon::now()-&gt;format('m-d-Y').&quot;-backup.sql.gz&quot;;

        $command = &quot;mysqldump --user=&quot; . env('DB_USERNAME') .&quot; --password=&quot; . env('DB_PASSWORD') . &quot; --host=&quot; . env('DB_HOST') . &quot; \
            --ignore-table=&quot;.env('DB_DATABASE').&quot;.tablename_to_ignore \
            --ignore-table=&quot;.env('DB_DATABASE').&quot;.tablename_to_ignore &quot; . env('DB_DATABASE') . &quot; | gzip &gt; &quot; .storage_path(&quot;app/backup/&quot;. $filename);

        $returnVar = NULL;
        $output  = NULL;
  
        exec($command, $output, $returnVar);

        // Delete existing backup files 
        $startDate = Carbon::now()-&gt;subDays(30);
        $endDate = Carbon::now();
  
        $dateRange = CarbonPeriod::create($startDate, $endDate);
        $dates = &#x5B;'.gitignore'];
        foreach ($dateRange-&gt;toArray() as $key =&gt; $value) {
            $dates&#x5B;] = $value-&gt;format('m-d-Y').&quot;-backup.sql.gz&quot;;
        }

        $mediaPath = storage_path('app/backup');
        $files = File::allFiles($mediaPath);

        foreach ($files as $key =&gt; $value) {
            $fileName = $value-&gt;getRelativePathname();
            if (!in_array($fileName, $dates)) {
                File::delete($value);
            }
        }
    }
}
</pre>
<p><strong>Set up a daily cron job for automatic backups in file path &#8220;app/Console/Kernel.php&#8221;.</strong><br />
<strong>Kernel.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        $schedule-&gt;command('databasebackup:cron')-&gt;daily();
    }

    /**
     * Register the commands for the application.
     */
    protected function commands(): void
    {
        $this-&gt;load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
</pre>
<p>execute the scheduler code, run the command below.</p>
<pre class="brush: php; title: ; notranslate">
php artisan databasebackup:cron
</pre>
<p><strong>The backup file will be generated at the path &#8216;storage/app/backup&#8217;.</strong></p>
<p>I hope it will assist you…</p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/">Laravel 10  Backup Database While Excluding Specific Tables</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/laravel-10-backup-database-while-excluding-specific-tables/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
