Web Hosting Forums

Results 1 to 4 of 4

This is a discussion on How to delete oldest file in a dir in the Hosting Talk & Chit-chat forum
I want to archive the previous 4 backup files in my backup dir and delete the oldest when making a new one, automatically of course! ...

  1. #1
    Loyal Client
    Join Date
    Feb 2002
    Posts
    144

    How to delete oldest file in a dir

    I want to archive the previous 4 backup files in my backup dir and delete the oldest when making a new one, automatically of course! Here's what I have:
    PHP Code:
    $numfile 0;
    $dir '/home/site/backups/';
    if (
    is_dir($dir)) 
    {
       if (
    $dh opendir($dir)) 
       {
           while ((
    $file readdir($dh)) !== false
           {
               if(
    is_file($file)) 
               {            
                 
    $oldest $file;
                    if (
    filemtime($file) > filemtime($oldest))
                   {
                    
    $oldest $file;
                                   }
                     if (
    $numfile >= 5)
                     {
                      
    unlink($file);
                     }
                 
    $numfile++;
             }
           }
        
    closedir($dh);
       }
    }
    //then run a new backup
    // Backup directory without trailing / 
    $backup_path '/home/site/backups; 

    echo '
    <br /><class="content">Backing up the  database....<br /><br />'; 
    $exec = system("mysqldump --add-drop-table -c -umyusername -pmypass mydb>$backup_path/backup_".$today.".sql"); 
    This doesn't delete anything though, it just keeps adding the new backup to the archive.
    Can anyone see trouble with this? Thanks

  2. #2
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    I avoided having to look at times like that by naming them day of the week instead of today, thus the new one overwrites the one a week old. Posted the script elsewhere on this board.

    Did that with a shell script though. Your script makes me wonder about permissions: might the mysqldump be run under your username and thus create files not deletable that easily from php? BTW: there's also a note on using -pmypass in that other thread.
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  3. #3
    Loyal Client
    Join Date
    Feb 2002
    Posts
    144
    Quote Originally Posted by Gwaihir
    I avoided having to look at times like that by naming them day of the week instead of today, thus the new one overwrites the one a week old. Posted the script elsewhere on this board.

    Did that with a shell script though. Your script makes me wonder about permissions: might the mysqldump be run under your username and thus create files not deletable that easily from php? BTW: there's also a note on using -pmypass in that other thread.
    You will make me wade thru your 303 posted threads to find that message...

    This is easier and seems to work:
    PHP Code:
    /*this section works but it would be better to sort the array
    using filemtime($file) if I could figure out how */
    $files = array();
    $dir '/home/mysite/backups/';
    if (
    is_dir($dir)) 
    {
       if (
    $dh opendir($dir)) 
       {
           while ((
    $file readdir($dh)) !== false 
           {
             if (
    $file != "." && $file != ".."
             {
                 
    array_push($files$file);
             }
           }
        
    closedir($dh);
       }
    }
    rsort($files);
    //this section should delete oldest files and keep the newest 5 files
    //the array should be sorted in ascending order by sort() because all
    //the files are namedlike so: 'backup-mydb_2005-06-04.sql' etc.
    $i 1;
    foreach(
    $files as $file)
    {
    if (
    $i >= 5){ unlink($dir.'/'.$file); }
     ++
    $i;
    }
    // Backup directory without trailing / 
    $backup_path '/home/mysite/backups/'

    echo 
    '<br /><p class="content">Backing up the database....<br /><br />'
    $exec system("mysqldump --add-drop-table -c -umyuser -pmypass mydb >$backup_path/backup_".$today.".sql"); 
    I don't have trouble deleting files, it seems. What's the note on using -pmypass in that other thread?
    Thanks.
    Last edited by soundser; 06-13-2005 at 05:52 AM.

  4. #4
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    Quote Originally Posted by soundser
    You will make me wade thru your 303 posted threads to find that message...
    http://forums.jaguarpc.com/showthread.php?p=81970

    Sorry; in my mind it was so alike that I thought it would be easy to find with a keyword or two. Perhaps I should not have hidden it under "cron jobs".

    The thing about the password parameter the way you use it is that it makes your password visible in the process list. Won't likely get you hacked tomorrow, but not exactly best practice either.
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •