User avatar
CopterRichie
Posts: 131
Joined: Tue Mar 26, 2013 3:14 am
Location: Los Angeles CA.

Using dd to backup a PI SD

Wed Jun 12, 2013 5:19 pm

May I ask please, I want to use Linux DD to make a back up image of my Raspberry Pi SD card now that I have the apps installed.

What is the best way to proceed with this?

Thank you.

kalehrl
Posts: 350
Joined: Tue Jul 24, 2012 10:49 am

Re: Using dd to backup a PI SD

Wed Jun 12, 2013 5:44 pm

Something like this:

Code: Select all

sudo fdisk -l
Take note of how the SD card is identified.
In my case it is /dev/sdb1.
Then:

Code: Select all

sudo dd bs=4M if=/dev/sdb | gzip > /home/your_username/image`date +%d%m%y`.gz
This will compress the image using gzip.
To restore the backup on SD card:

Code: Select all

sudo gzip -dc /home/your_username/image.gz | dd bs=4M of=/dev/sdb
All this must be done on a linux PC.
It won't work on the raspi!

erikcf
Posts: 19
Joined: Thu May 23, 2013 4:17 am

Re: Using dd to backup a PI SD

Wed Jun 12, 2013 9:56 pm

In your restore example, I think you need sudo on the dd command rather than gzip, so your example should look like this:

Code: Select all

gzip -dc /home/your_username/image.gz | sudo dd bs=4M of=/dev/sdb

kalehrl
Posts: 350
Joined: Tue Jul 24, 2012 10:49 am

Re: Using dd to backup a PI SD

Thu Jun 13, 2013 7:40 am

Code: Select all

I think you need sudo on the dd command
Nope, it works without it.
There is sudo in front of gzip so I guess it is also applied to the dd command.
The command lines posted are almost exactly the ones I use myself when backing my SD card.

erikcf
Posts: 19
Joined: Thu May 23, 2013 4:17 am

Re: Using dd to backup a PI SD

Thu Jun 13, 2013 8:00 am

Maybe it depends on the distribution or which shell you are using. I've had mine be picky about where I place the sudo, so I always put it on the command that specifically needs it.

madwoollything
Posts: 14
Joined: Fri Oct 25, 2013 1:13 pm

Re: Using dd to backup a PI SD

Sat Nov 09, 2013 12:27 pm

Great instructions on how to create a compressed backup but I'm struggling to restore the image to a different SD card.

The original SD card seems to have two partitions but the one created using the restore command only seems to have one and will not boot successfully. I've spent some time on the web trying to find how to backup and restore a Raspbian image but most only cover backing up.

Has anyone cracked the problem of restoring an image that will boot?

beejayf
Posts: 24
Joined: Fri Aug 16, 2013 2:30 pm

Re: Using dd to backup a PI SD

Sat Nov 09, 2013 1:52 pm

Don't try to backup a live system - plug your card into your pi with a USB adapter while it runs on another card

madwoollything
Posts: 14
Joined: Fri Oct 25, 2013 1:13 pm

Re: Using dd to backup a PI SD

Sat Nov 09, 2013 2:21 pm

I've created an image from the original PI SD card while inserted in my laptop SD card reader.

Using the created image on the laptop I'm now trying to restore that image to a new blank SD card (to act as a spare). Every time I try to do this only one partition is created on the new SD card where the original Raspbian install has two partitions.

