Laravel Database Snapshot Admin UI

Author - Krishna Solanki

Laravel Database Snapshot Admin UI is similar to admin panel in which you can take a snapshot of the database, show previously taken database snapshot list, download snapshot SQL file and delete the snapshot file.

The following steps are for making laravel database snapshot admin UI

  • Create a Laravel Project

        Laravel new db-snapshot-admin-UI

  • Install Database Snapshot Package Via Composer

       Composer require spatie/laravel-db-snapshots

  • Service Provider Configuration

       If Laravel 5.4 or below version you must add below code otherwise it will be auto-registered.

       Open the file config/app.php and then add following service provider

  • Filesystem Configuration

In config/filesystems.php add following code for creating snapshot disk on which all snapshots will be        saved. You can change the driver and root values.

  • Publish The Configuration File

       This is optional, you may publish the configuration file using this command to customize your setting.

         php artisan vendor:publish –provider=”Spatie\DbSnapshots\DbSnapshotsServiceProvider” –tag=”config”     //publish configuration file will be stored at config\db-snapshots.php

  • Create an admin login using laravel auth

     php artisan make:auth

  • Database configuration

     In Env file, specify the value of host, database name, user name, password

     DB_HOST=YOUR_HOST

     DB_DATABASE=YOUR_DATABASE

    DB_USERNAME=YOUR_USERNAME

    DB_PASSWORD=YOUR_PASSWORD

  • Create Migrations

  • Make a Controller, In which Controller create functions like index,, CreateDBSnapshot, DBSnapshotList, DBSnapshotDatatable, DBSnapshotDownload, DBSnapshotDelete.

Create a Snapshot

In this form, we take a snapshot name from the user for creating that named snapshot. CreateDBSnapshot method will call when submitting the form

 

Creating validation like snapshot name is required and must contain alphanumeric characters. Remove the underscore from snapshot name and add a carbon DateTime for preventing duplications of snapshot’s name.

Artisan::call(‘snapshot:create ‘.$file_name) is use for create snapshot from database.

Snapshot list

Display snapshots list which is stored in our disk. laravel yajra data table is used for the display snapshot list.

Artisan::call(‘snapshot:list’) is used to list all snapshots. Getting output on screen we use Artisan::output() command. After receiving output, you can get the value of snapshot name, date, size from the formatted output.

Download Snapshot

Click on the download button, download Snapshot SQL file from storage. In this function pass snapshot name.

Delete Snapshot

Click on the delete button, delete the snapshot file from storage. In this function pass snapshot name which you want to delete.

Artisan::call(‘snapshot:delete ‘.$name) is used for delete snapshot file from storage.

Some useful console commands for database snapshot

  • Create a snapshot with user defined name

    php artisan snapshot:create snapshot_name

    In this command create a snapshot file with a user-defined name. File store at a place which is specified      in the filesystem.

  • Create a snapshot with the current date and time as snapshot name

    php artisan snapshot:create

    If we do not pass the snapshot name it will be taking the current date time as snapshot name

  • Create Compressed Snapshot SQL file

    php artisan snapshot:create snapshot_name –compress

    If you want to compress the snapshot file this command is used. We can use it if the database is too large.

  • Load Snapshot file

   php artisan snapshot:load snapshot_name

   This command used for load snapshot file. In this command, the snapshot name specify which snapshot      is load

  • Load Snapshot file from multiple connections

    php artisan snapshot:load snapshot_name –connection=connectionName  

    when multiple connections are available, then load snapshot file from specific connection this                         command  is used

  • List all snapshot files

    php artisan snapshot:list

    This command is used to get a list of all snapshot created. This is return snapshot name, date and                     snapshot file size.

  • Delete specific snapshot file

    php artisan snapshot:delete snapshot_name  

   If you want to delete snapshot this command is used. In this command, specify snapshot name which           you want to delete

Some useful events

  • This Event will be fired before a snapshot is created

     Spatie\DbSnapshots\Events\CreatingSnapshot

  • This Event will be fired after a snapshot has been created

    Spatie\DbSnapshots\Events\CreatedSnapshot

  • This Event will be fired before a snapshot is loaded

    Spatie\DbSnapshots\Events\LoadingSnapshot

  • This Event will be fired after a snapshot has been loaded

    Spatie\DbSnapshots\Events\LoadedSnapshot

  • This Event will be fired before a snapshot is deleted

    Spatie\DbSnapshots\Events\DeletingSnapshot

  • This Event will be fired after a snapshot has been deleted

Spatie\DbSnapshots\Events\DeletedSnapshot

Laravel database snapshot admin UI is useful for the backup database from the admin panel. Now, you can create a database snapshot in your project easily. You can download this from link Github. If you have any queries or questions, feel free to comment.

Thanks for reading this article. I hope it will be useful for you.

https://github.com/logisticinfotech/Laravel-Database-Snapshot-from-Admin-UI

Don’t miss the next post!

Related Posts