mercredi 20 février 2019

Unable to get backup file name spatie laravel backup

Although Spatie backup is generating zip file and I've to save the zip file name in database. So I've created BackupWasSuccessfulListener and trying to get backup filename using $event->backupDestination but it doesn't return filename or path.

Backup is saving in the following path: Storage/app/

I'm using following versions:

"php": "^7.1.3",
"laravel/framework": "5.6.*",
"spatie/laravel-backup": "^5.12",

class BackupWasSuccessfulListener {
    public function handle(BackupWasSuccessful $event) {
    $path = $event->backupDestination;
    dd($path); //See Screenshot 
    //Get file size
    // $fileSize = $this->formatSizeUnits(filesize(storage_path('app/' . $path)));

    //$backup = DatabaseBackup::create([
    //  'name' => $path,
    //  'size' => $fileSize,
    //]);

    \Log::info("Backup done successfully");

}

public function formatSizeUnits($bytes) {
    if ($bytes >= 1073741824) {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    } elseif ($bytes > 1) {
        $bytes = $bytes . ' bytes';
    } elseif ($bytes == 1) {
        $bytes = $bytes . ' byte';
    } else {
        $bytes = '0 bytes';
    }

    return $bytes;
}

This is what I get when I try to dd($event->backupDestination);

Spatie Laravel Backup BackupDestination Result

While using "spatie/laravel-backup": "3.0.0", this version I was getting the path of file using

$event->backupDestination->getNewestBackup()->path();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire