Wednesday, June 10, 2015

Write raspbian disk image to SD and Backup your SD Card on Linux

In Linux, we have lot more cool stuffs to create raspberry SD card and backup your SD card in to PC or Laptop. You might need root permission to execute commands with dd tool.

Identify the SD card
  1. Remove if you have any SD card inserted and then run df -h to see what devices are currently mounted.

  2. Insert the SD card and run df -h again. The device that wasn't there last time is your SD card. The left column gives the device name of your SD card. It will be listed as something like /dev/mmcblk0p1 or /dev/sdd1.

    The last part ("p1" or "1" respectively) is the partition number, but you want to write to the whole SD card, not just one partition, so you need to remove that part from the name (getting for example /dev/mmcblk0 or /dev/sdd) as the device for the whole SD card. Note that as in screenshot the SD card can show up more than once in the output of df: in fact it will if you have previously written a Raspberry Pi image to this SD card, because the Raspberry Pi SD images have more than one partition.
  3. Now that you've noted what the device name is, you need to unmount it so that files can't be read or written to the SD card while you are copying over the SD image. So run the command below, replacing mmcblk0p5 with whatever your SD card's device name is (including the partition number) If your SD card shows up more than once in the output of df due to having multiple partitions on the SD card, you should unmount all of these partitions.
    umount /dev/mmcblk0p5
Write raspbian disk image to SD
    WARNING: 
    You might need some knowledge about Linux terminal commands and if something went wrong, you might even lost your data.
    Please note that the use of the dd tool can overwrite any partition of your machine. If you specify the wrong device in the instructions below you could delete your primary Linux partition. Please be careful.

    In the terminal write the image to the card with dd command, making sure you replace the input file if= argument with the path to your .img file, and the /dev/mmcblk0 in the output file of= argument with the right device name (this is very important: you will lose all data on the hard drive on your computer if you get the wrong device name). Make sure the device name is the name of the whole SD card as described above, not just a partition of it (for example, sdd, not sdds1 or sddp1, or mmcblk0 not mmcblk0p1)

    dd bs=1M if=~/Documents/2015-05-05-raspbian-wheezy.img of=/dev/mmcblk0


    Note that if you are not logged in as root you will need to prefix this with sudo. The dd command does not give any information of its progress and so may appear to have frozen. It could take more than five minutes to finish writing to the card. To see the progress of the copy operation you can run pkill -USR1 -n -x dd in another terminal (prefixed with sudo if you are not logged in as root). The progress will be displayed (perhaps not immediately, due to buffering) in the original window, not the window with the pkill command. Another option to monitor progress is to use Pipe Viewer (pv). Pipe dd input part through pv to the output part of dd:

    dd bs=4M if=2014-09-09-wheezy-raspbian.img | pv | dd of=/dev/mmcblk0

    Remove SD card from card reader, insert it in the Raspberry Pi, and have fun.


    Backup your SD Card

    You can simply backup your SD card to PC or Laptop using dd tool. Just only have to swap if and of in the image writing command:

    dd bs=1M if=/dev/mmcblk0 of=~/Documents/raspi-backup.img

    But if the SD card is with higher capacity and plenty of free space; (example of 32GB with only 4GB is used) the image file is a waste of space since it took entire SD card size from your hard drive (32GB image file will save in your hard drive according to the example).

    To avoid hard disk space wastage, you can use gzip to compress your SD card image while you creating it.

    dd if=/dev/mmcblk0 | gzip > ~/Documents/raspi-backup.img.gz

    Then your image will compressed it self and you can extract it later if required. How ever if you need to write your compressed image directly without extracting it from compressed archive, you can do it with following command.

    cat backup.img.gz | gunzip | dd of=/dev/mmcblk0


    No comments:

    Post a Comment