Link to home
Start Free TrialLog in
Avatar of ealdaz
ealdaz

asked on

Can SCP copy files by first doing a temporary copy on the remote host

I am using scp to update files in a network of linux units that we access through GPRS, unfortunately when updating some critical files, like the main executable of our application, it has happened that the connection has gone down while copying the file, and it has been left mangled.

Is it possible to make scp automatically copy the file with a temporary name on the remote host, and only when it has finished rename it to it's proper name? This seems to be the default behaviour of WinSCP for windows.

Thanks
Eduardo
Avatar of ravenpl
ravenpl
Flag of Poland image

I'm not aware of.
But You could split the job to two jobs. First copy files to /tmp subdirectory tree, then make another ssh connection to rename/move them to proper place
Avatar of chris_calabrese
chris_calabrese

As ravenpl suggests, there are many ways to skin this cat.

for example, you could do something like....


function get_file {
    if scp -p "$1" "$2.tmp"; then
        mv "$2.tmp" "$2"
        return 0
    fi
    rm -f "$2.tmp"
    print -u2 -r -- "$0: error getting file '$1' '$2'"
    return 1
}
How about rsync (over ssh for security):

rsync -Cavz -e ssh user@source:/path user@dest:/path

If it goes wrong, then you can just rerun it until it works?
> If it goes wrong, then you can just rerun it until it works?
Same for scp job. The Q is whether rsync guarantees that in case of connection loose - whole tree would be consistent (like transactional copy of bunch files)?
Avatar of ealdaz

ASKER

Thank you all,
I knew that scp worked to copy to a remote host, but i wasn't aware that mv could be used to move files on a remote machine, is this really the case?
The idea is that i would like the process to run totally automaticaly on the local machine, so i would like to avoid doing a ssh and manually having to do the mv on the remote machine itself, but from your email i gather there is a way to do this automatically with ssh?

Basicaly I would like to run the function below from the local computer, but i would need the move to work on the remote computer.

function get_file {
    if scp -p "$1" "$2.tmp"; then
        mv "$2.tmp" "$2"
        return 0
    fi
    rm -f "$2.tmp"
    print -u2 -r -- "$0: error getting file '$1' '$2'"
    return 1
}
ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ealdaz

ASKER

It sounds like a good solution, does the && ensure that the ssh part only gets executed if the scp is succesful?
Yes. Unless scp get confused somehow and will return true as exit code
Avatar of ealdaz

ASKER

So the solution including deleting the temporary file would be to do this?
scp localfile user@remote:/destination/filename.tmp && ssh user@remote "mv -f /destination/filename.tmp /destination/filename" && ssh user@remote "rm -f /destination/filename.tmp"

after mv source file does not exists. Rather
scp localfile user@remote:/destination/filename.tmp && ssh user@remote "mv -f /destination/filename.tmp /destination/filename" || ssh user@remote "rm -f /destination/filename.tmp"
the third part(removing file) will usually fail - since copy failed (due to network error) next ssh connect would propably fail as well.
But don't worry - scp overwrites the destination file.
Avatar of ealdaz

ASKER

Sorry, disregard my previous silly question
Avatar of ealdaz

ASKER

I've implemented the suggested solution in my  script, and i've found a strange problem, wich is that the script that is calling ssh seems to cause it to exit prematurely because of it.

I've created a new question with the Title: Calling ssh seems to provoke exiting of while loop , as i thought that was the problem.