免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2338 | 回复: 8
打印 上一主题 下一主题

[新手入门] 请教一个shell脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-11 13:31 |只看该作者 |倒序浏览
有一个目录/log1下,每天有好多个新日志文件产生,格式为:YYYYMMDD000***.log,几年来积累了无数文件。
现在想要把以前的这些文件整理一下,按照每天生成一个包并压缩成*.Z的格式文件,移到另外一个目录/log2下,/log1下面只保留7天内的压缩文件包和当天新生成的日志文件。
请帮忙看脚本应该如何写?

论坛徽章:
0
2 [报告]
发表于 2007-06-11 16:11 |只看该作者
#!/bin/ksh
id=`date +%m%d`
cd /log1
find . -mtime +7 -name *.log|tar cvf $id.tar *
gzip $id.tar
mv $id.tar.gz /log2

然后在cron里边每天的晚上执行一次
思路应该有了吧  
写的不好
期待高手

[ 本帖最后由 television 于 2007-6-11 16:16 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2007-06-12 11:13 |只看该作者
原帖由 television 于 2007-6-11 16:11 发表
#!/bin/ksh
id=`date +%m%d`
cd /log1
find . -mtime +7 -name *.log|tar cvf $id.tar *
gzip $id.tar
mv $id.tar.gz /log2

然后在cron里边每天的晚上执行一次
思路应该有了吧  
写的不好
期待高手  ...


你测试过吗?我觉得你脚本没有达到人家的目的,而且本身应该也有错误,应该执行不过去.

论坛徽章:
0
4 [报告]
发表于 2007-06-12 12:00 |只看该作者
我觉得这个最好是今天的零点来tar昨天的日志,同时移走七天前的压缩包,我写了个脚本,请参考:
 # step 1, generate application statistics at the morning for yesterday
get_one_day_before_specified_date()
{
#get the command line input(date month & year)
day=$1
month=$2
year=$3

# if it is the first day of the month
if [ $day -eq 01 ]
then
# if it is the first month of the year
if [ $month -eq 01 ]
then
# make the month as 12
month=12

# deduct the year by one
year=`expr $year - 1`
else
# deduct the month by one
month=`expr $month - 1`
fi
day=`cal $month $year | awk 'NF != 0{ last = $0 }; END{ print last }' | awk '{ print $NF }'`
else
# deduct the day by one
day=`expr $day - 1`
fi
echo $year$month$day
}

YEAR_T=`date +%Y`;
MONTH_T=`date +%m`;
DAY_T=`date +%d`;
YESTERDAY=`get_one_day_before_specified_date $DAY_T $MONTH_T $YEAR_T`;

DATE=$YESTERDAY
LOGDIR1=/log1/
LOGDIR2=/log2/

#first backup all log
/usr/bin/tar cvf ${LOGDIR1}${DATE}.tar  ${LOGDIR1}${DATE}*.log  > /dev/null
/usr/bin/gzip ${LOGDIR1}${DATE}.tar

#second move the package which before the time
cd $LOGDIR1
rm  -f  ${DATE}*.log

for i in `find  . -mtime +7  -name '*.tar.gz'`
do
cp  $i  $LOGDIR2
rm -f $i
done


在cron里每天零晨10分执行.

[ 本帖最后由 我老婆黑社会 于 2007-6-12 12:11 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2007-06-12 12:40 |只看该作者


没看清题意

原来他那里早就有log了



哪里有问题
请指出

[ 本帖最后由 television 于 2007-6-12 12:52 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-06-12 13:41 |只看该作者
find . -mtime +7 -name *.log    需要*.log 加引号 '*.log'
find . -mtime +7 -name *.log|tar cvf $id.tar *  作用是什么?我想应该不能达到你的目的

论坛徽章:
0
7 [报告]
发表于 2007-06-12 13:50 |只看该作者
好像都有问题

论坛徽章:
0
8 [报告]
发表于 2007-06-14 10:13 |只看该作者
用这个试试看:


#!/usr/bin/ksh
log_directory=/log1
backup_directory=/log2                                            
backup_file_list=""                                     #备份日志列表
file_prefix=${backup_date_long}000
backup_file_prefix=${backup_date_long}
days_for_keep=7                               #打包N天前的日志
#通过调整时区来获得N天前的日期

(( hours_ago=24*${days_for_keep} ))                     #取N天前的日期
     
tz_diff=`echo $TZ | tr -d 'A-Z'`
tz_pre=`echo $TZ| tr -d '[:digit:]'|cut -f1 -d-`
tz_post=`echo $TZ| tr -d '[:digit:]'|cut -f2 -d-`
((tz_diff=${hours_ago}+${tz_diff}))
n_days_ago_tz=${tz_pre}${tz_diff}${tz_post}
     
old_tz=$TZ
TZ=${n_days_ago_tz}
backup_date_long=`date '+%Y%m%d'`                        #以YYYYMMDD格式取得N天前系统日期     
backup_date_short=`date '+%m%d'`                         #以MMDD格式取得N天前系统日期     
TZ=${old_tz}

cd ${log_directory}

  #检查要备份的日志是否存在
  if [ -d ${file_prefix}??? ]
  then
    backup_file_list=${file_prefix}???.log" "
  fi

if [ -z "${backup_file_list}" ]
then
  print "No Log Left!"
  exit 1
fi

tar cfp - ${backup_file_list} \
       2>${backup_directory}/${backup_file_prefix}backup_tar.log \
| compress > ${backup_directory}/${backup_file_prefix}${backup_date_long}.tar.Z \
   2>${backup_directory}/${backup_file_prefix}backup_compress.log

#判断打包是否成功,成功则删除日志文件
if [  -s ${backup_directory}/${backup_file_prefix}backup_tar.log \
   -o -s ${backup_directory}/${backup_file_prefix}backup_compress.log \
   -o $? = "1" ]
then
  print "${backup_file_prefix} Backup Failure! "
  exit 1
else
  print "${backup_file_prefix} Backup O.K.! "
  rm -rf ${backup_file_list}
fi

论坛徽章:
0
9 [报告]
发表于 2007-06-14 15:53 |只看该作者
学习啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP