Consolidate a lot of redundant backup/restore code.

This code is mostly used for doing manipulation on the
image during customization pre- and post-.  There was a
lot of duplication there, so this patch consolidates that
into a couple of functions, and then replaces the redundancy
with those functions.  This gets rid of a lot of error-prone
code.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>