ahmedweb/laravel-backup-service

A simple Laravel package to make backup services easier to implement using traits.

v1.0.3 2025-06-01 11:26 UTC

This package is auto-updated.

Last update: 2025-06-01 11:26:51 UTC


README

Latest Version on Packagist License: MIT

Laravel Backup Service is a powerful and developer-friendly package designed to automate your Laravel application backups. It seamlessly integrates with Google Drive and Telegram, offering convenient backup uploads, link notifications, and scheduled cleanup โ€“ all through simple Artisan commands.

๐Ÿš€ Features

  • ๐Ÿ“ Backup uploads directly to Google Drive
  • ๐Ÿ”— Auto-send backup download links to Telegram
  • ๐Ÿ“Œ Store and manage latest backup file links
  • ๐Ÿงน Automatically delete outdated backup files
  • โœ… Clean, modular, and maintainable code structure
  • ๐Ÿ’ป Easy-to-use Artisan commands
  • ๐Ÿ• Fully schedulable via Laravel Scheduler

๐Ÿ“ฆ Installation

Install the package via Composer:

composer require ahmedweb/laravel-backup-service

Publish the configuration and service provider:

php artisan vendor:publish --tag=laravel-backup-service

โš™๏ธ Configuration

1. Environment Setup

Add the following variables to your .env file:

FILESYSTEM_CLOUD=google

GOOGLE_DRIVE_CLIENT_ID=your-client-id
GOOGLE_DRIVE_CLIENT_SECRET=your-client-secret
GOOGLE_DRIVE_REFRESH_TOKEN=your-refresh-token
GOOGLE_DRIVE_FOLDER_ID=your-folder-id
# Optional custom folder name
GOOGLE_DRIVE_FOLDER=

TELEGRAM_BOT_TOKEN=your-telegram-bot-token
TELEGRAM_CHAT_ID=your-telegram-chat-id

2. Filesystem Configuration

Update config/filesystems.php with a new google_drive disk:

'google_drive' => [
    'driver' => 'google',
    'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
    'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
    'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
    'folder' => env('GOOGLE_DRIVE_FOLDER'),
    'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
],

๐Ÿ’ฌ Telegram Integration

Step 1: Create a Telegram Bot

  1. Open Telegram and search for @BotFather.
  2. Use /newbot and follow the prompts to create a bot.
  3. Copy the provided token and paste it into your .env as TELEGRAM_BOT_TOKEN.

Step 2: Add Bot to a Group

  1. Create a group or use an existing one.
  2. Add your bot to the group.
  3. Mention the bot once to activate it.

Step 3: Get Group Chat ID

  • Use @userinfobot or check the chat.id field via bot API messages.
  • Paste the group chat ID into your .env as TELEGRAM_CHAT_ID.

โš™๏ธ Laravel 11 Integration

Register Storage Provider

In bootstrap/app.php, register the Google Drive provider:

use AhmedWeb\LaravelBackupService\Providers\GoogleDriveStorageProvider;

return Application::configure(basePath: dirname(__DIR__))
    ->withProviders([
        GoogleDriveStorageProvider::class,
    ])
    ->create();

โฐ Scheduling Backups (Optional)

In bootstrap/app.php, add scheduled commands:

use Illuminate\Console\Scheduling\Schedule;

return Application::configure(basePath: dirname(__DIR__))
    ->withSchedule(function (Schedule $schedule) {
        $schedule->command('backup:run')->daily();
        $schedule->command('backup:clean')->daily();
        $schedule->command('backup:store-latest-link')->dailyAt('01:00');
        $schedule->command('backup:delete-old')->weekly();
    })
    ->create();

Ensure Laravel Scheduler is set in your server cron:

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

๐Ÿงช Available Artisan Commands

Command Description
backup:store-latest-link Stores and sends the latest backup download link via Telegram
backup:delete-old Deletes outdated backup files from Google Drive
backup:clean-drive Cleans up Google Drive backups according to retention rules

For Laravel 10 or below, schedule commands in app/Console/Kernel.php.

๐Ÿ“ File Structure

laravel-backup-service/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ Commands/
โ”‚   โ”‚   โ”œโ”€โ”€ StoreLatestBackupLink.php
โ”‚   โ”‚   โ”œโ”€โ”€ DeleteOldBackupFiles.php
โ”‚   โ”‚   โ””โ”€โ”€ CleanGoogleDriveBackups.php
โ”‚   โ”œโ”€โ”€ Services/
โ”‚   โ”‚   โ””โ”€โ”€ GoogleDriveBackupService.php
โ”‚   โ”œโ”€โ”€ Providers/
โ”‚   โ”‚   โ””โ”€โ”€ GoogleDriveStorageProvider.php
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ filesystems.php (optional override)

โœ… Requirements

Dependency Version
PHP ^8.2
Laravel ^11.31
spatie/laravel-backup ^9.3
google/apiclient ^2.15
irazasyed/telegram-bot-sdk ^3.15
masbug/flysystem-google-drive-ext ^2.4
yaza/laravel-google-drive-storage ^4.1

๐Ÿค Contributing

Contributions are welcome! Feel free to fork the repo, submit issues, or open pull requests for any improvements or fixes.

๐Ÿ“œ License

This package is open-sourced software licensed under the MIT license.

๐Ÿ‘จโ€๐Ÿ’ป Author

Ahmed Web ๐Ÿ“ง ahmedwry588@gmail.com ๐ŸŒ GitHub

๐Ÿ”— Resources