Backing up a Raspberry Pi's Operating System

The Raspberry Pi is a lot of fun to tinker with. However, you may not want to spend a bunch of time reinstalling the Pi's OS (Raspbian or others) when something goes wrong or you want to start over fresh. Here's how you can create a backup of your Pi's operating system so you can restore it quickly when you need to do so.
Creating a Pi Backup with Linux
You'll need another computer to create the backup of the Pi OS. I will show you how to do this with a Linux machine, since that is what I use. First, shutdown the Pi (sudo shutdown -h now or use the Pi's GUI). Next, remove the SD card or USB drive that has the Pi's OS installed on it. Now plug it into the machine that you are using to create the backup. Open a terminal on that machine and type
dmesg | tail
It will output something like this:
[12519.406486] mmc0: new high speed SDHC card...
[12519.412045] mmcblk0: mmc0:a7e2 SD04G 3.75 GiB 
[12519.412790]  mmcblk0: p1
Use this command to create a backup of the card:
sudo dd if=/dev/mmcblk0 of=backup.iso bs=4M
Notice that I'm using the device name from the output of dmesg, mmcblk0. This will create a file called backup.iso that is a backup of your entire SD card, with the Raspberry Pi's OS installed on it. Note that you will need at least enough free space to hold the entire card on the machine you're using, which would be 4 GB for my card. This may take a while to complete depending on the size of your Raspberry Pi's SD card.
Restoring the Pi's Backup
Whenever you want to restore the Raspberry Pi to the state it was in when you created the backup, use this command after inserting the SD card into the other machine:
sudo dd if=backup.iso of=/dev/mmcblk0 bs=4M
This will restore the backup.iso copy of your Pi's SD card back onto the card. Then you can re-insert the card into the Raspberry Pi and boot it.