Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linux noob needs help on file archive automation.

Status
Not open for further replies.

hookahmasta

IS-IT--Management
Aug 5, 2005
31
US
As a wet behind the ears admin, I need some help in terms of cycling archive files... Here's the situation...

A Cron job runs every morning that compresses our DB2 database into a archive file around 4 Gigs. It gets made, and gets copied to the NFS share of my main file server. I only need to keep copies from the previous 3 days. Is there a script that can look in the contents in that directory, find any files that are older than 3 days, and deletes them, since I don't need them anymore? If I'd placed that as a cron job, then older files that I don't need gets deleted automatically... I'm relatively new to scripting, so I need all the help I'd ned.
 
I would use at for that job.

After creation of the backup, run as last step:

Code:
at now+3days rm $BACKUP

an alternative is to use find, but I'm not used to the 'cnewer' or 'mtime' - syntax:

a) man find
b) test first
Code:
touch timestamp
find ./ -mtime +3 -maxdepth 1 -type f -name "*.backup" -exec rm {} \;

seeking a job as java-programmer in Berlin:
 
find /path/to/dir -name '*.bak' -mtime +3 -exec rm {} \;

Test first with:

find /path/to/dir -name '*.bak' -mtime +3 -exec ls -la {} \;

to ensure that you're picking up the correct files. I've included the /path/to/directory because it's safer than using . in my opinion.
 
Here's a shell script I use for everything. I'm sure there are better scripts, but this one serves it's purpose well. Just run "backup_db 3" or schedule it. See the bottom for your own personal configuration.

Mark


#!/bin/sh

# Input: backup_db n (e.g. backup_db 31)

# Check that the input is valid.
# There should be exactly 1 argument.
if [ $# -ne 1 ]; then
echo Error: $0 invalid usage.
echo Usage: $0 n
exit 1
fi

# The argument should be an integer.
n=`expr $1 + 0 2> /dev/null`
if [ $? -ne 0 ]; then
qnbad=0
elif [ $n -lt 0 ]; then
qnbad=0
else
qnbad=1
fi
if [ $qnbad -eq 0 ]; then
echo Error: n must be a positive integer.
echo Usage: $0 n
exit 1
fi

# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`
ymonth=`date +%m`
yday=`date +%d`
yyear=`date +%Y`
hour=`date +%H`
min=`date +%M`

# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`
ymonth=`expr $ymonth + 0`
# Subtrace n from the current day.
yday=`expr $yday - 0`
day=`expr $day - $n`

# While the day is less than or equal to
# 0, deincrement the month.
while [ $day -le 0 ]
do
month=`expr $month - 1`

# If month is 0 then it is Dec of last year.
if [ $month -eq 0 ]; then
year=`expr $year - 1`
month=12
fi

# Add the number of days appropriate to the
# month.
case $month in
1|3|5|7|8|10|12) day=`expr $day + 31`;;
4|6|9|11) day=`expr $day + 30`;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=`expr $day + 29`
elif [ `expr $year % 100` -eq 0 ]; then
day=`expr $day + 28`
else
day=`expr $day + 29`
fi
else
day=`expr $day + 28`
fi
;;
esac
done

# While the day is less than or equal to
# 0, deincrement the month.
while [ $yday -le 0 ]
do
ymonth=`expr $ymonth`

# If ymonth is 0 then it is Dec of last year.
if [ $ymonth -eq 0 ]; then
yyear=`expr $yyear`
ymonth=12
fi

# Add the number of days appropriate to the
# month.
case $ymonth in
1|3|5|7|8|10|12) yday=`expr $yday + 31`;;
4|6|9|11) yday=`expr $yday + 30`;;
2)
if [ `expr $yyear % 4` -eq 0 ]; then
if [ `expr $yyear % 400` -eq 0 ]; then
yday=`expr $yday + 29`
elif [ `expr $yyear % 100` -eq 0 ]; then
yday=`expr $yday + 28`
else
yday=`expr $yday + 29`
fi
else
yday=`expr $yday + 28`
fi
;;
esac
done
if [ $day -le 9 ] ; then
day="0"$day
fi
if [ $month -le 9 ] ; then
month="0"$month
fi
if [ $yday -le 9 ] ; then
yday="0"$yday
fi
if [ $ymonth -le 9 ] ; then
ymonth="0"$ymonth
fi

# make a backup directory using date
# ex. /path/to/backup/20050907
mkdir /path/to/backup/$yyear$ymonth$yday
# copy database files to it
cp /path/to/files/* /path/to/backup/$yyear$ymonth$yday

# remove old backup
rm -Rf /path/to/backup/$year$month$day
exit 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top