app/Console/Commands/MysqlDump.php
/**
     * Execute the console command.
     *
     * @return mixed
     */
Execute the console command.
@return mixed
Execute the console command . @return mixed
public function handle()
    {
        $db = env('DB_DATABASE', 'c9');
        $user = env('DB_USERNAME', 'dannycao');
        $pass = env('DB_PASSWORD');
        $backupFolder = 'backups';
        $file = 'backups/' . date('Y-m-d') . '-' . time() . 'backup.sql';
        $command = 'mysqldump -u '.$user.' '.$db.' > ' . $file;
        if (!is_dir('backups')) {
            mkdir('backups', 0755, true);
        }
        exec($command);
          $fi = new \FilesystemIterator($backupFolder, \FilesystemIterator::SKIP_DOTS);
        $backupsCount = iterator_count($fi);
        $maxBackupCount = 5;
        if($backupsCount > $maxBackupCount) {
            $files = scandir ($backupFolder);
            unlink($backupFolder. '/'.$files[2]);
        }
    }
public function handle ( ) { $ db = env ( 'DB_DATABASE' , 'c9' ) ; $ user = env ( 'DB_USERNAME' , 'dannycao' ) ; $ pass = env ( 'DB_PASSWORD' ) ; $ backupFolder = 'backups' ; $ file = 'backups/' . date ( 'Y-m-d' ) . '-' . time ( ) . 'backup.sql' ; $ command = 'mysqldump -u ' . $ user . ' ' . $ db . ' > ' . $ file ; if ( ! is_dir ( 'backups' ) ) { mkdir ( 'backups' , 0755 , true ) ; } exec ( $ command ) ; $ fi = new \ FilesystemIterator ( $ backupFolder , \ FilesystemIterator :: SKIP_DOTS ) ; $ backupsCount = iterator_count ( $ fi ) ; $ maxBackupCount = 5 ; if ( $ backupsCount > $ maxBackupCount ) { $ files = scandir ( $ backupFolder ) ; unlink ( $ backupFolder . '/' . $ files [ 2 ] ) ; } }
Execute the console command.
Execute the console command .
// delete the first file from a directory // run every ? 5 minutes, after 288 files (24 hours), delete the first backup // to conserve disk space // i put $maxBackupCount to 5 to demonstrate the proces of deleting the first entry