User avatar
Posts: 16979
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Backup SD card using Pi

Sat Apr 20, 2013 5:28 pm

I've noticed quite a few people have been having issues copying the contents of their SD card to another card, if the size of the destination card is smaller, or maybe don't have another computer to hand that can do this.

I've been thinking about this for a while and think I've come up with a way of safely backing up the SD card to another one (even if the size is slightly different), using just the Pi, a USB card reader, keyboard and screen.

I'm using the "recovery kernel" as supplied on Raspbian and at least some of the other distros (although I've only been working with Raspbian so far). The recovery kernel is loaded on boot if pins 5 and 6 on P1 ( the third pair from the edge of the board) are connected together when power is applied (unless disabled in config.txt).

The advantage of the recovery kernel is that it all runs from a RAMdisk, so the SD card is not in use, especially after I unmount /boot.

I have a script that will read the size of the partitions on the card, check the size of the destination card and see if the contents will fit. It will then partition the new card, copy the FAT partition using dd, copy the contents of the ext4 partition using tar, create a swap partition only if one existed on the original card and that's it. Complete ready to use backup card.

The script would be placed in the /boot partition and copies itself to /tmp before unmounting /boot.

The only real issue I've had so far is that there is no program in the emergency kernel to create an ext4 filesystem. I've managed to get around that for now by mounting the current ext4 partition in read-only mode, linking in the required library files and using mke2fs from there. It seems to work.

Once I've tidied it up a bit, is anyone interested in giving it a try?

Can anyone see any pitfalls I might hit using this method?

Posts: 55
Joined: Sun Jan 15, 2012 10:49 pm

Re: Backup SD card using Pi

Sun Apr 21, 2013 6:50 pm

i haven't had any problems backing up my pi's SD card, but it does sound like something worth pursuing. i did, however have a problem with an SD card going bad. how long does the backup process take on the pi? it takes about 15min to back up my pi's 5G of space (on an 8G SD card) on my main linux box, and took about 12min when i was using a 4G card. since my pi acts as a tor node and web server, keeping downtime to a minimum is important.

User avatar
Posts: 16979
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Backup SD card using Pi

Sun Apr 21, 2013 7:37 pm

If you have another Linux box to use for backup, that would most likely be a faster way to do it.

My idea was for people who only have Windows systems and use the disk imager program that can't cope with restoring to a smaller card, or for people who only have a Pi.

I haven't done any accurate timings yet, but I think it took around 15 minutes to transfer about 1.8GB of files from a 4GB card to a 2GB card, both class 4.

I still need to polish the routine a little, and there is one thing I need to check tomorrow. Other than that, it's looking good so far :)

Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: Backup SD card using Pi

Mon Apr 22, 2013 3:12 am

Would you be interested in contributing this script to PiParted ?
I had planned to do exactly this with PiParted, but if you're willing to contribute will reduce the work required :)
My Blog -
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post !

Return to “General discussion”