Go Back   Website Publisher Forums > Website Management > General Management Issues

Notices

Reply
 
Thread Tools Rate Thread
Old 08-19-2007, 06:20 AM   #1
KLB
Site Contributor
 
KLB's Avatar
 
Join Date: Feb 2006
Location: Portland Maine
Posts: 1,185
Need cron job to delete files in folder that are older than X days

I need to know how to create a cron job script to delete files within a specific folder that are older than say seven days old. I can figure out how to schedule the cron job to run on my server, but I need a script. I'm assuming the script would probably need to be written in PHP. My webserver is Apache running on FreeBSD (UNIX).
__________________
Ken Barbalace - EnvironmentalChemistry.com (Environmental Careers, Blog)
InternetSAR.org: Volunteers Assisting Search and Rescue via the Internet
My Firefox Theme Classic Compact: Based onFirefox's classic theme but uses much less window space
KLB is offline   Reply With Quote

Old 08-19-2007, 06:28 AM   #2
Mike
Super Moderator
 
Mike's Avatar
 
Join Date: May 2003
Location: UK
Posts: 2,760
Send a message via AIM to Mike Send a message via MSN to Mike
What about the unlink() function?
__________________
Don't you just love free internet games ?
Mike is offline   Reply With Quote
Old 08-19-2007, 06:41 AM   #3
KLB
Site Contributor
 
KLB's Avatar
 
Join Date: Feb 2006
Location: Portland Maine
Posts: 1,185
unlink() would be the core of the script, but it requires the file name, so I'm assuming I need some sort of "for" loop that cycles through the files by date.
__________________
Ken Barbalace - EnvironmentalChemistry.com (Environmental Careers, Blog)
InternetSAR.org: Volunteers Assisting Search and Rescue via the Internet
My Firefox Theme Classic Compact: Based onFirefox's classic theme but uses much less window space
KLB is offline   Reply With Quote
Old 08-19-2007, 07:04 AM   #4
Chris
Administrator
 
Chris's Avatar
 
Join Date: Feb 2003
Location: East Lansing, MI USA
Posts: 6,912
Is this for perhaps log rotating or backups ken?

Here is the sh script I use for backups & rotating. It creates SQL dumps of databases listed in a file (db-list.txt) and then rotates them. It puts all the files in a daily backup directory where they're stored for 4 days, but then also copies the most recent one to a uploads folder for transfering to a backup server via FTP.

Code:
#!/bin/bash
# Set a value that we can use for a datestamp
DATE=`date +%Y-%m-%d`                                                                                                                                       $
# Our Base backup directory
BASEBACKUP="/home/backups/daily"

for DATABASE in `cat /home/backups/db-list.txt`
do
        # This is where we throw our backups.
        FILEDIR="$BASEBACKUP/$DATABASE"

        # Test to see if our backup directory exists.
        # If not, create it.
        if [ ! -d $FILEDIR ]
        then
                mkdir -p $FILEDIR
        fi

        echo -n "Exporting database:  $DATABASE"
        mysqldump --user=root --opt $DATABASE | gzip -c -9 > $FILEDIR/$DATABASE-$DATE.sql.gz
        echo "      ......[ Done Exporting to local backup, now exporting for remote backup] "
        cp $FILEDIR/$DATABASE-$DATE.sql.gz /home/backups/uploads/$DATABASE.sql.gz
        echo "      .......[Done]"
done

# AutoPrune our backups.  This will find all files
# that are "MaxFileAge" days old and delete them.
MaxFileAge=4
find $BASEBACKUP -name '*.gz' -type f -mtime +$MaxFileAge -exec rm -f {} \;
__________________
Chris Beasley - My Guide to Building a Successful Website
Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives Compost Tumbler
Chris is online now   Reply With Quote
Old 08-19-2007, 07:14 AM   #5
KLB
Site Contributor
 
KLB's Avatar
 
Join Date: Feb 2006
Location: Portland Maine
Posts: 1,185
It is for the OSS RSS parser I use called Magpie (http://magpierss.sourceforge.net) to pull my career listings. It has a nasty habit of leaving old files behind, which I didn't discover until this past week when I started having problems with my listing pages not working correctly and discovered over 4,000 cached files in my Magpie cache. The file names are randomly generated 32 character GUIDs so there is no systematic way to delete the files via file name. I need to look at their age first. I simply want to delete any file in the magpie cache that is over seven days old once a day.
__________________
Ken Barbalace - EnvironmentalChemistry.com (Environmental Careers, Blog)
InternetSAR.org: Volunteers Assisting Search and Rescue via the Internet
My Firefox Theme Classic Compact: Based onFirefox's classic theme but uses much less window space
KLB is offline   Reply With Quote
Old 08-19-2007, 07:25 AM   #6
Chris
Administrator
 
Chris's Avatar
 
Join Date: Feb 2003
Location: East Lansing, MI USA
Posts: 6,912
Well pick apart the last 4 lines from my script, the code is all there.
__________________
Chris Beasley - My Guide to Building a Successful Website
Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives Compost Tumbler
Chris is online now   Reply With Quote
Old 08-19-2007, 04:00 PM   #7
rpanella
Registered
 
Join Date: Aug 2006
Location: Sacramento, CA
Posts: 215
Code:
find /path/to/cache/files -maxdepth 1 -type f -mtime +7 -exec rm -f {} \;
Placing this line in your cron should work but make sure to set the right path so you don't delete any files you didn't mean to. This line also only deletes files from the cache directory and not an subdirectories.
__________________
Phone Lookups
rpanella is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump



All times are GMT -7. The time now is 06:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
Site Copyright © 2003-2006 Jalic Inc. All rights reserved.