Results 1 to 6 of 6
Good All . . .
First Post so be gentle . . .
I have been using some Bash scripts for a while and they work well. One user script ...
- 06-28-2007 #1Just Joined!
- Join Date
- Jun 2007
- Posts
- 3
Yet Another Bash Question (?)
Good All . . .
First Post so be gentle . . .
I have been using some Bash scripts for a while and they work well. One user script does an Kcron incremental hourly backup of my ~/ directory to a current date folder on a backup drive and a root script does a backup of some / folders on a demand basis to the same current date directory. Both scripts clean out some garbage as well. They work very well.
So far every week or so I manually delete with konqueror a row of directories on the backup drives. I thought it was time to automate the deletion process but this looks like it is a little bit beyond my skill level. The Bash manual and the coding guides that I have looked at showed no direction for me to go in.
I need some help with the scripted deletion of old directories.
The backup daily directory looks like, i.e. "070625-Kubuntu7.04" in "YYMMDD" format. The date is created using SDATE=`date +%y%m%d`.
Is it possible to incorporate code to the end of the root script to rm -fv any directory that is say, 14 days or older? The first run will delete many. The first run of the day will delete one or two. The rest for the day will delete none.
Any examples maybe? Please?
Thank You.
- 06-28-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Welcome to the forum.
You can use find, something like:
RegardsCode:find <dir> -mtime +14 -type d -exec rm -r {} \;
- 06-29-2007 #3Just Joined!
- Join Date
- Jun 2007
- Posts
- 3
Thanks for the reply.
The directories in question have different names. Each name starts with the date it was created on such as 1st of January 2007 being '070101-Kubuntu7.04'.
I need something that creates a date that is fourteen days earlier than today - formats it as `date +%y%m%d` - adds "-Kubuntu7.04" on to it - then tests for the directory and if present deletes it then does the whole thing over with todays date minus fifteen and stops after say ten loops.
It is too complicated for me.
- 06-29-2007 #4Linux User
- Join Date
- Jun 2007
- Posts
- 318
Would this work for you:
#!/bin/bash -vx
typeset -i ct
ct=14
while [ $ct -le 24 ]
do
dir="`date --date="$ct days ago" +%y%m%d`-Kubuntu7.04"
if [ -e $dir ]; then rm -fr $dir; fi
ct=ct+1
done
- 06-29-2007 #5
I never knew about that "date -d" thing. That's pretty nifty.
However, Franklin's command does work, I think he just missed a tiny bit of explanation. The <dir> in his find command is actually the directory to start searching in. So, let's suppose that you write your backups to /home/user/backups/:
This will start in /home/user/backups/, look for any directories that are 14 days or older, and remove any such directories. This has the advantage of not wasting loops on numbers that may not exist, as well as handling cases that are even older than 24 days.Code:find /home/user/backups/ -mtime +14 -type d -exec rm -r "{}" \;
You could even make it more specific and say something like:
This way, it will only look for directories that end in 'Kubuntu7.04'.Code:find /home/user/backups/ -iname '*Kubuntu7.04' -mtime +14 -type d -exec rm -r "{}" \;
find is an incredibly powerful command, and it can often be used to entirely replace whole scripts.DISTRO=Arch
Registered Linux User #388732
- 06-29-2007 #6Just Joined!
- Join Date
- Jun 2007
- Posts
- 3
Thank You all for your replies.
This is a very lively and helpful area.
Cabhan and Franklin52 your replies would have worked except for the fact that I rename the first backup of every month by adding .keep to the filename. This ensures that when I screw up big time then I have a long term backup. I am sorry but I needed the exact filename to pick and choose the right directories.
vsemaska it worked. To test it, I substituted an echo for the rm and bumped the 24 up to 250 and it properly wrapped around the end of month and year back to 2006. I will use it.
Later on, I will modify the script to remove the .keep files that are over 4 months old. Then after some more practice, I will automatically rename the first backup of the month by adding .keep and by that time I will be back with some more questions.
Ladies and Gentleman Thank You . . .
I really appreciated your help.
Paul


Reply With Quote