APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Use scp without overwriting existing files


No clobber scp - scp without overwriting




2012/10/07


I don't allow ftp to my website. The only access I allow is ssh, which means I need to scp files and images that I want to add there.

I have a problem with images particularly because I could accidentally over-write an older image with a newer image of the same name. While that may be exactly what I want to do, the new image might not be right for older web pages that reference that name.

I have made that mistake already. I can fix it from backups, but in that particular case I didn't notice the error for several years - it was not until someone pointed it out in a comment that I realized my mistake. By then, I had no backups old enough and I couldn't easily recreate the image because it was showing something on old software that I could no longer run. All I could do was apologize!

I don't want that to happen again.

No Clobber in SSH

I thought there surely must be a "no clobber" or "no over-write" or "no update" option in ssh. There is not. Apparently the developers see no need of such a thing and will always suggest using rsync instead because rsync has a flag for that:

--ignore-existing       skip updating files that exist on receiver
 

That DOES work, but it's not helpful for my need. When I'm preparing an article, I might have half a dozen or more files with similar names. When I'm ready to scp them up, I reference them with a wildcard:

scp control_* ... # WILL over-write files
rsync control_* -v -e ssh  --ignore-existing ... # WON'T over-write
 

The problem with that is that it ignores silently. We can increase the verbosity:

rsync control_* -vv -e ssh  --ignore-existing ... # WON'T over-write
 

But that's too noisy for my tastes:

existing . stage 
delta-transmission enabled
control_additional_domains.jpg exists
control_additional_domains_lg.jpg exists
control_domain.jpg exists
control_domain_lg.jpg exists
control_local.jpg
total: matches=0  hash_hits=0  false_alarms=0 data=53802

sent 53994 bytes  received 42 bytes  21614.40 bytes/sec
total size is 282872  speedup is 5.23
 

Other ways to avoid over-writing with ssh

There are plenty of ways around this. You could scp stuff up to a directory and then use ssh to call a server side script that tests what exists before copying to the final destination, but I wanted something I could use without depending on a server side script.

This is what I finally decided to use. I wrote it in Perl, but you could do something similar in Bash or indeed any language. This script does do (or at least attempt) two scp's, but that isn't as inefficient as it sounds: if the first scp succeeds, it won't do the second, so really there's only a very little extra overhead.

#!/usr/bin/perl
$dest=pop @ARGV;
print "$dest\n";
foreach (@ARGV) {
unlink("/tmp/$_");
system("scp $dest/$_ /tmp > /dev/null 2>&1");
if ( -e "/tmp/$_") {
  push @errors, $_;
  next;
}
system("scp $_ $dest");
}
foreach (@errors) {
  print "Could not copy $_ -  exists!\n";
}
 

That's it. You use it much as you would ssh:

myscp foo* [email protected]:folder/put_them_here/
control_additional_domains.jpg                100%   28KB  27.8KB/s   00:00    
control_additional_domains_lg.jpg             100%   89KB  89.4KB/s   00:00    
fobbydo                                       100%   29     0.0KB/s   00:00    
Could not copy control_domain.jpg -  exists!
Could not copy control_domain_lg.jpg -  exists!
Could not copy control_local.jpg -  exists!
 

By showing existing files at the end, I'm not going to miss seeing them. You could also write @errors to a file so that you could easily examine the problems.




If this page was useful to you, please help others find it:  





1 comment




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!




Sun Oct 7 20:44:29 2012: 11369   TonyLawrence

gravatar


Also: don't use /tmp on a multiuser machine. You may want to set a specific directory just for this purpose.



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Basics
       - Linux
       - MacOSX
       - Scripting
       - Shell















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)