Two Plus Two Newer Archives

Two Plus Two Newer Archives (http://archives1.twoplustwo.com/index.php)
-   Computer Technical Help (http://archives1.twoplustwo.com/forumdisplay.php?f=50)
-   -   shell script question (http://archives1.twoplustwo.com/showthread.php?t=545218)

B2_eBoogaloo 11-13-2007 02:53 PM

shell script question
 
trying to write a shell script to automatically backup & then delete old backups. Got the auto-backup part, but the deleting old files will go something like this:

list contents of directory in a single column (ls -1 /path/to/dir)

output looks like this:
file_2007.11.01.tgz
file_2007.11.02.tgz
file_2007.11.03.tgz
..etc

Take the date part of the filename (i.e. YYYY.MM.DD) and assign them to an array, so that I can go through the array and find the ones that are X days older than today's date. Then the script deletes the files with these dates in the name.

I know how to put current date into a variable, I just don't know how to take those files, and piecemeal them so that I can I can take action on them.

Ideas?
Buzz

Fluffy_Shark 11-13-2007 03:02 PM

Re: shell script question
 
A very easy solution would be something like:

find /path/to/dir -mtime X -delete

This should delete all files in the directory older than X days. I haven't tried it so just look it up in the find manual if there are problems.

-Fluffy_Shark

MrWookie 11-13-2007 03:08 PM

Re: shell script question
 
I guess I'm not totally clear about the problem here, but isn't what need to do here basically just

<font class="small">Code:</font><hr /><pre>#Insert some code that converts $DATESTRING into a numerical $DATE

if $DATE + $CUTOFF &lt; $TODAY; then
rm file_$DATESTRING.tgz
fi</pre><hr />

At what step in this process are you having trouble?

Edit: Or yeah, just use a built in linux function that does all the work for you.

tyler_cracker 11-13-2007 04:00 PM

Re: shell script question
 
buzz,

don't do this. there are millions of pre-written backup solutions, and they have already fixed the bugs that you will need to fix. apt-cache search and use one of those.

downrange 11-13-2007 04:23 PM

Re: shell script question
 
IMM whatever cycle you're doing this over you don't really need to do any calculating. What I mean is, if you do this every day but only want 3 days-worth of backups then sort by date and delete everything but the newest 3 copies:

/bin/rm -f dummy `/bin/ls -t &lt;filename spec&gt; | sed -e 1,3d`

Those are backticks, "N" in "Nd" controls how many copies and obviously test this out where you can do no irreparable damage.

And pick your &lt;filename spec&gt; well enough to ensure that the only thing "ls" parses is the group of files you intend it to examine.

CORed 11-13-2007 07:16 PM

Re: shell script question
 
As others have said, there are plenty of backup solutions available, but if you really want to write your own program, you would be much better off writing it in perl than writing a shell script. You can use the opendir and readdir functions to get file names and the stat function to get at the actual create or modify dates of the files. This will be much more straightforward than trying to parse dates out of ls output using sed or something like that.


All times are GMT -4. The time now is 05:30 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.