tags:

views:

557

answers:

4

My webapp is hosted on a unix server using MySQL as database.

I wrote a Perl script to run backup of my database. The Perl script is inside the cgi-bin folde and it is working. I only need to set the cronjob and run the Perl script once a day.

The backups are stored in a folder named db_backups,. However, I also want to add a command inside my Perl script to remove any files inside the folder db_backups that are older than say 10 days ago.

I have searched high and low for unix commands and cannot find anything that matches what I needed.

A: 

You should be able to do it without resorting to Unix commands. Loop through the files in your directory, use stat on each file to get its last modify time for a file, then use unlink on the file to delete it if it's older than what you want.

Shaggy Frog
+4  A: 

if (-M $file > 10) { unlink $file }

or, coupled with File::Find::Rule

my $ten_days_ago = time() - 10 * 86400;
my @to_delete = File::Find::Rule->file()
  ->mtime("<=$ten_days_ago")
  ->in("/path/to/db_backup");
unlink @to_delete;
hobbs
i got this error:Can't locate object method "file" via package "File::Find::Rule" (perhaps you forgot to load "File::Find::Rule"?) at mysql_backup_job.pl line 42.
keisimone
sorry i just started learning perl for 2 days :(
keisimone
oh it worked. i just needed to add in use File::Find::Rule;
keisimone
by the way, any resources to help me to learn perl faster? i know some simple php and java. i am not very good. and most online resources i dont really can understand. i heard perl is very good for text manipulation so i wanted to learn that to automate most of the tasks i do.
keisimone
@keisimone: O'Reilly has a book called "Learning Perl"
derobert
+4  A: 

On Unix you can't, because the file's creation date is not stored in the filesystem.

You may want to check out stat, and -M (modification time)/-C (inode change time)/-A (access time) if you want a simple expression with relative timestamps (how long ago).

bart
A good point, and important to understand, but considering the usage pattern of a "backup file", mtime is as good as creation time :)
hobbs
There is also File::CreationTime, which stores some data out-of-band that lets you get a file's creation time. The creation time starts off as the mtime, but after you change the file, the creation time sticks around. (This works surprisingly well.)
jrockway
+2  A: 

i have searched high and low for unix commands and cannot find anything that matches what i needed.

Check out find(1) and xargs(1). Warning: these commands may change your life at the shell prompt.

$ find /path/to/backup -type f -mtime +10 -print0 | xargs -0 echo rm -f

When you're confident that will Do What You Want (tm), remove the echo. It says, roughly, starting in /path/to/backup, descend looking for plain files whose mtime is greater than 10 days, and print their names to xargs, which will pass those names to rm in batches.

(print0 and its complement -0 are GNU extensions -- you mentioned you were on Linux -- which let you deal with whitespace in filenames safely.)

pilcrow