Say, you had a bad flash and are now boot-looping. Or your dog installed a custom ROM but Android 4.99 came out and you want to update ASAP. This is where this guide is going to come in handy. The instructions aren't any different from the usual factory image installation procedure but they apply specifically to the 2013 Nexus 7, so you can rest assured you're not following some generic guide that may not work on your device. Additionally, I've seen many variations of the steps necessary to preserve your data, and I think this is the most optimal way to go.

Note #1: If your bootloader is locked, your data will be wiped for security reasons - there's no way around that. You will want to back up what you want manually.

Note #2: If your bootloader is already unlocked, you have the option of skipping the data wipe during the factory image flashing procedure. In some situations, not wiping user data may prevent the Nexus 7 from booting, in which case you'd probably need to go ahead with the wipe, but for simple OS upgrades from one version of stock Android to another one higher, wiping data should not be necessary.

Prerequisites

In order to complete the steps below, you need to have the Android SDK installed. The SDK contains the latest version of adb and fastboot, which you will need for flashing files and issuing commands. You will also need a MicroUSB cable to execute adb sideload, fastboot flash, and other commands. You also need to unlock your bootloader - a process that wipes your data.

Enable Developer options by going to the Settings -> About screen and tapping Build number until the OS unlocks the hidden Developer options menu. Now go to Developer options and make sure you have USB debugging turned on. For security purposes, you can turn it off after we're done.

To unlock the bootloader, if you haven't already done so:

Reboot into the bootloader by issuing an adb reboot bootloader command on your computer or holding VolDn+Power while booting (power off, hold VolDn, then hold Power).

Warning: This will wipe your data. Type in fastboot oem unlock on your computer. Agree to the unlock by selecting Yes on your tablet (VolUp, then Power).

You have now unlocked your bootloader (and the data partition was wiped clean). Verify by rebooting to the bootloader again and looking at Lock State.

The Steps

Download the full factory image corresponding to your exact model (for example, 2013 Nexus 7 Wi-Fi is razor), which includes the full system, bootloader, recovery, kernel (boot.img), and radio (radio not included for Wi-Fi-only variants). It's a clean slate for these components - they'll flash cleanly over anything you have now.

Unpack the tgz (tar/gz) archive into its own directory. I use Total Commander, but you can use any tool that reads gz and tar, like 7-Zip:

Proceed with this step if you want to keep your data intact. If you skip this step, your data will be wiped. With your favorite text editor, edit (be careful not to run it by accident instead of editing) flash-all.bat (if you're on Windows) or flash-all.sh (Mac, Linux) and remove "-w" from the fastboot update command. For example, if the line reads fastboot -w update image-razor-jss15j.zip, it should now read fastboot update image-razor-jss15j.zip. The -w option instructs fastboot to wipe data.

Note: The above procedure restored the factory recovery, so if you want a custom one, you can flash one now (see below).

For better security, you can relock the bootloader by issuing fastboot oem lock while booted to the bootloader. This will, however, mean that if you ever want to do anything that requires an unlocked bootloader, you will lose your data in the unlock process.

Rooting

Rooting a Nexus 7 is very easy. It requires an unlocked bootloader and a custom recovery. I recommend using TWRP.

Flash TWRP from here (Reboot into the bootloader as before and then fastboot flash recovery FILENAME.img).

Reboot into recovery (adb reboot recovery or hold VolUp and Power while booting).

Flash SuperSU from this post. To do that, download the zip, then put the recovery into adb sideload mode (in TWRP, it's under Advanced), and then do adb sideload FILENAME.zip. I've seen some people complain that doing so via CWM doesn't work, but flashing through TWRP and selecting Fix Superuser Permissions at the end does the trick.