[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1555
  • Last Modified:

Need help writing a script for a Cron Job to copy files.

I need someone to write me a short cron job script to copy files from an NFS mount to an external hard drive on a daily basis.

The best situtation would be to only copy files that have changed, but it could also just delete everything on the external drive and recopy it.

I'm a total noob with respect to this, so I need to be walked through it.

This is worth 500 Points.
  • 2
2 Solutions
You could use rsync as it will automatically copy only the files which have changed :-

rsync /path/to/source/files /destination/path
You will probably also want to use the '-a' option so that all subdirectories are copied aswell.
The '--delete' option will delete files in the destination path which are no longer in the source path.
The '-n' option will perform a dry run showing which files would have been copied.
Kent OlsenData Warehouse Architect / DBACommented:
Hi gerhardub,

As far as the script goes, it won't care about the device where the files currently reside, nor will it care about the destination device.  All the script will do is copy files.

The script can be as short as a single line.  Note that you'll have to use the corrent path/filenames.

#  Copy all of the messages files to /tmp
   cp  /var/log/messages* /tmp  

So the trickiest part is figuring out exactly when you want the copy to execute.

Then set up the crontab to run at the desired time.  Start by running crontab

   crontab -e                  # this displays the cron jobs in your default editor

Edit the file and add a line like this:

  * 1 * * * /home/me/scripts/backupmessages.sh

Then exit the editor (with save)

The job will run every morning at 1AM.

Good Luck,

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now