bash 复制 / Tarring 过去 14 天内修改过的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9981570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying / Tarring Files that have been modified in the last 14 days
提问by Sarfaraz Jamal
I have a server whose files are modified every so now and again.
我有一个服务器,它的文件时不时地被修改。
We want to have a script or cron job that runs every 7 days and finds any php files that have been modified or created in the last 14 days and puts them in a tar or a zip file on the server so it can be downloaded.
我们希望有一个脚本或 cron 作业,每 7 天运行一次,查找过去 14 天内修改或创建的任何 php 文件,并将它们放在服务器上的 tar 或 zip 文件中,以便可以下载。
This command finds the right files:
此命令查找正确的文件:
find . -name "*.php" -mtime -14 -print
What else do I need to do?
我还需要做什么?
回答by wisent
if your list of files from find output is correct just pipe it to tar:
如果 find 输出中的文件列表正确,只需将其通过管道传输到 tar:
find . -name "*.php" -mtime -14 -print | xargs tar cvf backup.tar
You should check tar options in man. You might want use for example -p (preserve permissions), just look for useful options in man and use whatever you need.
您应该检查 man 中的 tar 选项。您可能想要使用例如 -p(保留权限),只需在 man 中查找有用的选项并使用您需要的任何内容。
and to add it to cron, the simplest way if your distro supports it, is to put your script in:
并将其添加到 cron,如果您的发行版支持它,最简单的方法是将您的脚本放入:
/etc/cron.weekly
otherwise you have to modfiy crontab:
否则你必须修改 crontab:
crontab -e
and put there a line like:
并在那里放一行:
0 3 * * 6 <user> <your script>
it runs a script at 3am every saturday, the last script is day of the week, 0 or 7 is sunday.
它在每个星期六凌晨 3 点运行一个脚本,最后一个脚本是星期几,0 或 7 是星期日。
man 5 crontab:
男人 5 crontab:
field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use
names)
回答by dstromberg
cron's good.
cron 很好。
You may want GNU tar's --files-from; xargs is dangerous here. xargs is fine if the number of files is known to always be small, but if the list of files gets big, xargs restarting tar will toast all but the last n files.
您可能需要 GNU tar 的 --files-from; xargs 在这里很危险。如果已知文件的数量总是很小,xargs 很好,但是如果文件列表变大,xargs 重新启动 tar 将吐司除最后 n 个文件之外的所有文件。

