DEV Community

Cover image for Automate your database backups with Laravel: A comprehensive guide
Nikola Perišić
Nikola Perišić

Posted on • Updated on

Automate your database backups with Laravel: A comprehensive guide

👋 Hello everyone!

🚀 Today, we'll explore how to automate database backups using Laravel.

📁 The backup consists of a zip file containing all specified directories' files and a database dump. Store it on any of your configured filesystems. Plus, receive notifications via E-mail, Slack, or any provider if something goes wrong with your backups.

Let's dive in!


1. Composer package installation

composer require spatie/laravel-backup
Enter fullscreen mode Exit fullscreen mode

To publish the config file to config/backup.php run:

php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
Enter fullscreen mode Exit fullscreen mode

2. Database backup execution

php artisan backup:run
Enter fullscreen mode Exit fullscreen mode

3. MySQL clients installation

What are MYSQL clients?

MySQL clients are software tools that allow users to interact with MySQL databases, enabling tasks such as running SQL commands, managing database structures, and importing/exporting data. They also enable database dumping, which involves exporting the entire database or specific tables into a file for backup or transfer purposes.

🐧 For Arch Linux users:
sudo pacman -S mysql-clients

🐧 For Ubuntu/Debian users:
sudo apt-get install mysql-client

💻 For macOS users:
brew install mysql-client

💻 For Windows users: Install MySQL Installer

4. Commands execution

php artisan storage:link
php artisan backup:run
Enter fullscreen mode Exit fullscreen mode

Voila!

Terminal Garuda Linux

📂 Locate the dumped zip inside storage/app/Laravel directory.
You can modify this export path location in the exported config file found in config/backup.php.

Laravel project architecture

🗓️ As you can observe, the format is 2024-06-02-16-01-40.zip. You can adjust this in the configuration file on line.


Additional

Cleanup old backups

//config/backup.php

    'cleanup' => [
        /*
         * The strategy that will be used to cleanup old backups. The default strategy
         * will keep all backups for a certain amount of days. After that period only
         * a daily backup will be kept. After that period only weekly backups will
         * be kept and so on.
         *
         * No matter how you configure it the default strategy will never
         * deleted the newest backup.
         */
        'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class,

        'default_strategy' => [

            /*
             * The number of days that all backups must be kept.
             */
            'keep_all_backups_for_days' => 7,

            /*
             * The number of days that all daily backups must be kept.
             */
            'keep_daily_backups_for_days' => 16,

            /*
             * The number of weeks of which one weekly backup must be kept.
             */
            'keep_weekly_backups_for_weeks' => 8,

            /*
             * The number of months of which one monthly backup must be kept.
             */
            'keep_monthly_backups_for_months' => 4,

            /*
             * The number of years of which one yearly backup must be kept.
             */
            'keep_yearly_backups_for_years' => 2,

            /*
             * After cleaning up the backups remove the oldest backup until
             * this amount of megabytes has been reached.
             * Set null for unlimited size.
             */
            'delete_oldest_backups_when_using_more_megabytes_than' => 5000,
        ],
    ],

Enter fullscreen mode Exit fullscreen mode

Use the following command:

php artisan backup:clean
Enter fullscreen mode Exit fullscreen mode

Sending notifications

Visit official package documentation page


Thank you for reading my blog post! Hope you found it helpful.

👉 Follow me on GitHub for more updates!

Top comments (4)

Collapse
 
g1dra profile image
Darko Vucetic

Good job :)

Collapse
 
perisicnikola37 profile image
Nikola Perišić

Thank you!

Collapse
 
dung_nguyen_3d9ec24ffda4d profile image
Dzung Nguyen 🇻🇳

helpful. awesome post.

Collapse
 
perisicnikola37 profile image
Nikola Perišić

Hi Dung Nguyen,
Thanks :) Follow me to stay up to date with more content like this 🚀