Which is why I suggested making the third copy, which could be "rolled back" in case of a problem. It's similar to DB transactions with redo logs. They don't have to worry about the file permissions problems, though.
eval {
copy file to temp_file
copy file to backup_file
modify temp_file
cat temp_file > file
verify file is OK
remove temp_file and backup_file
};
if ( $@ ) {
remove temp_file
cat backup_file > file
remove backup_file
}
Worst case scenario the cat of the backup_file to temp_file fails and you're left with a corrupt "file" and the extra "backup_file" which would have to be moved back manually.