Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 按创建日期从Cron到tar文件_Unix_Cron_Tar - Fatal编程技术网

Unix 按创建日期从Cron到tar文件

Unix 按创建日期从Cron到tar文件,unix,cron,tar,Unix,Cron,Tar,我需要创建一个cron作业,该作业将tar在过去X天内创建的所有图像。 我通过在CPanel中设置cron作业来在我的网站上使用cron作业,但在unix方面我不是一个有经验的脚本编写者。任何帮助都将不胜感激 这是我的cron工作: 0 4 * * 1 tar pzvcf /home/xxxxxx/public_html/backups/images_backup.tar /home/xxxxxx/public_html/images/products 使用find定位所有图像并将其提供给ta

我需要创建一个cron作业,该作业将
tar
在过去
X
天内创建的所有图像。 我通过在
CPanel
中设置cron作业来在我的网站上使用cron作业,但在unix方面我不是一个有经验的脚本编写者。任何帮助都将不胜感激

这是我的cron工作:

0 4 * * 1 tar pzvcf /home/xxxxxx/public_html/backups/images_backup.tar /home/xxxxxx/public_html/images/products

使用find定位所有图像并将其提供给tar

类似这样的内容应该会为您提供在过去2天内创建的文件(-2表示<2*24小时)

find-ctime-2-print
像这样的东西可能会完成整个工作:

find <path> -ctime -<within_days> -print | tar cf <output.tar> -T -

You need to specify:
  <path>        (where to search for images)
  <output.tar>  (output file name)
  <within_days> (add files < this many days old)
find-ctime--print | tar cf-T-
您需要指定:
(在何处搜索图像)
(输出文件名)
(添加文件<此天数)
tar cf
创建一个tarball,
tar af
附加到现有tarball

-T-
告诉tar从stdin读取命令列表

find命令将匹配文件列表回显到stdout


|
将find的stdout连接到tar的stdin:因此tar应该添加find找到的所有文件。

U可以使用以下命令

tar -cvzf outputfilename.tar ` find . -name '*' -mtime -2 -print`
在find命令中 . -> 要搜索的目录 *->文件模式 -2->表示天数

就你而言

0 4 * * 1 tar -cvz /home/xxxxxx/public_html/backups/outputfilename.tar ` find /home/xxxxxx/public_html/images/products -name '*' -mtime -2 -print`

cron是否能够将文件添加到现有tar中?使用tar avf而不是cvf来追加(c=create,a=append)-您可能也可以不使用v(这只是意味着冗长-在cron作业中不太有用),您可能希望将
find
限制为仅列出常规文件。如果将目录名传递给
tar
,它将递归地归档整个目录内容。使用:
find-type f…
如果文件名可能包含空格或其他shell元字符,则需要使用
-print0
参数来
find
而不是
-print
,然后需要将
--null
选项包含到
tar
中。我应该在tar命令中的哪里添加--null选项?你能解释一下它的作用吗?我能用find吗-命名为'l_*.jpg'以仅对具有该模式的文件进行tar?如果文件列表很大(数千个),则使用此技术可能会出现
命令行过长的错误。另外,最好限制
find
不列出目录。很抱歉重播延迟。希望你能通过测试找到答案,你可以用同样的方法来寻找模式。如果有大量文件,您可以将响应重定向到虚拟文件(使用“>>日志文件名”)。
0 4 * * 1 tar -cvz /home/xxxxxx/public_html/backups/outputfilename.tar ` find /home/xxxxxx/public_html/images/products -name '*' -mtime -2 -print`