By: +David Herron; Date: December 15, 2015
The root volume of a Raspberry Pi is, by design, an SD Card. The first thing to do is to shutdown the Pi, and remove the SD Card. You won't be able to do any of these tasks in the Pi itself, and instead you'll need to use another computer. That computer will itself need an SD Card slot, or else to use an SD card reader.
Something I'm not going to cover is if your computer runs Windows. Your answer is a program called Win32DiskImager. I know nothing about using that program, and cannot tell you anything about it.
Preparation - device pathname for SD Card
On your Mac or Linux machine, all devices have a path name like
/dev/sdd1. The actual pathname depends on your operating system. To discover the pathname for the SD card, you first run this command:
$ df -k
Then insert the SD card and run that command again. Once the card is inserted the operating system assigns it a pathname, and you'll see the card as new entries in the table printed by
df -k. On my computer this is what's printed:
/dev/sdb2 30G 9.4G 19G 34% /media/removable/SD Card /dev/sdb1 60M 20M 41M 34% /media/removable/boot
There are two lines shown because the Raspberry Pi OS puts two partitions on the SD card. The pathname we're interested in is the portion before the partition number. In this case that is
/dev/sdb. This is the pathname of the device itself, as opposed to the pathname of a partition on the device. On other systems the pathname might be
/dev/mmcblk0p1 and in such cases it is
p1 which is the partition number, and therefore
/dev/mmcblk0 is the device pathname.
On a Mac the
/dev/rdisk<n> pathname is preferred because it is far quicker than using the
The next step is to make sure the SD card is not mounted by the operating system. As user-friendly behavior, most operating systems automatically mount an SD card as soon as it's inserted into the computer. To do any of these tasks the card has to be unmounted.
Backing up an SD card
In this task we're going to copy an image of the SD card to your desktop computer. Later we'll see how to write that image to another SD card.
$ dd if=/dev/device-pathname of=pi-disk-image.img bs=1M
dd command does raw copying of data. The
if= option specifies the input file (
if), and of course
of= specifies the output file. This simply means the
dd command is copying from the device file to a file on the local computer. The
bs= option specifies the buffer size used while copying.
Nothing will be printed while the "dd" command is running. When it's over, a message is printed saying how much was copied:
$ dd if=/dev/device-pathname of=pi-disk-image.img bs=1M 30703+0 records in 30703+0 records out 32194428928 bytes (32 GB) copied, 3054.01 s, 10.5 MB/s
You won't be able to do much with this file - to my knowledge, but at least it's safely stored on your computer.
Writing an image to an SD card
Suppose you've faithfully made backups of your SD card, and one day it dies. Thankfully your desktop computer has backup images, so what do you do?
$ dd bs=4M if=2015-11-21-pi-disk-image.img of=/dev/device-pathname 30703+0 records in 30703+0 records out 32194428928 bytes (32 GB) copied, 3054.01 s, 10.5 MB/s
Basically, we've reversed the order of the
of= arguments, and told
dd to copy from the saved image to the SD card.
You would do this to get running again from a backup - or to duplicate an SD card to have two identical Raspberry Pi's.
By the way, very similar instructions are on the Raspberry Pi website for setting up a Raspbian image downloaded from their site.
Copying to a larger SD card, and increasing the file system size
Something I skipped over is the size of the SD cards. When copying an image to a new SD card, the destination card has to be equal in size or larger to the original SD card.
What you're copying is the raw data bytes that make up the file systems and partitions on the SD card.
When those bytes are copied to a new SD card, the disk partitions will be the same size as the original SD card. If you had a 32 gigabyte SD card to start with, the partition size shown once the new card is mounted in a Raspberry Pi will be about 32 GB (minus space for the boot partition).
If you bought a 128GB SD card hoping to get more space on your Raspberry Pi, you'll be disappointed to see only 32 GB of space. Fortunately it's easy to fix this - at least under Raspbian.
Simply run " raspi-config" and select the Expand Filesystem option. There's a command line version available, as well as a GUI if you run the GUI desktop.
Et voila, once you've done this (and rebooted the system) the partition will have filled out the disk.
Backing up source code and other files
While it's feasible to backup system images this way, there are other ways to skin this cat. You don't have to keep the one-and-only copy of your work on the Raspberry Pi.
For example, source code should be managed using Git or other similar tools. Simply set up a Git repository elsewhere, and every so often push your changes there.
The same is true of other sorts of files. Not necessarily to use Git for everything, but to simply copy your files to other machines on a routine basis.
For example, use "rsync":
$ rsync --archive --delete project-dir/ remote-user@remote-host:/path/do/remote/project-dir/