1

I've got an issue with making the backup from microSD 16GB card. My Raspbian installation takes up 2GB and the other space is empty.

When I tried to use dd on my MacBook, I've got an image with 15.93GB.

How can I make a backup without empty space? e.g. image, which size is equal with the size of my Raspbian (it's about 2GB).

Aurora0001
  • 6,357
  • 3
  • 25
  • 39
romankh3
  • 165
  • 1
  • 1
  • 13

2 Answers2

2

There are some possibilities to reduce the size of the backup from a SD Card. You can use rsync as suggested by @binki, or you can use the classic method to do a backup with tar. With the last one you must mount the ext4 filesystem but macOS doesn't know ext4.

So I suggest another method. You can zero-out the free space on the SD Card. This will not reduce the physical size of the image but it can be compressed very much to nearly the size of the used space for data. There is a pseudo device file /dev/zero that will output zeros. On a running Raspberry Pi you can copy it to a zerofile as big as the unused space on the SD Card. Of course it will take some time doing it. For an 1.7 GB zerofile I get this:

rpi ~$ time dd if=/dev/zero of=zerofile bs=4M
dd: error writing 'zerofile': No space left on device
434+0 records in
433+0 records out
1817587712 bytes (1.8 GB, 1.7 GiB) copied, 368.33 s, 4.9 MB/s

real    6m43.538s
user    0m0.002s
sys     0m15.563s

As you see the command will terminate with an error when the disk is full. Just delete zerofile and poweroff the RasPi:

rpi ~$ rm zerofile
rpi ~$ sudo systemctl poweroff

Now you can take a compressed image from the SD Card. Put it into the card reader attached to the laptop. Then assuming it is seen as /dev/sdb:

# On OS X use lower case 'bs=4m'
laptop ~$ sudo bash -c 'dd if=/dev/sdb bs=4M | gzip > backup.img.gz'

To restore do:

# On OS X use lower case 'bs=4m'
laptop ~$ sudo bash -c 'gzip -cd backup.img.gz | dd of=/dev/sdb bs=4M conv=fsync'
Ingo
  • 42,961
  • 20
  • 87
  • 207
1

In short - you need to use rsync.

Have a look at this answer Can a Raspberry Pi be used to create a backup of itself?.
If you plan on keeping the backups on another machine also check my answer on a related question Restore backup failed (using rsync)

binki
  • 61
  • 2