How to save just 5 recent backups in Spatie Laravel backup?

975 views Asked by At

I have configured Spatie/Laravel backup in my application, and i have croned job for this on which it takes backup everyday automatically. But can I save just recent 5 or 10 backups of system? If new backup created then the oldest one should be deleted! Here is my clean-up strategy of backup configuration file:

'cleanup' => [
        /*
         * The strategy that will be used to cleanup old backups. The default strategy
         * will keep all backups for a certain amount of days. After that period only
         * a daily backup will be kept. After that period only weekly backups will
         * be kept and so on.
         *
         * No matter how you configure it the default strategy will never
         * delete the newest backup.
         */
        'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class,

        'default_strategy' => [

            /*
             * The number of days for which backups must be kept.
             */
            'keep_all_backups_for_days' => 7,

            /*
             * The number of days for which daily backups must be kept.
             */
            'keep_daily_backups_for_days' => 16,

            /*
             * The number of weeks for which one weekly backup must be kept.
             */
            'keep_weekly_backups_for_weeks' => 8,

            /*
             * The number of months for which one monthly backup must be kept.
             */
            'keep_monthly_backups_for_months' => 4,

            /*
             * The number of years for which one yearly backup must be kept.
             */
            'keep_yearly_backups_for_years' => 2,

            /*
             * After cleaning up the backups remove the oldest backup until
             * this amount of megabytes has been reached.
             */
            'delete_oldest_backups_when_using_more_megabytes_than' => 5000,
        ],
    ],

This is the function which create backup!

 public function create()
    {
        try {
            // start the backup process
            \Illuminate\Support\Facades\Artisan::call('backup:run');
            $output = \Illuminate\Support\Facades\Artisan::output();
            // log the results
            Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n" . $output);
            // return the results as a response to the ajax call
            return redirect('/backups')->with('message', 'Backup Created Successfully!')->with('alertType', 'alert-success');
        } catch (Exception $e) {
            return redirect('/backups')->with('message', 'Backup Failed, Try again')->with('alertType', 'alert-warning');
        }
    }
1

There are 1 answers

0
Adam On

If you do one backup a day, and want to just keep the latest 10, this would be your strategy:

 'default_strategy' => [

        /*
         * The number of days for which backups must be kept.
         */
        'keep_all_backups_for_days' => 10,

        /*
         * The number of days for which daily backups must be kept.
         */
        'keep_daily_backups_for_days' => 0,

        /*
         * The number of weeks for which one weekly backup must be kept.
         */
        'keep_weekly_backups_for_weeks' => 0,

        /*
         * The number of months for which one monthly backup must be kept.
         */
        'keep_monthly_backups_for_months' => 0,

        /*
         * The number of years for which one yearly backup must be kept.
         */
        'keep_yearly_backups_for_years' => 0,

        /*
         * After cleaning up the backups remove the oldest backup until
         * this amount of megabytes has been reached.
         */
        'delete_oldest_backups_when_using_more_megabytes_than' => 5000,
    ],
],

If you do multiple backups every day, but just want to keep one backup per day (of the last 10 days) your strategy would be this:

 'default_strategy' => [

        /*
         * The number of days for which backups must be kept.
         */
        'keep_all_backups_for_days' => 1, // Every backup should exists 10 days, then it can be removed

        /*
         * The number of days for which daily backups must be kept.
         */
        'keep_daily_backups_for_days' => 9,

        /*
         * The number of weeks for which one weekly backup must be kept.
         */
        'keep_weekly_backups_for_weeks' => 0,

        /*
         * The number of months for which one monthly backup must be kept.
         */
        'keep_monthly_backups_for_months' => 0,

        /*
         * The number of years for which one yearly backup must be kept.
         */
        'keep_yearly_backups_for_years' => 0,

        /*
         * After cleaning up the backups remove the oldest backup until
         * this amount of megabytes has been reached.
         */
        'delete_oldest_backups_when_using_more_megabytes_than' => 5000,
    ],
],