Last active
March 20, 2024 13:23
-
-
Save tarikmanoar/dec4da582b70e17fb3956c3de104a4ca to your computer and use it in GitHub Desktop.
Laravel 10 Google Drive Backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For generating refresh token | |
https://developers.google.com/oauthplayground | |
for any help | |
https://www.youtube.com/watch?v=OPDkQ3V4Goo&ab_channel=ImranicShow | |
composer require spatie/laravel-backup [docs: https://spatie.be/docs/laravel-backup/v8/installation-and-setup] | |
eta dia config file publish korben | |
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider" | |
composer require masbug/flysystem-google-drive-ext | |
ekta Provider banano lagbe Google Drive er adapter er jonno | |
php artisan make:provider GoogleDriveServiceProvider | |
GoogleDriveServiceProvider.php | |
``` | |
<?php | |
namespace App\Providers; | |
use Illuminate\Filesystem\FilesystemAdapter; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\ServiceProvider; | |
use League\Flysystem\Filesystem; | |
class GoogleDriveServiceProvider extends ServiceProvider { | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() { | |
Storage::extend( 'google', function ( $app, $config ) { | |
$client = new \Google_Client(); | |
$client->setClientId( $config['clientId'] ); | |
$client->setClientSecret( $config['clientSecret'] ); | |
$client->refreshToken( $config['refreshToken'] ); | |
$client->fetchAccessTokenWithRefreshToken( $config['refreshToken'] ); | |
$service = new \Google_Service_Drive( $client ); | |
$options = []; | |
if ( isset( $config['teamDriveId'] ) ) { | |
$options['teamDriveId'] = $config['teamDriveId']; | |
} | |
$adapter = new \Masbug\Flysystem\GoogleDriveAdapter( $service, $config['folder'] ?? '/', $options ); | |
$driver = new Filesystem( $adapter ); | |
return new FilesystemAdapter( $driver, $adapter ); | |
} ); | |
} | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() { | |
// | |
} | |
} | |
``` | |
then ei provider ta config/app.php te add korben. | |
'providers' => [ | |
... | |
App\Providers\GoogleDriveServiceProvider::class, | |
] | |
ekhane $config array ta ashe config/filesystems.php theke. Tai config/filesystems.php e apnar ekta disk create kora lagbe (virtually disk chinta korte paren). | |
'disks' => [ | |
... | |
'backup-google' => [ | |
'driver' => 'google', | |
'clientId' => env( 'GOOGLE_DRIVE_CLIENT_ID' ), | |
'clientSecret' => env( 'GOOGLE_DRIVE_CLIENT_SECRET' ), | |
'refreshToken' => env( 'GOOGLE_DRIVE_REFRESH_TOKEN' ), | |
'folder' => env( 'GOOGLE_DRIVE_BACKUP_FOLDER_NAME' ), | |
], | |
] | |
ei value gula e GoogleDriveServiceProvider e use hoy. ar ei value ashe env file theke. tai .env teo ei value add kora lagbe, ar .env change korlei (php artisan config:cache) | |
GOOGLE_DRIVE_CLIENT_ID= | |
GOOGLE_DRIVE_CLIENT_SECRET= | |
GOOGLE_DRIVE_REFRESH_TOKEN="refresh token ta emne quotation mark er moddhe diyen, naile hoyto kaj korbe na" | |
GOOGLE_DRIVE_BACKUP_FOLDER_NAME="backups" [ete kore google drive e backups folder e apnar backup jabe shob] | |
GOOGLE_DRIVE_BACKUP_FOLDER_ID="ekhane folder id diben, oita paben folder er url theke https://drive.google.com/drive/u/0/folders/EITA_APNAR_FOLDER_ID" | |
ekhon apne oi spatie/laravel-backup package e set korben ei google drive er disk e backup save korte. package install er pore je command disen oitar karone config/backup.php ekta file create hoise oikhane set korben ei disk use korte | |
'backup' => [ | |
'destination' => [ | |
'disks' => [ | |
'backup-google', // same name as the disk in filesystems.php | |
], | |
] | |
] | |
ekhon apne locally ei command dile apnar backup Google Drive e jawar kotha | |
php artisan backup:run --only-db --disable-notifications | |
VPS er jonno kichu kaj ekta kora lage. | |
Auto backup er jonno apnar laravel project e console command schedule kora lagbe | |
app/Console/Kernel.php | |
``` | |
<?php | |
namespace App\Console; | |
class Kernel extends ConsoleKernel | |
{ | |
/** | |
* Define the application's command schedule. | |
* | |
* @param \Illuminate\Console\Scheduling\Schedule $schedule | |
* @return void | |
*/ | |
protected function schedule(Schedule $schedule) | |
{ | |
$schedule->command('backup:run --only-db --disable-notifications')->hourly(); | |
} | |
/** | |
* Register the commands for the application. | |
* | |
* @return void | |
*/ | |
protected function commands() | |
{ | |
$this->load(__DIR__.'/Commands'); | |
require base_path('routes/console.php'); | |
} | |
} | |
``` | |
then server e emon cronjob add korben (search some video/article) | |
* * * * * cd /home/user/web/domain.com/public_html && /usr/bin/php artisan schedule:run >> /dev/null 2>&1 | |
Erpor ekta error khawar chance ase, normally ei cron job apnar .env file er DB user/password use kore na. oita arekta jagay dite hoy. cronjob oikhan theke nei. eta debian er ta dei. apnar OS e eta onno rokom hoite pare. ei 5 ta jaygay thakte pare. | |
1. /etc/my.cnf | |
2. /etc/mysql/my.cnf | |
3. $MYSQL_HOME/my.cnf | |
4. [datadir]/my.cnf | |
5. ~/.my.cnf | |
ami #5 e paisi amar default password. ei default password er karon db-dump hobe na. ekahne apnar DB er password diben | |
[client] | |
password='db_password' | |
ekhon VPS server e project directory te giye | |
php artisan backup:run --only-db --disable-notifications | |
dile run howar kotha |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment