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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

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
0
ealdaz
Asked:
ealdaz
1 Solution
 
ravenplCommented:
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
0
 
chris_calabreseCommented:
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
}
0
 
pjedmondCommented:
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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ravenplCommented:
> 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)?
0
 
ealdazAuthor Commented:
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
}
0
 
ravenplCommented:
You could
scp localfile user@remote:/destination/filename.tmp && ssh user@remote "mv -f /destination/filename.tmp /destination/filename"
0
 
ealdazAuthor Commented:
It sounds like a good solution, does the && ensure that the ssh part only gets executed if the scp is succesful?
0
 
ravenplCommented:
Yes. Unless scp get confused somehow and will return true as exit code
0
 
ealdazAuthor Commented:
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"

0
 
ravenplCommented:
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.
0
 
ealdazAuthor Commented:
Sorry, disregard my previous silly question
0
 
ealdazAuthor Commented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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