Two Plus Two Newer Archives  

Go Back   Two Plus Two Newer Archives > Other Topics > Computer Technical Help
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-13-2007, 02:53 PM
B2_eBoogaloo B2_eBoogaloo is offline
Senior Member
 
Join Date: Oct 2007
Location: procrastinating 7K
Posts: 187
Default 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
Reply With Quote
  #2  
Old 11-13-2007, 03:02 PM
Fluffy_Shark Fluffy_Shark is offline
Member
 
Join Date: Aug 2007
Posts: 56
Default 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
Reply With Quote
  #3  
Old 11-13-2007, 03:08 PM
MrWookie MrWookie is offline
Senior Member
 
Join Date: Feb 2005
Location: Treating my drinking problem
Posts: 17,411
Default 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.
Reply With Quote
  #4  
Old 11-13-2007, 04:00 PM
tyler_cracker tyler_cracker is offline
Senior Member
 
Join Date: Apr 2005
Posts: 4,661
Default 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.
Reply With Quote
  #5  
Old 11-13-2007, 04:23 PM
downrange downrange is offline
Senior Member
 
Join Date: Feb 2007
Posts: 157
Default 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.
Reply With Quote
  #6  
Old 11-13-2007, 07:16 PM
CORed CORed is offline
Senior Member
 
Join Date: Sep 2002
Posts: 4,798
Default 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.
Reply With Quote
Reply


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 On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:40 PM.


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