waheed43 / googledrive-backup
Laravel package for backing up and managing backups on Google Drive with Telegram notifications
Requires
- php: ^8.2
- google/apiclient: ^2.15.0
- irazasyed/telegram-bot-sdk: ^3.15
- laravel/framework: ^11.31
- laravel/sanctum: ^4.0
- laravel/tinker: ^2.9
- masbug/flysystem-google-drive-ext: ^2.4
- spatie/laravel-backup: ^9.3
- waheed43/googledrive-backup: ^1.0.5
- yaza/laravel-google-drive-storage: ^4.1
Requires (Dev)
- fakerphp/faker: ^1.23
- laravel/pail: ^1.1
- laravel/pint: ^1.13
- laravel/sail: ^1.26
- mockery/mockery: ^1.6
- nunomaduro/collision: ^8.1
- phpunit/phpunit: ^11.0.1
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
, andFolder 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
- Fillable:
๐งช 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
- Fork the repo
- Create a new branch (
git checkout -b feature/my-feature
) - Commit your changes (
git commit -m "Add feature"
) - Push to the branch (
git push origin feature/my-feature
) - 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?