Backup Raspberry Pi image (.img) file – How to resize

While there are multiple backup ways and resize methods out there (most of them failing on the latest raspbian OS – at time of writing Stretch).

I found that the best way to do it (the resize) is  to actually take a snapshot of the OS, live, from within the Raspberry Pi and trim it down to a more manageable size (something that can be written later, on a smaller SD card). Keep in mind, this does not work on Noobs !

The main problem is that no matter what you use to “clone” the original SD with (multiple choices out there – I use Win32Img), it will make a 1:1 snapshot of the card. Sectors, cylinders, partitions, all in a 1:1 snapshot of the original SD card.

While this is something that we actually want and need, it turns into a problem when you need to restore the .img file, on a new SD.

If your new SD doesn’t have an equal (or higher) number of sectors and cylinders, as in both cards (original and new) are listed as equal in size, but your original card has, let’s say 123 sectors  while the new one has 122, you will not be able to write the original backup image file (with 123 sectors) on the card that has only 122 sectors.

That was the problem I faced when I saved my backup file. It so happened that out of 5 available SD cards, I picket (by accident) the (slightly) largest one out of the lot.

Here is the code to the scrip I use to save the backups (we will go into detail on how it works bellow).

This is meant to be used on the host that needs to be backed up.

Depending on the hardware and network speed, this will take a little bit of time to process.

A few things to keep in mind:

  1. Make sure you have a valid path where the img file will be stored. In order to be safe and consistent with the backup, you need to store it either on an attached USB drive or on a mounted network path.
  2. When prompted for the size, enter 0 as the value (if you don’t want to save the image file with the full SD size) and you will be prompted to enter a (smaller) value for the image.
    This will allow you to write the image file to a smaller SD card:

    Root filesize selection
  3. I’d say that in the above example, a size of 5000 (MB) should be more than enough.
  4. You would need to expand the filesystem once the Pi is booted with the new SD via sudo raspi-config:


That’s pretty much all there is to it.

Once you have an image file, you can use Win32Img to write it on the new card and you’re set !

If you have any questions, please don’t hesitate to ask bellow.

Leave a Reply

Your email address will not be published. Required fields are marked *