waheed43/googledrive-backup

Laravel package for backing up and managing backups on Google Drive with Telegram notifications

v1.0.7 2025-05-27 12:45 UTC

This package is auto-updated.

Last update: 2025-05-27 12:51:20 UTC


README

Here is a more professional, polished, and industry-standard version of your README.md, with enhanced formatting, clarity, and structure:

โ˜๏ธ Laravel Backup to Google Drive

A Laravel-based system to automate application file and database backups, securely store them in Google Drive, and manage backup lifecycle efficiently using Laravel's task scheduler and custom Artisan commands.

๐Ÿš€ Features

  • Automated Backups โ€“ Powered by Spatie Laravel Backup to back up files and databases.

  • Google Drive Integration โ€“ Secure cloud storage via Yaza Laravel Google Drive Storage.

  • Scheduled Tasks โ€“ Regular backups and cleanup via Laravel's task scheduler.

  • Backup Metadata โ€“ Stores file links and project identifiers in a backups database table.

  • Custom Artisan Commands:

    • backup:store-link {project} โ€“ Saves/updates the latest backup URL to the database.
    • backup:delete-old-files โ€“ Retains only the latest file on Google Drive.
  • Retention & Cleanup Policy โ€“ Automatically removes older backups based on defined rules.

  • Logging & Monitoring โ€“ Logs operations and errors for full observability.

๐Ÿงฐ Requirements

  • PHP โ‰ฅ 8.1

  • Laravel โ‰ฅ 10

  • Composer

  • MySQL (or any supported DB)

  • Google Drive API credentials:

    • Client ID & Secret
    • Refresh Token
    • Folder ID

โš™๏ธ Installation

1. Clone & Install Dependencies

git clone <repository-url>
cd <project-directory>
composer install

2. Required Packages

"masbug/flysystem-google-drive-ext": "^2.4",
"spatie/laravel-backup": "^9.3",
"yaza/laravel-google-drive-storage": "^4.1",
"google/apiclient": "^2.15.0"

3. Set Environment Variables

Copy .env.example and configure the following:

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
GOOGLE_DRIVE_FOLDER=your-folder-name

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your-database
DB_USERNAME=your-username
DB_PASSWORD=your-password

APP_NAME=your-app-name
BACKUP_ARCHIVE_PASSWORD=your-optional-password

MAIL_FROM_ADDRESS=your-email@example.com
MAIL_FROM_NAME="Your App Name"

4. Google Drive API Setup

  • Create a project in Google Cloud Console.
  • Enable the Google Drive API.
  • Generate OAuth 2.0 credentials.
  • Obtain Client ID, Client Secret, Refresh Token, and Folder ID.

5. Database & Storage Setup

php artisan migrate
php artisan storage:link

6. Configure Cron for Scheduler

Add to your server's crontab:

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

๐Ÿ”ง Configuration

๐Ÿ“ config/backup.php

  • Included paths: Entire app directory (base_path()).

  • Excluded paths: vendor, node_modules.

  • Database: Uses .env DB config.

  • Destination: Google Drive disk using ZIP format.

  • Cleanup Strategy:

    • Keep all for 7 days.
    • Daily: 16 days
    • Weekly: 8 weeks
    • Monthly: 4 months
    • Yearly: 2 years
    • Limit: 5000 MB

โ˜๏ธ config/filesystems.php

'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'),
],

๐Ÿ“ฆ Usage

Manually Trigger Backup

php artisan backup:run

Store/Update Latest Backup URL

php artisan backup:store-link sass

Delete All But Latest Backup File

php artisan backup:delete-old-files

Clean Up Based on Retention Policy

php artisan backup:clean

Monitor Logs

View logs in:

storage/logs/laravel.log

๐Ÿ—ƒ๏ธ Database Schema

Table: backups

Column Type Description
id BIGINT Auto-increment primary key
project_name VARCHAR Name of the project (nullable)
project_slug VARCHAR Unique slug for the project
file_link TEXT Backup file URL on Google Drive
created_at TIMESTAMP Record creation time
updated_at TIMESTAMP Record last update time

โฑ๏ธ Scheduled Tasks (app/Console/Kernel.php)

Task Description
backup:run Generates a fresh backup
backup:clean Cleans up old backups
backup:store-link sass Stores the latest backup link
backup:delete-old-files Deletes all but the latest backup

๐Ÿ›  Custom Components

App\Services\GoogleDriveAdapter

  • Lists files from Google Drive.
  • Gets the most recently uploaded file.
  • Deletes all files except the newest.

Artisan Commands

  • StoreLatestBackupLink: Fetches and stores latest backup link in DB.
  • DeleteOldBackupFiles: Retains only the most recent file on Google Drive.

Eloquent Model

  • App\Models\Backup

    • Fillable: project_name, project_slug, file_link

๐Ÿงช Troubleshooting

Issue Resolution
Google Drive API errors Verify .env credentials and folder access
Adapter method not found Ensure GoogleDriveAdapter includes all required methods
Backups not running Confirm cron is active and points to correct Laravel path
Storage issues Validate disk config in config/filesystems.php
Debugging Check logs: storage/logs/laravel.log

๐Ÿค Contributing

  1. Fork the repo
  2. Create a new branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m "Add feature")
  4. Push to the branch (git push origin feature/my-feature)
  5. Create a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License.

๐Ÿ™ Acknowledgments

Would you like me to update this version into your current canvas now?

backupPackage