app/Console/Commands/MongoDump.php
/**
     * Execute the console command.
     *
     * @return mixed
     */
Execute the console command.
@return mixed
Execute the console command . @return mixed
public function handle()
    {
        $backupFolder = 'backups_mongodb';
        if (!is_dir($backupFolder)) {
            mkdir($backupFolder, 0755, true);
        }
        $command = 'mongodump -d order -o "backups_mongodb/'. date('Y-m-d') . '_' . time() . '_bak' .'"';
        exec($command);
        $fi = new \FilesystemIterator($backupFolder, \FilesystemIterator::SKIP_DOTS);
        $backupsCount = iterator_count($fi);
        $maxBackupCount = 5;
        function deleteDir($path) {
            return is_file($path) ?
                @unlink($path) :
                array_map(__FUNCTION__, glob($path.'/*')) == @rmdir($path);
        }
        if($backupsCount > $maxBackupCount) {
            $files = scandir ($backupFolder);
            deleteDir($backupFolder. '/'.$files[2]);
        }
    }
public function handle ( ) { $ backupFolder = 'backups_mongodb' ; if ( ! is_dir ( $ backupFolder ) ) { mkdir ( $ backupFolder , 0755 , true ) ; } $ command = 'mongodump -d order -o "backups_mongodb/' . date ( 'Y-m-d' ) . '_' . time ( ) . '_bak' . '"' ; exec ( $ command ) ; $ fi = new \ FilesystemIterator ( $ backupFolder , \ FilesystemIterator :: SKIP_DOTS ) ; $ backupsCount = iterator_count ( $ fi ) ; $ maxBackupCount = 5 ; function deleteDir ( $ path ) { return is_file ( $ path ) ? @ unlink ( $ path ) : array_map ( __FUNCTION__ , glob ( $ path . '/*' ) ) == @ rmdir ( $ path ) ; } if ( $ backupsCount > $ maxBackupCount ) { $ files = scandir ( $ backupFolder ) ; deleteDir ( $ backupFolder . '/' . $ files [ 2 ] ) ; } }
Execute the console command.
Execute the console command .
// command to restore previous dump // mongorestore -d confsite --drop backups_mongodb/2017-05-29_1496056584_bak/confsite/ // 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