About technologies for your digital home

For beginners and tinkerers

Backup SD card

July 28th, 2014



Why would you need a backup SD card?

I wanted just to upgrade my SD card to faster one on my Raspberry Pi XBMC, but there could be more good reasons to do this.

Copying the files itself can’t be as straightforward as you thought because your SD card may contain few different file system partitions (like in Raspberry Pi case). For this reason, it will be easier to copy the whole drive.

SD card backup

What you will need?

  • A working Linux (e.g. Ubuntu) environment;
    if you do not have one, a bootable Ubuntu CD should be enough
  • two SD cards;
  • the will to learn something new 😉

Find your SD card reader on Linux

First, you need find out which device is used for your SD card reader.

This can be done by issuing the command:

sudo fdisk -l

which returned such results on my SONY VAIO laptop:

Disk /dev/sda: 750.2 GB, 750156374016 bytes
255 heads, 63 sectors/track, 91201 cylinders, total 1465149168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x74a02dcf

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 206847 102400 7 HPFS/NTFS/exFAT
/dev/sda2 206848 1260345343 630069248 7 HPFS/NTFS/exFAT
/dev/sda3 1260347390 1465147391 102400001 5 Extended
Partition 3 does not start on physical sector boundary.
/dev/sda5 1260347392 1456828415 98240512 83 Linux
/dev/sda6 1456830464 1465147391 4158464 82 Linux swap / Solaris

Disk /dev/mmcblk0: 7822 MB, 7822376960 bytes
4 heads, 16 sectors/track, 238720 cylinders, total 15278080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b6135

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 * 2048 258047 128000 c W95 FAT32 (LBA)
/dev/mmcblk0p2 258048 15245311 7493632 83 Linux

The fdisk command showed two storage devices: /dev/sda (750.2 GB) and /dev/mmcblk0 (7822 MB). I am only interested in 2nd device /dev/mmcblk0

Use dd command to backup SD card

After finding which device is used as an SD card reader, we can backup SD card by issuing the command:

sudo dd if=/dev/mmcblk0 of=~/sd_card_image

This command will make a binary backup of SD card and it will put in current’s user’s home directory (~/).

If you skip the sudo command, the result will be:

dd: opening `/dev/mmcblk0': Permission denied

Keep in mind, that copying may take a little bit time.
You can see the results here for 8GB card:

15278080+0 records in
15278080+0 records out
7822376960 bytes (7.8 GB) copied, 703.505 s, 11.1 MB/s

So, that’s more than 10 min for copying 😉

If you want to follow the progress of copying, you can issue such command:

ls -lah ~/sd_card_image
-rw-r--r-- 1 root root 1.9G Jul 26 21:09 sd_card_image

You can see that only 1.9GB is used as a backup of the SD card.

Restoring your backup to SD card

If you want to restore the just created backup of SD card, you have to change if and of variable in the command below:

sudo dd of=/dev/mmcblk0 if=~/sd_card_image

which takes longer compared to the reading command:

15278080+0 records in
15278080+0 records out
7822376960 bytes (7.8 GB) copied, 2689.77 s, 2.9 MB/s

If you would like to read more about using the dd command there is a nice article on Linux journal about this.

Congratulations, you can enjoy faster or newer SD card!!!