Cron Job to copy file from one location to another for new files (daily)

cronfiles

How do I create a cron job to copy all files that are new within the 1 day span (each day at midnight)? So essentially all files created from the day of to the new folder with all permissions, date attributes, owner/group, in tact.

All files from /var/www/folder1/ to /var/www/folder2/

using crontab -e

Essentially, I am looking for the parameters that can be used to copy files daily from one folder to the other keeping all attributes intact.

Linux: UBUNTU 10.04 lts

Best Answer

cron only executes commands at a given time. In order to do what you want, you need to figure out a command that will do what you want, and then execute it with cron at a given time.

For example, to simply copy the files from one location to another, you could use

rsync -a /origin /destination

and then schedule it to run with cron by running crontab -e and specifying

0 0 * * * /usr/bin/rsync -a /origin /destination

in the file. That will cause your rsync to run at midnight each day.

Doing that each day will keep the two directories in sync. If you want to only copy files that have been created in the last day, that's a bit more difficult, but can be done with find with the --newer and -exec option to run a cp to copy the files.

Related Topic