How to back up your Raspberry Pi SD card, or copy it to another (larger?) Raspberry Pi SD card

By: ; Date: December 15, 2015

Tags: Raspberry Pi » Raspberry Pi Zero

Raspberry Pi Logo
SD Cards aren't exactly the most reliable of data storage devices. What happens if you've put hundreds of hours of work into a Raspberry Pi system, it's all on your SD Card, and the card craps out. Have you saved your work? Or maybe you need to move to a larger SD Card because you've run out of space. Or maybe you want to duplicate the card to have additional systems. These tasks are pretty easy, but not intuitively obvious. It'll take some time at the command line, but fortunately the commands are easy.

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 /dev/disk<n> pathname.

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

The 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 if= and 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/

More info

See: http://raspberrypi.stackexchange.com/questions/311/how-do-i-backup-my-raspberry-pi

« MEADS Capability Nodes Hell Bent from Gallifrey straight to Big Finish - Maisie/Jenna as Me/Clara from now til the end of time »
2016 Election Acer C720 Android Apple Hardware History Apple iPhone Hardware April 1st ARM Compilation Authoritarianism Big Brother Botnets Cassette Tapes Cellphones Christopher Eccleston Chrome Apps Chromebook Chromebooks ChromeOS CIA CitiCards Civil Liberties Clinton Cluster Computing Computer Hardware Computer Repair Cross Compilation Crouton Cybermen Daleks Darth Vader Data backup Data Storage David Tenant DIY Repair Docker Doctor Who Drobo DVD Emdebian ESP8266 Eurovision Fake News FireFly Fraud Freedom of Speech Gallifrey git Google Google Chrome Google Gnome Great Britain Home Automation HTTPS Internet Internet of Things Internet Privacy iPad iPhone iPhone hacking Iron Man Iternet of Things John Simms Lets Encrypt Linux Single Board Computers Mac OS MEADS Anti-Missile Mercurial Michele Gomez Military Hardware Missy Mobile Applications Mondas Monty Python MQTT Music Player Music Streaming MySQL NanoPi Node Web Development Online Fraud Open Media Vault Open Source Software OpenVPN Personal Flight Peter Capaldi Plex Media Server Political Protest Power Control Privacy Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero Recycling Retro-Technology Right to Repair Rocket Ships Russia Season 1 Season 10 Season 11 Security Security Cameras Silence Simsimi Skype Social Media Warfare Software Development Space Flight Space Ship Reuse Space Ships SpaceX SQLite3 SSD Drives SSD upgrade SSL Terrorism The Cybermen The Daleks The Master Trump Trump Administration Ubuntu Virtual Private Networks VOIP Web Developer Resources Weeping Angels WhatsApp