Daddyfix
Posts: 52
Joined: Sun Dec 09, 2012 2:13 pm
Location: Sudbury, Ontario

Backup and Restore: Long and Quick way

Wed Aug 28, 2013 6:14 pm

There are two ways to copy and restore the SD Card using DD (Linux).

Long Way

Copy SD Card 8GB with DD

In linux you can use DD to copy the whole image of a SD Card. Find the attached SD Card device

Code: Select all

$ df -h | grep sd
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda1                19G  4.1G   14G  24% /
/dev/sda5                87G   21G   63G  25% /home
/dev/sdc2               3.0G  1.8G  1.1G  63% /media/mcon/rootfs
/dev/sdc1                56M   19M   38M  33% /media/mcon/3312-932F
In this case, /dev/sdc is the attached SD Card.

Make an image of the Sd Card attached.

Code: Select all

$ sudo umount /dev/sdx
$ sudo dd if=/dev/sdx of=sdcard.img bs=1024
$ ls -lrt | grep sdcard.img
-rw-r--r-- 1 root root 7948206080 Aug 28 10:04 sdcard.img
  • if - input file or device
    of - output file or device
    bs - block size
Notice the size of the image file sdcard.img? 7948206080 bytes. this means it was copied from a 8Gb SD Card.

Write Image with DD

To write the image file copied above to an sd card. Find the location of the Sd Card attached

Code: Select all

$ df -h | grep sd
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda1                19G  4.1G   14G  24% /
/dev/sda5                87G   21G   63G  25% /home
/dev/sdc2               3.0G  1.8G  1.1G  63% /media/mcon/rootfs
/dev/sdc1                56M   19M   38M  33% /media/mcon/3312-932F
or

Code: Select all

$ sudo $ mount | grep /dev/sd
/dev/sda1 on / type ext4 (rw,errors=remount-ro)
/dev/sda5 on /home type ext4 (rw)
/dev/sdb1 on /home/mcon/Storage type ext4 (rw,nosuid,nodev,_netdev)
/dev/sdd1 on /media/mcon/A9E7-0ECD type vfat (rw,nosuid,nodev,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,showexec,flush,uhelper=udisks2)
/dev/sdc2 on /media/mcon/rootfs type ext4 (rw,nosuid,nodev,uhelper=udisks2)
/dev/sdc1 on /media/mcon/3312-932F type vfat (rw,nosuid,nodev,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,showexec,flush,uhelper=udisks2)
Unmount the the device and copy the image to the SD Card.

Code: Select all

$ sudo umount /dev/sdx1
$ sudo umount /dev/sdx2
$ sudo dd if=sdcard.img of=/dev/sdx bs=1024
Quicker Way
Quicker or Putting a regular 4GB image on 4+ Gb Sd Card

If the SDCard is bigger than the partitions then copying the whole card is a waste of time and HDD space. Below I will show you how to copy just what is needed.
gparted_sdcard_partitions.png
GParted (linux) to show the sd card partitions. 8GB card but only 4GB is used.
gparted_sdcard_partitions.png (47.27 KiB) Viewed 12397 times
Copy the MBR and Partitions

The MBR is stored in the the first 512 bytes of the disk. It consist of 3 parts:
  • The first 446 bytes contain the boot loader.
    The next 64 bytes contain the partition table (4 entries of 16 bytes each, one entry for each primary partition).
    The last 2 bytes contain an identifier
To save the MBR into the file "mbr.img"

Code: Select all

$ sudo dd if=/dev/sdc of=mbr.img bs=512 count=1
To restore (be careful : this could destroy your existing partition table and with it access to all data on the disk):

Code: Select all

$ sudo dd if=mbr.img of=/dev/sdc
If you only want to restore the boot loader, but not the primary partition table entries, just restore the first 446 bytes of the MBR.

Code: Select all

$ sudo dd if=mbr.img of=/dev/sdc bs=446 count=1
To restore only the partition table.

Code: Select all

$ sudo dd if=mbr.img of=/dev/sdc bs=1 skip=446 count=64
You can also get the MBR from a full dd disk image, see above for filename.

Code: Select all

$ sudo dd if=sdcard.img of=just-mbr.img bs=512 count=1
Copy a Partition

DD Copy just the partitions on the SD Card.

Code: Select all

