kwenziwa / database-backup
A Laravel package to backup the database and upload to Google Drive.
Requires
- php: ^8.2
- google/apiclient: ^2.0
- spatie/laravel-backup: ^9.2.8
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).
- Name: e.g.,
- 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
- Visit the Google Drive API page and click Enable.
- For detailed step-by-step instructions, see this guide:
How to Enable Google Drive API on the Google Console
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
- PHP 8.2+
- Laravel 10, 11, or 12
- Spatie Laravel Backup v9.2.8+
- Google API Client
π 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