php artisan make:command DatabaseBackup
buka file DatabaseBackup.php yang ter dapat pada App\Console\Commands
ubah menjadi:
<?php
namespace App\Console\Commands;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class DatabaseBackup extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:backup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Backup database harian';
/**
* Execute the console command.
*/
public function handle()
{
if (! Storage::exists('backup')) {
Storage::makeDirectory('backup');
}
$fileName = 'backup-' . date('Y-m-d_H-i-s') . '.sql';
$databaseName = DB::connection()->getDatabaseName();
$username = config('database.connections.mysql.username');
$password = config('database.connections.mysql.password');
$host = config('database.connections.mysql.host');
$command = "mysqldump --user={$username} --password={$password} --host={$host} {$databaseName} >" . storage_path() . "/app/backup/{$fileName}";
exec($command);
$this->info("Database exported to: {$fileName}");
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
}
}
kemudian buka bootstrap/app.php
tambahkan kode berikut:
use Illuminate\Console\Scheduling\Schedule;
->withSchedule(function (Schedule $schedule){
$schedule->command('db:backup')->daily();
})->create();
untuk lebih lengkapnya seperti ini:
<?php
return Application::configure(basePath: dirname(DIR))
->withRouting(
web: DIR.’/../routes/web.php’,
commands: DIR.’/../routes/console.php’,
health: ‘/up’,
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->withSchedule(function (Schedule $schedule){
$schedule->command(‘db:backup’)->daily();
})->create();
<?php
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->withSchedule(function (Schedule $schedule){
$schedule->command('db:backup')->daily();
})->create();
kemudian untuk menjalankannya ketik di terminal:
php artisan db:backup
atau bisa juga seperti:
<?php
namespace Dbsafeguard\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
class BackupDatabase extends Command
{
protected $signature = 'backup:db';
protected $description = 'Backup the database';
public function handle(): void
{
$database = env('DB_DATABASE');
$username = env('DB_USERNAME');
$password = env('DB_PASSWORD');
$backupPath = $this->ask('Enter the backup path (default: storage/app/backups)', 'storage/app/backups');
File::ensureDirectoryExists($backupPath);
$backupFilePath = $backupPath . '/' . date('Y-m-d_H-i-s') . '_backup.sql';
$command = "mysqldump -u{$username} -p{$password} {$database} > {$backupFilePath}";
exec($command);
$this->info('Database backup completed successfully.');
}
}
cara menggunkannya
php artisan backup:db
