kwenziwa/database-backup

A Laravel package to backup the database and upload to Google Drive.

1.0.0 2025-05-24 23:50 UTC

This package is auto-updated.

Last update: 2025-05-28 13:04:41 UTC


README

A Laravel package to automate database backups and upload them to Google Drive.
Seamlessly integrates with Spatie's Laravel Backup and the Google API client.
Ideal for keeping your backups safe and accessible in the cloud.

πŸš€ Features

  • 🎯 Backup your database with Spatie’s Laravel Backup
  • ☁️ Upload the latest backup automatically to Google Drive
  • πŸ”’ Fully Laravel 12+ compatible
  • πŸ”§ Easy configuration and installation

πŸ“₯ Installation

Install the package via Composer:

composer require kwenziwa/database-backup

βš™οΈ Configuration

Step 1: Publish Config

php artisan vendor:publish --tag=config

Creates config/database-backup.php.

πŸ”‘ Google API Setup

1️⃣ Create Google API Credentials

  • Go to the Google Cloud Console.
  • Navigate to Service Accounts.
  • Click Create Service Account.
  • Follow the prompts:
    • Name: e.g., Database Backup Service
    • Role: Choose Editor (or more restricted if desired).
  • After creating the service account:
    • Go to the Keys tab.
    • Click Add Key β†’ Create New Key β†’ JSON.
    • Download the JSON key file.

2️⃣ Enable Google Drive API

3️⃣ Save the Credentials File

Move the downloaded JSON key file to:

storage/app/google-drive/credentials.json

Ensure the path matches this in your .env:

GOOGLE_DRIVE_CREDENTIALS=storage/app/google-drive/credentials.json

πŸ“ Set Up Google Drive Access

4️⃣ Get Your Google Drive Folder ID

  • Create a folder in Google Drive (e.g., Database Backups).

  • Right-click the folder β†’ Get Link β†’ Copy the long ID in the URL:

    Example:

    https://drive.google.com/drive/folders/1a2B3cD4EfG5hI6Jk7LmNOPQr8TuvWXy
    

    The folder ID is:

    1a2B3cD4EfG5hI6Jk7LmNOPQr8TuvWXy
    

Add this to your .env:

GOOGLE_DRIVE_FOLDER_ID=1a2B3cD4EfG5hI6Jk7LmNOPQr8TuvWXy

5️⃣ Share Folder with Service Account

  • Open your Google Drive folder.
  • Click Share.
  • In the People field, paste the client_email from your JSON file (example below).
  • Give Editor access.

πŸ” Example credentials.json Structure

{
  "type": "service_account",
  "project_id": "your-project-id",
  "private_key_id": "your-private-key-id",
  "private_key": "-----BEGIN PRIVATE KEY-----\nABC...\n-----END PRIVATE KEY-----\n",
  "client_email": "database-backup@your-project.iam.gserviceaccount.com",
  "client_id": "1234567890",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/database-backup%40your-project.iam.gserviceaccount.com"
}

πŸš€ Usage

Backup your database and upload to Google Drive:

php artisan google-drive:backup

Test Google Drive connection:

php artisan google-drive:test

🌍 Requirements

πŸ“– Documentation

Command Description
php artisan google-drive:backup Backup database and upload to Google Drive
php artisan google-drive:test Test Google Drive connection and credentials
php artisan vendor:publish --tag=config Publish the package config file

πŸ™Œ Author

Developed by Kwenziwa Khanyile
Feel free to contribute, submit issues, or fork the repository!

πŸ“„ License

MIT Β© 2024 Kwenziwa Khanyile