1752777 Members
6039 Online
108789 Solutions
New Discussion юеВ

Housekeeping task

 
Son dam bi
Advisor

Housekeeping task

In my system , there are many large files are generated to the system time by time , I would like to do the housekeeping task to archive the files to keep the server clean but still can let the user to extract the files when they required , can advise if I have below requirement

1) keep the files ( that the creation day is within 2 months ) in the system , remove the files are elder than 2 months .
2) keep all files that created on monthly end in the system .


I know tar -zcvf can achive the files and append the files to it , but is it possible also to use tar to keep the files are within 2 months ? if not , what is the best way to do the housekeeping in my case ? thx a lot




find . -type f -mtime +60 -exec tar zcvf {} file \;
7 REPLIES 7
Laurent Menase
Honored Contributor

Re: Housekeeping task

something like

#!/usr/bin/ksh
function roll
{
typeset x
name=$1
set -- $name.[0-9][0-9][0-9][0-9].gz
if [ $1 = $name'.[0-9][0-9][0-9][0-9].gz' ]
then
return 0
fi
x=${1%.gz}
typeset -i y=${x##*.}
typeset -i idx=$y
while [ $# != 0 ]
do
x=${1%.gz}
y=${x##*.}
if [[ y > idx ]]
then
idx=y
fi
shift
done
idx=idx+10001
typeset -i idx2=0
while [[ $idx != 10000 ]]
do
idx2=idx-1
n1=$name.${idx#1}.gz
n2=$name.${idx2#1}.gz
if [ -f $n2 ]
then
mv $n2 $n1
fi
idx=idx2
done
}

mkdir archive
find . \( -name archive -prune -type f \) -o -type f -mtime +60 |
while read a
do
roll archive/$a
mv $a archive.0000
gzip archive/$a.0000
done
and start this in a cron
Rita C Workman
Honored Contributor

Re: Housekeeping task

Create a mountpoint with enough disk and adjust your find statement to include full path and either cp (and later remove) or just move (mv) them to new archive mountpoint.
Here's one that looks for specific filename, owned by specific user, & over xx days old:

find /dir1/dir2 \( -name -filename* -a -user johndoe \) -mtime +60 -exec mv {} /archive1/dir2 \+

...or you can end it with \; like you have.

Just a thought,
Rgrds,
Rita



Son dam bi
Advisor

Re: Housekeeping task


thx replies ,

thx Laurent Menase 's work , but the method seems too complicate for me ..

Laurent Menase ,

your method move old file to archive dir , but can it handle the monthly file , if I want to archive the monthly file , what can i do ? thx
Laurent Menase
Honored Contributor

Re: Housekeeping task

what do you mean by archive the monthly files?

do you mean you want to keep all the files archived month/month?
Son dam bi
Advisor

Re: Housekeeping task

thx,

thx reply ,

what I want is just to keep the month end files ( eg. 31-Jan , 28-Feb , 31-Mar , 30-Apr , 31-May ... )

For simply , tar command can do this request ?

Thx
Laurent Menase
Honored Contributor

Re: Housekeeping task

can you just draw an example of what your dir hierarchy looks like, and what would be the result.. and example always worth long explanations

Son dam bi
Advisor

Re: Housekeeping task

thx reply,

if today is 29-Aug-09 , in the archive , the user can see the files from 29-Jun-09 to 29-Aug-09 and monthly files 31-Jan , 28-Feb , 31-Mar , 30-Apr , 31-May 30-


if today is 30-Aug-09 , in the archive , the user can see the files from 30-Jun-09 to 30-Aug-09 and monthly files 31-Jan , 28-Feb , 31-Mar , 30-Apr , 31-May 30-


if today is 31-Aug-09 , in the archive , the user can see the files from 1-Jun-09 to 31-Aug-09 and monthly files 31-Jan , 28-Feb , 31-Mar , 30-Apr , 31-May 30-

if today is 1-Sep-09 , in the archive , the user can see the files from 1-July-09 to 1-Sep-09 and monthly files 31-Jan , 28-Feb , 31-Mar , 30-Apr , 31-May 30-,30-Jun-09

├в
├в

if today is 1-Oct-09 , in the archive , the user can see the files from 1-Aug-09 to 1-Oct-09 and monthly files 31-Jan , 28-Feb , 31-Mar , 30-Apr , 31-May 30-,30-Jun-09 , 31-Jul-09