I am writing a utility/script (in Python, as it happens) to run on Linux, which backs up data from a remote server, and outputs to stdout. At the moment, I run this script on the hour from cron, and redirect the output to a file, like this:

./mybackupscript > ~/backups/mybackupfile

Sometimes the backup will fail (for example, the network connection is down, or the remote server times out) - this means the stdout content is either incomplete, or missing. The utility is well behaved - when this happens, it exits with a non-zero exit code. However, the backup file is at that point still empty or incomplete. I would prefer that it retain its original contents (from the last successful run of my script).

What is the most appropriately "Unix-y" way of handling this issue, to make the backup more robust? Should I output to a temporary file, then copy it over the final file only if the backup works?

1 Answer 1


Should I output to a temporary file, then copy it over the final file only if the backup works?

Not copy but rename.

But this is impossible for the backup script if it writes to stdout. It cannot even prevent the empty file from being created as that has already happened when the script starts. This must be done from the outside.

if ./script.sh >backup.tmp; then
  mv backup.tmp backup
  rm backup.tmp
  • Great, don't know how I missed something so obvious. Thanks. Marking this one correct as you've got a longer explanation (thanks, though, Stéphane!) Jan 29, 2015 at 13:18
  • 1
    @AndrewFerrier That's why we call it "accepted answer" not "correct answer". :-) Jan 29, 2015 at 13:19
  • Thinking about it, I would also add: || rm backup.tmp to clean up the temporary file. Does that seem reasonable or is there unforeseen danger there? Jan 29, 2015 at 13:25
  • 1
    @AndrewFerrier No. It's rather strange that none of us had this idea by himself. Jan 29, 2015 at 13:31
  • 1
    @Costas And why is backup.tmp && cp backup.tmp backup ; rm backup.tmp better than backup.tmp && mv backup.tmp backup || rm backup.tmp? mv can fail but that should not be a problem here. Jan 29, 2015 at 13:45

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.