Automatic scp Script

I need a script to automatically scp the latest files on DIR1,DIR2 and DIR3 from Source to destination server at same place(DIR1,DIR2 and DIR3) . Further details are:

Source Server (192.168.1.5)
DIR1
DIR2
DIR3

Destination Server (192.168.1.10)
DIR1
DIR2
DIR3

If you tell us your OS and shell it would help to give you a better answer. A lot.

Please show us what you have tried. Thanks.

I am new in shell scripting thats y i need a help to create a script. please help me

Once again: we are willing to help, but:

  • which OS do you use? (and which verson of it?

  • which shell do you use?

  • do you have SSH-keys already exchanged between the servers so that the transfer can be passwordless?

We need to know that before we can provide you an answer. If you call the doctor and tell him "it hurts" he would ask "where" and "how long" too before starting to suggest a cure, no?

bakunin

The output of uname -a and ps would be a good place to start on both the client (where you want to drive this from) and the server (where you are connecting too.

Can you log on from the client to the server without putting in the password? This would show that the SSH keys are in place and correct.

Kind regards,
Robin

  • which OS do you use? (and which verson of it?
    SUSE Linux 11 Ent

  • which shell do you use?
    Bash

  • do you have SSH-keys already exchanged between the servers so that the transfer can be passwordless?
    Yes

Thank you for that information. So, is the question "How do you know which is the latest file?" ?

Can you do something like this?:-

#Turn on shell tracing so we can see what's being called
set -x

#Work out the file I need to get
source_file=$(ssh $server "ls -1rt DIR1 | tail -1" 2>/dev/null)
echo "The source file is \"$source_file\""

# Collect the file, writing it to the directory I need
scp $server:$source_file $target_directory

It's not tested, but have a go and let's see if it gets us further. Obviously, set the server and target_directory to something useful first.

It is likely not to work first time, so paste the output in CODE tags into the thread so we can see where I have gone wrong.

Kind regards,
Robin

In post#1 you want "the latest files". How do you identify those? Are there other files; how to tell them from the target ones?