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

Posted on 2007-10-01
Last Modified: 2013-12-16
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.
Question by:gerhardub
    LVL 36

    Accepted Solution

    You could use rsync as it will automatically copy only the files which have changed :-

    rsync /path/to/source/files /destination/path
    LVL 36

    Expert Comment

    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.
    LVL 45

    Assisted Solution

    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/

    Then exit the editor (with save)

    The job will run every morning at 1AM.

    Good Luck,

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now