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.

  • 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

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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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