0

I have set up a Scheduled Backup (Database and Media) which works fine. However, it's done every day and I now have 72 of them!

I only need to keep the last 7 days worth. Is there any way of deleting the older backups? And is there a way of automatically limiting the number of backups kept?

1

yes you can survive your files with my custom function.

public function deleteBackupFiles() {

        $filelist = array();
        if ($handle = opendir(Mage::getBaseDir('var') . DS . "backups" . DS)) {
            while ($entry = readdir($handle)) {
                //if ($this->endsWith($entry, "_db.gz")) {            
                if ($this->endsWith($entry, "nomedia.tgz")) {
                    $filelist[] = $entry;
                }
            }
            closedir($handle);
        }

        foreach ($filelist as $each_file) {
            /* echo "<br> $filename was last modified: " . date ("Y-m-d h:i:s", filemtime(Mage::getBaseDir('var') . DS . "backups" . DS.$each_file));

              $last_modi = filemtime(Mage::getBaseDir('var') . DS . "backups" . DS.$each_file); */
            if (time() - filemtime(Mage::getBaseDir('var') . DS . "backups" . DS . $each_file) >= 7 * 86400) { // where 7 is days
                unlink(Mage::getBaseDir("var") . DS . "backups" . DS . $each_file);
            }
        }
    }

Edit

cron tab should look like

  <crontab>
        <jobs>
            <your_custom_tag_backup_files>
                <schedule>
                    <cron_expr>0 4 * * *</cron_expr>
                </schedule>
                <run>
                    <model>yourmodel/observer::deleteBackupFiles</model>
                </run>
            </your_custom_tag_backup_files> 
          </jobs>
    </crontab>

Add this function to your custom model / observer and run it with cron expression.

let me know if i could help you more

5
  • Looks great. Don't know what my 'custom model / observer' is, or how to run it with a cron expression. – Phil Stewart Mar 16 '15 at 9:57
  • @PhilStewart, i have added cron tab just add this to your config.xml file so it will delete all back files which is older then 7 days at 4 am – liyakat Mar 16 '15 at 10:17
  • Great. The cron looks straightforward - but what do I do with the function? – Phil Stewart Mar 16 '15 at 10:51
  • put it in some model and change yourmodel in cron tab to define model – liyakat Mar 16 '15 at 12:42
  • Sorry, I don't understand... How do I put it in 'some model'? – Phil Stewart Mar 17 '15 at 16:04
0

You may also try to write a shell script, like shown here: https://superuser.com/questions/268344/how-do-i-delete-all-but-10-newest-files-in-linux (just adapt the number of days)

After that, setup a cron job outside of Magento and run it every day. Might also work.

Regards

0

If you have shell access.

find var/backups -type f -mtime +14 -delete

Removes all files older than 14 days from within the backup directory.

You could easily convert it into a script to run as a cronjob outside Magento.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.