Delete Old Backups

Post your support requests/solutions for phpMySQLAutoBackup [website]
HillClimb

Delete Old Backups

Postby HillClimb » Mon Dec 05, 2011 11:08 pm

Software is useful, especially running daily/weekly/monthly via Cron. I prefer to have a number of recent backups, just in case, but the number of files and space utilisation can become a problem, so the following code can be used to delete old files

Add the following function to /files/phpmysqlauto_extras.php

Code: Select all

function delete_old_backups()
{
 global $db;
 $files=$keep=array();
 $prefix = 'mysql_'.$db;
 $suffix = ".sql.gz";
 $dir = LOCATION."../backups/";
 if ($handle = opendir($dir))
  {
   while (false !== ($file = readdir($handle)))
    {
     if ((filetype($dir.$file) == "file") && (substr($file,0,strlen($prefix)) == $prefix) && (substr($file,-strlen($suffix)) == $suffix) && (filesize($dir.$file)>0))
      {
       $files[filemtime($dir.$file)]= $file;       
      }
     }
    closedir($handle);
    krsort($files);
    $slice = min(NO_OF_BACKUPS,sizeof($files));
    if ($slice)
     {
      $erase = array_slice($files,NO_OF_BACKUPS);
      foreach ($erase as $key=>$thisOne)
       {
        unlink($dir.$thisOne);
       }
     } 
  }
}


Then also in this script, amend the "function write_backup($gzdata, $backup_file_name)" function by inserting right at the end of the function

Code: Select all

 return delete_old_backups();


Lastly, to make the number of files kept a configuration parameter,then in /run.php replace

Code: Select all

$save_backup_zip_file_to_server = 1; // if set to 1 then the backup files will be saved in the folder: /phpMySQLAutoBackup/backups/
                                    //(you must also chmod this folder for write access to allow for file creation)

with

Code: Select all

$save_backup_zip_file_to_server = 1; // if set to 1 then the backup files will be saved in the folder: /phpMySQLAutoBackup/backups/
                                    //(you must also chmod this folder for write access to allow for file creation)
define('NO_OF_BACKUPS',10);//the total number of backups files to retain, e.g. 10. the 10 most recent based the files mtime (modified date) are kept, older versions are deleted

Hope this helps some of you and perhaps make it into a future version

davidgwalker
MODS thats what I do that is! But definately NO mod music! It has to be ROCK!
Posts: 1807
Joined: Mon Oct 25, 2004 8:20 pm
Location: Huddersfield, UK
Contact:

Re: Delete Old Backups

Postby davidgwalker » Tue Dec 06, 2011 8:08 am

Hi and welcome to the forums.

This is a really good addition and will, I am sure, help others.

I have added it for inclusion in the next update which will be next year sometime.

Many thanks for your contribution, it is much appreciated

Cheers

Dave

davidgwalker
MODS thats what I do that is! But definately NO mod music! It has to be ROCK!
Posts: 1807
Joined: Mon Oct 25, 2004 8:20 pm
Location: Huddersfield, UK
Contact:

Re: Delete Old Backups

Postby davidgwalker » Thu Dec 15, 2011 8:26 pm

This is now integrated within the new version 1.6.0, which will be released in a couple of days.


Return to “phpMySQLAutoBackup [forum]”

Who is online

Users browsing this forum: No registered users and 2 guests