Automate your database backups with Laravel: A comprehensive guide
Tags: webdev, laravel, php, backenddevelopment
Published: 2024-06-02 16:16:49

👋 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 <u>any</u> 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

```php
composer require spatie/laravel-backup
```

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

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

## 2. Database backup execution

```php
php artisan backup:run
```

## 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](https://dev.mysql.com/downloads/installer/)

## 4. Commands execution

```php
php artisan storage:link
php artisan backup:run
```

---

## Voila!

![Terminal Garuda Linux](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yncy2ykvg4k31uy0zl5s.png)

📂 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tnecd6vix2k19racr1p.png)

🗓️ 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

```php
//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,
        ],
    ],

```

Use the following command:

```php
php artisan backup:clean
```

#### Sending notifications
Visit [official package documentation page](https://spatie.be/docs/laravel-backup/v8/sending-notifications/overview)

---

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

👉 Follow me on [GitHub](https://github.com/perisicnikola37) for more updates!