How to take Backup of Laravel Application and Store it on Amazon S3

In the past, I have published an article storing Laravel backup locally on the server. Though in that article we discussed storing a backup on the hosting server, it’s not recommended a way of storage. The safer option for storing backup is on the cloud. It may happen your server gets crashed and you lose the application along with locally stored backups. In these worst scenarios, if you have a backup stored on the cloud then you can revert it easily at any time.

Considering the importance of backup, in this article, we study how to backup a Laravel application and store it on Amazon s3. To achieve our goal, we’ll use both Laravel Filesystem and the package developed by Spatie.

Get Your AWS Security Credentials

To get started, you should first have an account on Amazon S3. After creating the account make sure you have activated the S3 service by following the AWS verification process.

After activating the S3 service, get your security credentials which we will need in a few moments. You will get it by clicking on the link ‘My Security Credentials’.

AWS Credentials

Configure S3 Driver in Laravel

Head over to your code editor and open the file config/filesystems.php. In this file, you can see S3 details are set using the env() method. It means you need to set your S3 credentials in .env file.

config/filesystems.php

<?php
return [
 
    .....
 
    'disks' => [
 
        .....
 
        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],
    ...
    ...
];

Open your .env file and add the AWS S3 credentials to it. No need to set the AWS_URL value as it is optional.

.env

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
AWS_BUCKET=

Before using the S3, you will need to install the appropriate package via Composer:

composer require league/flysystem-aws-s3-v3 ~1.0

Install and Configure Spatie’s Library

Before installing Spatie’s library make sure your system meets the package requirements. Read more about it on their Requirements page.

If you are using an older version of Laravel, then check out one of the previous versions of this package at v6, v5, and v4.

Let’s install the package by running the command below:

composer require spatie/laravel-backup

Once the library is installed, run the next command below which will publish the config file to config/backup.php.

php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"

Open config/backup.php file and set the ‘disks’ name as ‘s3’. We are telling this package to store the Laravel backup on the Amazon S3 bucket.

<?php
...
...
/*
* The disk names on which the backups will be stored.
*/
'disks' => [
    's3',
],

There are other configurations also available in this file. A user can include and exclude the directories for backup. For the include the value is base_path() and for exclude option values are base_path(‘vendor’), base_path(‘node_modules’). It means don’t include vendor and nod_modules folders in the final backup.

Store the Backup of Laravel Application on Amazon S3

We are done with all the basic configurations required to store Laravel backup on Amazon S3. Now we can store our first backup. Open the terminal in your project root directory and run the command:

php artisan backup:run

The above command will backup your Laravel application on AWS. You will find the zip of the backup in your S3 bucket. The backup zip contains your application files, folders, and database stored in it.

AWS-Backup

This library also allows scheduling a backup process. You can automate your backup task. For this, add the below code in the app/Console/Kernel.php file.

<?php
...
...
protected function schedule(Schedule $schedule)
{
    $schedule->command('backup:run')->daily()->at('06:00');
}

Here I set the daily backup at 06:00. The user can change this time as per their requirement.

I hope you understand how to take a backup of the Laravel application and store it on Amazon S3. I would like to hear your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

2 thoughts on “How to take Backup of Laravel Application and Store it on Amazon S3

  1. ‘”mysqldump”‘ is not recognized as an internal or external command, operable program or batch file.

    i am faceing this error. can you please help?

    1. You should have set MySQL in environment variable. In my case I set c://xampp/mysql/bin path in system environment variables.

Leave a Reply

Your email address will not be published. Required fields are marked *