(I'm not trying to backup or restore with the SD card(s) connected directly to the PI but using my laptop).

All I'm trying to achieve is to create a clone of the existing system (via an image) to act as a backup. Sounds as though this should be simple but that is proving remarkably challenging.

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Using dd to backup a PI SD

Sat Nov 09, 2013 2:41 pm

Can we check that the backup contains what you think it should contain? (I'm assuming you are being cautious and still have the original of which you trying to make a clone?)

What are the sizes of the two original partitions reported by the 'sudo fdisk -l' command?

What is the size of the (uncompressed) image file that dd created as a backup?

Hint: the sum of the first two should equal the third.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

madwoollything
Posts: 14
Joined: Fri Oct 25, 2013 1:13 pm

Re: Using dd to backup a PI SD

Sat Nov 09, 2013 2:56 pm

Sizes all check out OK so must be something to do with the restore command given in the post above?

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Using dd to backup a PI SD

Sat Nov 09, 2013 3:25 pm

Some thoughts -

1. in the restoration command

Code: Select all

gzip -dc /home/your_username/image.gz | sudo dd bs=4M of=/dev/sdb
presumably you are substituting your own backup file name with the date embedded in it?

2. if you have uncompressed the backup to check the size, you could try a simplified restore with

Code: Select all

dd bs=4M of=/dev/sdb if=/path/to/my_backup.img
(changing the details for the input file appropriately)

3. note that the output path is simply /dev/sdb - NOT /dev/sdb1

4. is the SD card reader/writer internal to the laptop? There is anecdotal evidence that some built-in SD card slots don't write data via dd correctly.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

madwoollything
Posts: 14
Joined: Fri Oct 25, 2013 1:13 pm

Re: Using dd to backup a PI SD

Sat Nov 09, 2013 4:37 pm

DeeJay wrote: 4. is the SD card reader/writer internal to the laptop? There is anecdotal evidence that some built-in SD card slots don't write data via dd correctly.
Thanks ..... tried all this again being very careful to get all the filenames etc correct and again the same result.
Then tried the exercise using Windows 7 on the same laptop and all worked just fine.

I think you might be right that the problem is dd with this built in SD card slot on the laptop.
Thanks

leopheard
Posts: 49
Joined: Thu Jun 27, 2013 5:34 pm

Re: Using dd to backup a PI SD

Wed Feb 12, 2014 11:54 pm

I'm unsure how I would get this to work in one go when my SD card with Raspbian on is in two partitions:

/dev/mmcblk0p0
/dev/mmcblk0p1

The former containing the boot data.

Any ideas?

User avatar
DougieLawson
Posts: 29715
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Using dd to backup a PI SD

Thu Feb 13, 2014 12:00 am

dd if=/dev/mmcblk0 of=/some/file/name/here BS=1M

That reads the raw device and creates a complete (fuzzy) image of all partitions. It's fuzzy because the filesystem is in use while you're doing it.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

leopheard
Posts: 49
Joined: Thu Jun 27, 2013 5:34 pm

Re: Using dd to backup a PI SD

Thu Feb 13, 2014 12:14 am

DougieLawson wrote:dd if=/dev/mmcblk0 of=/some/file/name/here BS=1M

That reads the raw device and creates a complete (fuzzy) image of all partitions. It's fuzzy because the filesystem is in use while you're doing it.
Cheers Dougie, I have given that a try and it appears to be churning away. Only diff. is that I came across a similar post on Stackexchange earlier which suggests the same thing except BS=4M. I'm assuming the block size doesn't make a massive difference and may just depend on the class of the SD card?

Also, by "fuzzy" do you mean still working?

User avatar
DougieLawson
Posts: 29715
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Using dd to backup a PI SD

Thu Feb 13, 2014 12:32 am

Fuzzy means some files may be open, some files may be updated while you're copying them. (Stuff in /tmp and /var/log which, mostly doesn't matter.) Your copied filesystems may need to be fsck'd before you can use the backup.

BS=nnnM tells dd how much data to read on each I/O operation.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

davidtuti
Posts: 83
Joined: Tue Oct 22, 2013 6:21 am

Re: Using dd to backup a PI SD

Sat Sep 13, 2014 9:51 am

Be careful with these method. I have a lot of problems restore my backup because image is maked with filesystem mounted. So I suggest you shutdown properly the rasp. put sd in Linux and make the backup with sd unmounted

User avatar
pluggy
Posts: 3636
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Using dd to backup a PI SD

Sat Sep 13, 2014 10:49 am

I never bother with the block size when using dd. It works fine with just if= and of= for Pi use.

I'd never attempt it on a 'live' system (trying to copy the card its running off) - its asking for trouble.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

aporeg
Posts: 2
Joined: Sat Oct 10, 2015 11:58 am

Re: Using dd to backup a PI SD

Sat Oct 10, 2015 12:15 pm

Hi,

insert the SD card into your computer and type:

Code:

dmesg

maybe you will see something like this:

mmc0: new ultra high speed SDR104 SDHC card at address aaaa
[519666.121873] mmcblk0: mmc0:aaaa SL16G 14.8 GiB

The important part about this output is "mmcblk0"

Type the following from inside the folder where your image is located (i use an OSMC image which i downloaded before):

Code:

dd bs=4M if=OSMC_TGT_rbp2_20150929.img | pv | dd of=/dev/mmcblk0

Notice "pv" you have to download pv before you use the "dd" command

Code:

sudo apt-get install pv

Try it again:

Code:

dd bs=4M if=OSMC_TGT_rbp2_20150929.img | pv | dd of=/dev/mmcblk0

You will see a progress bar of the hole process.

Have Fun.

drgeoff
Posts: 7317
Joined: Wed Jan 25, 2012 6:39 pm

Re: Using dd to backup a PI SD

Sat Oct 10, 2015 5:08 pm

aporeg wrote: Have Fun.
The fun was had more than a year ago!

aciddrive
Posts: 2
Joined: Wed Jan 13, 2016 4:41 am

Re: Using dd to backup a PI SD

Wed Jan 13, 2016 4:47 am

If you get only one partition when cloning the image to an sd card, make the block size smaller when executing the dd command. bs=32k works, but bs=1M or bigger does not. The other option is to not specify a block size and dd will use the default of 512. It will work but takes forever.

BrixSat
Posts: 1
Joined: Wed Jul 13, 2016 12:11 pm

Re: Using dd to backup a PI SD

Wed Jul 13, 2016 12:13 pm

I have made a script to run on linux that can help you guys to make a backup and a restore. Tested on ubuntu 16.04.

https://github.com/BrixSat/SdcardBackupRestore

Thanks in advance,

mwmwmw
Posts: 2
Joined: Sun Oct 16, 2016 2:41 pm

Re: Using dd to backup a PI SD

Sun Oct 16, 2016 2:47 pm

Is there any way to avid a "fuzzy" copy when using dd on the fly?
I often have problem that the clone sd card doesn't boot while file contents seem to be OK.
When I mount such fuzzy clone sd card on other linux pc, the name of "ROOT" partition becomes strange.

User avatar
DougieLawson
Posts: 29715
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Using dd to backup a PI SD

Sun Oct 16, 2016 6:21 pm

Use the the SDCard copier process that's been added to Raspbian, that's supposed to be reliable and give you a good copy.

https://www.raspberrypi.org/blog/anothe ... -raspbian/
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

mwmwmw
Posts: 2
Joined: Sun Oct 16, 2016 2:41 pm

Re: Using dd to backup a PI SD

Tue Oct 18, 2016 12:30 am

Thank you very much for helpful information!
I can't use the GUI tool, but I find a command line tool, rpi-clone, and it works nicely to duplicate the SD card on the fly.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 25 guests