Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I currently have a setup where I have an emergency server which on a scheduled basis grabs files from my main webserver using wget. If all goes well I want wget to clobber the old cached file. However, if the request returns 404 or something else goes wrong where data cannot be retrieved (e.g. my main server goes down), I don't want the cached data to get clobbered. I've tried using -N which correctly clobbers the data if a new version is available. However, if there is a 404 it clobbers that data with 0 byte files.

share|improve this question
    
wget is not a backup tool. you'd be really better off with rsync and database replication (if needed). –  forcefsck Mar 23 '11 at 22:19
    
Thanks for the tip. Unfortunately, I have kind of an odd situation that I didn't describe well the first time. I need to grab pages that have dynamic content (from my web server) and store the HTML contents on the emergency server. So I'm not really just backing up files per say. I also don't really want to setup all the databases on the emergency server to duplicate all the dynamic content mostly just because I don't think it's worth all the work. –  Kyle Mar 24 '11 at 19:50

2 Answers 2

up vote 1 down vote accepted

If you can modify your web server's configuration or allowed to have .htaccess, you can setup custom error pages. For example, assuming apache as webserver, make an error page /var/www/error-pages/404-error.html and add the following to your .htaccess or vhosts section.

Alias /error-pages /var/www/error-pages
ErrorDocument 404 /error-pages/404-error.html

Together with the alias directive, you can use the same error pages for multiple vhosts if you have more than one. This alone should help not clobbering, but you can also add -X error-pages to your wget parameters to skip all custom error pages in general.

share|improve this answer

Make a copy of the file before calling wget. (There doesn't seem to be a way to tell wget to unlink and recreate rather than clobber the file.)

cp -p foo foo.prev
wget http://example.com/foo
if ! [ -S "foo" ]; then mv -f foo.prev foo; done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.