Laravel database backup automatically - cPanel shared hosting, VPS


Keeping data backup is one of the most important task for our web application. We have to keep database backup regularly, but manually keeping database backup in each day is a boring task and most of developers or owner of application doesn't maintain it.  If you are looking for a step by step process for making database backup automatically in every day then here I'm going to teach you how you can make your database backup automatically in cPanel shared hosting or VPS.

 

Laravel Backup Database Schedule

  1. Install Laravel DB Backup package
  2. Setup backup schedule in Laravel
  3. Configure schedule in Server-Side

 

1 Install Laravel DB Backup package

First, install the haruncpi/laravel-db-backup package by composer command.

composer require haruncpi/laravel-db-backup

Backup Location

local disk - storage/app/backups
s3 bucket - backups

 

2 Setup backup schedule in Laravel

In this part, we'll set up a schedule for database backup in our laravel application side. To do that, go to the app/Console/Kernel.php file and code like below inside schedule method.

protected function schedule(Schedule $schedule)
{
   $schedule->command('db:backup')->twiceDaily(0,12);
}

Note: twiceDaily by default fire at 1 and 13 (hours). So we have to set it 0,12 to match with cronjob in cPanel or VPS.

 

Backup to AWS S3 then just pass s3 with the artisan command. You must have to setup this before use S3.

protected function schedule(Schedule $schedule)
{
   $schedule->command('db:backup s3')->twiceDaily(0,12);
}

Note: Here we used twiceDaily method for making 2 backups each day. You can use daily method for one backup in each day or any other schedule method available for laravel schedule. Keep in mind your application schedule time and server schedule time must have to be same otherwise it'll not work.

 

3 Configure schedule in Server-Side

In this part, we'll set up our server (VPS, cPanel) for schedule database backup.

 

cPanel Shared Hosting

For setup laravel schedule in cPanel shared hosting, we have to use cronjob for laravel application. You can read details post on cronjob Laravel schedule in shared hosting.

Now, login to your cPanel and go to Cron Jobs option and set a cronjob to make database backup automatically.

cronjob-laravel-backup.png

Note: Here jhonsmith is cPanel username. You have to change it according to your cPanel username.

/usr/local/bin/php /home/jhonsmith/public_html/artisan schedule:run >> /dev/null 2>&1

 

VPS Hosting

You need to specify the exact location of your php and artisan file path.

/usr/local/php72/bin/php site.com/artisan schedule:run >> /dev/null 2>&1

If you navigate to your site root directory then you can write the command like given below.

cd ~/site.com  && /usr/local/php72/bin/php artisan schedule:run >> /dev/null 2>&1

 

Hope this step by step tutorial post will help you to make a Laravel database scheduler for your Laravel application into shared hosting or VPS. If you find this post helpful to you then please share the post with others.


Share on




Related Post - Latest Post


Laravel Barcode generation tutorial

Make laravel site super fast by page-cache!

Laravel maintenance mode bypass by Secret Route!

Laravel Datatables - Ajax, Column, Buttons, Customization

Laravel 8 authentication tutorial

What's new in Laravel 8