bikey77
bikey77

Reputation: 6672

Cron to tar files by creation date

I need to create a cron job that will tar all images created within the last X days. I use cron jobs on my websites by setting them up in CPanel, but I'm not an experienced script writer when it comes to unix stuff. Any help will be appreciated.

This is my cron job:

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

Upvotes: 0

Views: 1726

Answers (2)

DAC84
DAC84

Reputation: 489

U can use the following command

tar -cvzf outputfilename.tar ` find . -name '*' -mtime -2 -print`

In find command . -> directory to search * -> file pattern -2 -> represent no of days

in your case

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

Upvotes: 0

John3136
John3136

Reputation: 29266

Use find to locate all the images and feed them to tar.

Something like this should give you file created in the last 2 days (the -2 means < 2*24 hrs)

find <path> -ctime -2 -print

Something like this probably does the whole job:

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)

tar cf <file> creates a tarball, tar af <file> appends to an existing tarball

The -T - tells tar to read the list of commands from stdin.

The find command echos the list of matching files to stdout.

The | joins the stdout of the find to the stdin of the tar: so tar should add all the file found by the find.

Upvotes: 2

Related Questions