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?