因此,在脚本编写方面,我是一个完全的新手,这是我的第一个。我创建了以下脚本:
# This simply declares which states we are using to create tarballs from
for blah in ar ky ms ny;
do
# This will go into each tomcat-state directory and create a tarball in /home/ec-user for all files older than 90 days
find /var/www/apps/tomcat-${blah}/logs -type f -mtime +90 | xargs tar -cvzf /home/ec2-user/archive-${blah}.tar.gz;
# This creates a master archive tarball containing all the archive-state.tar.gz files
tar -cvzf /home/ec2-user/ebd-log-archives.tar.gz /home/ec2-user/archive-${blah}.tar;
# Since in step one, we archives all files older than 90 days, this step removes them.
# find /var/www/apps/tomcat-${blah}/logs -type f -mtime +90 -exec rm {} \;
# And since we have the master archive file, this removes the archive-state files
rm /home/ec2-user/archive-${blah}.tar.gz;
done
我已注释掉要测试的日志的删除。
问题是,当我获得主tarball时,其中仅包含NY,或者循环中我放置的最后一个(我对此进行了测试,以确保它不仅仅是一个奇怪的NY东西)
不知道发生了什么,我在服务器上用类似的旧脚本对它进行了测试,并且该脚本运行良好。
编辑:感谢您的答复,我已将脚本更新为:
# This creates the master tarball we will be using
tar -cvzf /home/ec2-user/ebd-log-archives.tar.gz
# This simply declares which states we are using to create tarballs from
for blah in ar ky ms ny;
do
# This will go into each tomcat-state directory and create a tarball in /home/ec-user for all files older than 90 days
find /var/www/apps/tomcat-${blah}/logs -type f -mtime +90 | xargs tar -cvzf /home/ec2-user/archive-${blah}.tar.gz;
# This creates a master archive tarball containing all the archive-state.tar.gz files
tar -rvzf /home/ec2-user/ebd-log-archives.tar.gz /home/ec2-user/archive-${blah}.tar;
# Since in step one, we archives all files older than 90 days, this step removes them.
# find /var/www/apps/tomcat-${blah}/logs -type f -mtime +90 -exec rm {} \;
# And since we have the master archive file, this removes the archive-state files
rm /home/ec2-user/archive-${blah}.tar.gz;
done
我将尝试与此运行并发布更新
每次运行时tar -c
,该工具都会创建一个新的存档。如果您告诉它使用现有名称(ebd-log-archives.tar.gz
在您的情况下),则旧存档将被覆盖。
您可以在循环之前创建一个空的归档文件(“主归档文件”),然后在循环内部创建tar -r
(而不是tar -c
)。
来自man 1 tar
:
-c
,--create
创建一个新的存档。[…][…]
-r
,--append
将文件追加到归档文件的末尾。参数与-c
(--create
)具有相同的含义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句