$ sudo df -T
...
/dev/sdc2               3.0G  1.8G  1.1G  63% /media/mcon/rootfs
/dev/sdc1                56M   19M   38M  33% /media/mcon/3312-932F
...
$ sudo dd if=/dev/sdc1 of=partition1.img bs=1024
$ sudo dd if=/dev/sdc2 of=partition2.img bs=1024
$ ls -lrt
total 3203080
-rw-r--r-- 1 root root        512 Aug 28 13:23 mbr.img
-rw-r--r-- 1 root root   58720256 Aug 28 13:24 partition1.img
-rw-r--r-- 1 root root 3221225472 Aug 28 13:27 partition2.img
Restore MBR and Partitions

This is dangerous. You must be certain that the MBR image with the Partition Table matches that of the partitions you are going to put into them otherwise the data will be corrupt on the Sd Card.

Step 1 - Restore the MBR

First find the location of the Sd Card then restore.

Code: Select all

$ sudo dd if=mbr.img of=/dev/sdc
gparted_image_mbr_only.png
GParted (Linux) after the MBR img has been restored to SD Card
gparted_image_mbr_only.png (47 KiB) Viewed 12396 times
Step 2 - Restore the Partitions

Code: Select all

$ sudo dd if=partition1.img of=/dev/sdc1 bs=1024
$ sudo dd if=partition2.img of=/dev/sdc2 bs=1024
Hope this help someone! It was driving me nuts copying/restorinf the WHOLE SD Card!

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Backup and Restore: Long and Quick way

Fri Aug 30, 2013 12:53 pm

If you only want to restore the boot loader...
The Pi doesn't use this bootloader code, it simply reads bootcode.bin directly from the first partition (which must be FAT or FAT32 format).

And of course your "fast way" is only quicker than the "slow way" if you haven't resized your root partition to fill up the whole card ;-)

nummy
Posts: 35
Joined: Tue Aug 14, 2012 12:07 am

Re: Backup and Restore: Long and Quick way

Wed Sep 04, 2013 7:49 pm

Is there a way to do this by running dd on the Pi itself while it's on and running? And maybe, output to a file on a NAS or a mounted volume?

I imagine you'd have to boot up in read only mode and have a way of changing it back. Just wondering if you have any thoughts on this.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Backup and Restore: Long and Quick way

Wed Sep 04, 2013 7:57 pm

It's not recommended to use dd on a live-booted Pi. Maybe you could use rsync instead?
Or something like this http://www.raspberrypi.org/phpBB3/viewt ... 44#p412544

thawn
Posts: 1
Joined: Mon Dec 01, 2014 2:11 pm

Re: Backup and Restore: Long and Quick way

Mon Dec 01, 2014 2:49 pm

Wouldn't it be much quicker to use rsync to copy the actual files once you have restored the MBR and recreated the filesystem?

Assuming your source is /dev/sdc and your destination is /dev/sdd:

Code: Select all

sudo dd if=/dev/sdc of=/dev/sdd bs=512 count=1
eject and plug /dev/sdd back in

Code: Select all

sudo mkfs.vfat -F 16 /dev/sdd1
sudo mkfs.ext4 /dev/sdd2
sudo mkdir -p /mnt/source /mnt/dest
sudo mount -t vfat /dev/sdc1 /mnt/source
sudo mount -t vfat /dev/sdd1 /mnt/dest
sudo rsync -aAXxv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/source /mnt/dest
mount /dev/sdc2 /mnt/source
mount /dev/sdd2 /mnt/dest
sudo rsync -aAXxv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/source /mnt/dest
I haven't tested this but it seems much more efficient to me...
in principle, this should even work while the rpi is running (you would need to change the source to /dev/mmblk0 for copying the MBR and to /boot for the source of the first partition and / for the source of the second partition)

beta-tester
Posts: 1377
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: Backup and Restore: Long and Quick way

Tue Dec 02, 2014 10:10 pm

thawn wrote:Wouldn't it be much quicker to use rsync to copy the actual files once you have restored the MBR and recreated the filesystem?
MBR (Master boot record) ?! the RPi is not using the boot code of the MBR if there is any...
you mean the partitiontable instead of MBR i guess.

my favorite is backup files of boot (/boot) and files of root (/) to tar.xz archives, and the parameters of partitiontable and its filesystems (FAT32 partition1 16MB; ext4 partition 2 >1.5GB).

and to reatore, i recreate the partitiontables and its filesystems anc extract the tar.xz archives to its filesystems on its partitions...
very easy and the best is, by following this procedure, the boot.tar.xz und root.tar.xz are compatible to NOOBS...
so you are able to integrate your backup into NOOBS to distribute the backup to multiple sd cards.
see How to create a custom OS version
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

Return to “Advanced users”