1

I'm trying to make a backup of my raspberry pi (raspbian) so I can come back to this backup just in case something bad happens. The problem is that I only have 1 SD card. I know that you can use SD Card Copier to copy the files to a USB Drive.

I'm wondering if I can copy the files back from the USB Drive onto the SD card. I would be using Ubuntu 16.04 to do the copy back.

Is this possible?

Thanks.

Test4444
  • 15
  • 1
  • 3

3 Answers3

2

From Ubuntu you can create an SD card image this way (this requires superuser privileges, e.g., via sudo):

dd if=/dev/mmcblk0 of=mybackup.img bs=4M status=progress

Beware the dev node (mmcblk0) is not a partition. There will be mmcblk0p1 and mmcblk0p2 partitions, they are not what you want. You want the whole SD card device. It may be listed as something else, e.g., if you use a USB card adaptor it will be something like sdb (and the partitions, sdb1 and sdb2).

To put the image back on a card:

dd if=mybackup.img of=/dev/mmcblk0 bs=4M status=progress

This will destroy whatever was on the card. There is no point in formatting or prepping it first, dd is all that is needed.

A potential problem with this is that the image may be bigger than the new card, for which reason you may want to shrink it:

Reverse the expand root FS 1

As a final comment, using dd or anything else which copies the entire card everytime is the 2nd worse possible way of maintaining a backup, 1st being not doing anything until it is too late. So it's better than nothing, but you may want to consider an alternative, particularly if you need to keep the backup frequently updated.


  1. That post also explains how to create an empty image using fdisk. If you have a copy of the SD card contents from SD card copier, you can create a new card the exact same way, e.g. fdisk /dev/mmcblk0 instead of fdisk test.img. Make the first partition ~60 MB and the second one can just take the rest of the space, then rsync everything back in.

    You should check the backup copy contents first to make sure it did not include any of the stuff mentioned in that post that should not be in a backup (/proc, /dev, etc.).

goldilocks
  • 56,430
  • 17
  • 109
  • 217
  • I know this is how the common way to do it. But when I have a 128 GB SD card, this creates a 128 GB .img file. When I use the SD Card Copier, it only copies the files from the boot partition and rootfs partition. I can store all that onto a 8GB USB drive. I don't want to create a full .img, I want to copy the files back. – Test4444 Dec 15 '18 at 21:12
  • Good for you. As I said, copying the whole card using `dd` or anything else is an atrocious method, but it is common anyway. I'm unfamiliar with SD card copier, but I've added a footnote above that might be of use to you. – goldilocks Dec 15 '18 at 21:26
  • Ahh okay, looks like Rsync is what I want, no .img. thanks. – Test4444 Dec 15 '18 at 21:34
  • For the adventurous you may want to look into [Timeshift](https://github.com/teejee2008/timeshift/blob/master/README.md). It's not available as a ARM binary for the Pi but can be compiled for it, [see](https://unix.stackexchange.com/a/445910). – Roger Jones Dec 17 '18 at 09:51
1

As you have discovered using SD Copy to a USB Drive is not a good idea.

While I agree with goldilocks that rsync is a more efficient way of backing up files it is not the ideal way to make a security backup of a SD Card.

rsync requires a properly formatted file system for restoration, and will not backup the /boot sector (although this can be done as a separate step).

I use BOTH rsync and dd to backup my Pi.

I regularly create SD Card images (to use as security backup) particularly before major updates - although I rarely use the images. See Clone Raspberry Pi SDCard to file on Mac OS X? (this is macOS specific - there are plenty of examples for other OS) On the few occasions when I have to restore this is simple - and I can go back to historic backups if necessary.

I also use rsync to backup files to disk See Can a Raspberry Pi be used to create a backup of itself?

This is good for saving files BUT only saves the latest copy - as anyone who has been using computers for some time knows having a single backup is a recipe for disaster.

All of my scripts include sanity checks to prevent inadvertent damage to other partitions etc.

Milliways
  • 54,718
  • 26
  • 92
  • 182
0

I have a PI4 with 4GB ram running Bullseye 11. I use a SanDisk USB 3.0 32GB flash drive to operate my PI4, booting and running from the flash drive. These are much better than the SD cards.

I use PI's SD Card Copier to make a complete backup of the running USB drive that I startup & run my PI4 from. It is fast, makes an identical copy (provides option to select new partition ID's, select the box for yes).

I do this after I run updates to the OS and to my Pihole which is my DNS & DHCP server, then I reboot to make sure the new updates come up clean and I am able to log back into my PI4, making sure PIHOLE is working properly.

Then I insert my backup USB 3 flash drive into the PI using either a USB 3 or 2 port. A message comes up asking if I want the File Manager to handle the new drive. I answer cancel. Once completed, I then start SD Card Copier, select my 1st (running) USB drive as Source Copy From, 2nd USB drive as Destination Copy To Drive, then select the New Partition UUIDs checkbox and click on Start.

It takes about 4-6 minutes to complete making a backup currently. I close out the SD Copier Program, select the New Backup Flash Drive in File Manager and eject it. I then shutdown my PI4, swap the two USB drives so I boot up from the new backup and turn on the PI4 to verify it works properly. It works fast & reliably.

Greenonline
  • 2,448
  • 4
  • 18
  • 33